【发布时间】:2020-09-17 16:16:03
【问题描述】:
我在使用 nginx 作为负载平衡器时遇到了问题。我可以将它配置为负载均衡器,但我不知道如何让它缓存来自后端代理服务器的静态内容,例如 html、css、js 等......这意味着我希望 nginx 天气是否缓存取决于后端服务器的响应内容,如果它更改为绕过缓存并将请求发送到后端,如果不从缓存中提供服务。我在互联网上尝试并搜索了很多,以使用许多指令(例如 proxy_cache_bypass 和 proxy_no_cache)来实现它,但我做不到。如果有人在此类主题方面有经验,是否有任何方法可以做到这一点。这些是配置:
upstream backend {
server www.webserver1.com:443 max_fails=3 fail_timeout=15s;
server www.webserver2.com:443 max_fails=3 fail_timeout=15s;
}
server {
listen 443 ssl;
rewrite_log on;
error_log /var/log/nginx/lb.error.log;
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_set_header X-Proxy-Cache $upstream_cache_status;
ssl_certificate /etc/nginx/client.crt;
ssl_certificate_key /etc/nginx/client.key;
ssl on;
location / {
proxy_cache backcache;
#proxy_cache_methods GET HEAD POST;
#proxy_cache_bypass $cookie_nocache $arg_nocache;
#proxy_no_cache $cookie_nocache $arg_nocache;
proxy_cache_min_uses 1;
#proxy_cache_revalidate on;
#proxy_cache_valid 200 4m;
proxy_cache_lock on;
proxy_cache_background_update on;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_pass https://backend;
}
}
server {
listen 80 ;
if ($http_x_forwarded_proto != 'https') {
rewrite ^(.*) https://$host$1 redirect;
}
}
这些是配置的内容。 /etc/nginx/conf.d/ 下的文件,该文件包含在主配置中。文件是 /etc/nginx/nginx.conf 并且这两行也在主配置中。文件:
proxy_cache_path /var/lib/nginx/cache keys_zone=backcache:20m max_size=100m;
proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args$cookie_user";
【问题讨论】:
-
你能发布你的配置吗?
-
@Danizavtz,好的,我将我的配置添加到原始帖子中,我忘了告诉我,除了充当负载均衡器之外,我还配置了 nginx 从 http 重定向到 https,以便它与2 个后端服务器已加密。