我正在使用 nginx 作为负载均衡器。这是我的 nginx 配置文件的简化版本。
upstream myUpstream {
server server1.com;
server server2.com;
}
server{
location / {
proxy_pass https://myUpstream;
}
}
为了利用浏览器缓存进行我的 rest api 调用,我将后端服务器设置为设置在 1 小时内过期的缓存标头。
当我将请求直接发送到 server1.com 或 server2.com 时,一切都运行正常,并且缓存响应标头也很好。
但是当我将请求发送到负载均衡器服务器时,我收到两个缓存响应标头,一个来自负载均衡器,另一个来自后端服务器,并且浏览器不会缓存我的响应。
我的问题是如何配置我的负载均衡器服务器,以便它不会将其自己的缓存标头放入我的 rest api 响应中?
这是我的完整 nginx 配置文件:
limit_req_zone $binary_remote_addr zone=mylimit:100m rate=10r/s;
upstream myUpstream {
ip_hash;
server server1.com weight=1;
server server2.com weight=1;
}
server {
#---- Server Domain neme
server_name www.server.com;
keepalive_requests 100;
keepalive_timeout 20s;
listen 443 ssl http2 ;
listen [::]:443 ssl http2 ;
# ssl config
ssl_certificate path.pem;
ssl_certificate_key privateKeyPath.pem;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:SALT;
ssl_prefer_server_ciphers on;
location / {
limit_req zone=mylimit burst=10 nodelay;
proxy_pass https://myUpstream;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
if ($host = server.com) {
return 301 https://$host$request_uri;
}
listen 80;
server_name server.com;
return 301 https://$server_name$request_uri;
}
server {
if ($host = www.server.com) {
return 301 https://$host$request_uri;
}
listen 80;
server_name www.server.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2 ;
listen [::]:443 ssl http2 ;
ssl_certificate path.pem;
ssl_certificate_key privatekeyPath.pem;
server_name server.com;
return 301 https://www.$server_name$request_uri;
}