nginx动静分离,怎么做proxy_pass-----upstream
时间: 2024-01-15 17:05:08 浏览: 138
可以将静态资源和动态资源分别放在不同的服务器上,然后使用nginx的proxy_pass模块将请求转发到对应的upstream中。配置文件示例:
upstream dynamic_backend {
server localhost:8080;
}
upstream static_backend {
server localhost:8090;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://dynamic_backend;
}
location /static/ {
proxy_pass http://static_backend;
}
}
相关问题
upstream fsapiBackend { server 10.20.1.72:8082; server 10.20.1.72:8082 max_fails=10 fail_timeout=30s backup; } upstream fsbulkapiBackend { server 10.20.1.72:8082; } upstream ssapiBackend { server 10.20.1.72:8084; } upstream csapiBackend { server 10.20.1.72:9095; } upstream activityBackend { server 10.20.1.72:8094; } server { listen 80; listen 443 ssl http2; server_name mxapi.hckcooler.com; ssl_certificate /usr/local/openresty/nginx/ssl/mxapi.hckcooler.com/mxapi.hckcooler.com_chain.crt; ssl_certificate_key /usr/local/openresty/nginx/ssl/mxapi.hckcooler.com/mxapi.hckcooler.com_key.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:!aNULL:!MD5:!RC4; ssl_prefer_server_ciphers on; ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /usr/local/openresty/nginx/ssl/mxapi.hckcooler.com/mxapi.hckcooler.com_chain.crt; location /fs/ { proxy_pass http://fsapiBackend/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; } location =/fs/v2/mails/deliver { proxy_pass http://fsapiBackend/v2/mails/deliver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; access_by_lua_file ./app/mailLimit.lua; } location /fsbulk/ { proxy_pass http://fsbulkapiBackend/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; } location /ss/ { proxy_pass http://ssapiBackend/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; } location /approval/ { proxy_pass http://approvalapiBackend/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; } location /job/ { proxy_pass http://jobapiBackend/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; } }中,mxapi.hckcooler.com代理的是哪个服务
在给出的 Nginx 配置中,`mxapi.hckcooler.com` 代理了 `fsapiBackend` 这个服务。可以看到以下配置:
```nginx
location /fs/ {
proxy_pass http://fsapiBackend/;
...
}
location =/fs/v2/mails/deliver {
proxy_pass http://fsapiBackend/v2/mails/deliver;
...
}
```
这些配置指定了请求路径以及对应的代理后端,都是指向 `fsapiBackend`。因此,`mxapi.hckcooler.com` 代理的是 `fsapiBackend` 服务。
解释一下server { listen 81; server_name 10.28.8.1; location / { proxy_pass http://10.8.19.9:81/; client_max_body_size 500m; add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS'; add_header Access-Control-Allow-Origin *; add_header backendCode $upstream_status; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $Server_name; proxy_connect_timeout 30s; proxy_read_timeout 60s; proxy_send_timeout 60s; proxy_buffering off; }
这段配置是一个 Nginx 的服务器块,监听 81 端口,并且以 IP 地址 `10.28.8.1` 作为域名。这个服务器块的主要作用是将客户端的请求代理到另一个服务器上,这个服务器的地址是 `http://10.8.19.9:81/`。
当客户端发送请求时,Nginx 会将请求发送到 `http://10.8.19.9:81/` 上,并且将响应返回给客户端。在这个过程中,Nginx 还会对一些 HTTP Header 进行修改,例如将 `Host`、`X-Real-IP`、`X-Forwarded-For` 等 Header 设置为客户端的真实信息,这些信息对于后端服务器来说是很有用的。
此外,这个配置还添加了一些额外的 Header,例如 `Access-Control-Allow-Methods`、`Access-Control-Allow-Origin` 等,这些 Header 可以帮助客户端进行跨域请求。`client_max_body_size` 则是设置了客户端请求的最大体积,`proxy_buffering off` 则是关闭了 Nginx 的缓存功能。
阅读全文