我试图让我的Nginx反向代理正常工作。 但是,我发现一旦文件被caching,即使后端文件被改变,它也不会更新/重新生效,这可以通过ETag或LAST-Modified字段来识别。 有人可以请帮助我解决这个问题?
这是我的设置:
proxy: proxy.test.com backend: back.example.com
nginx设置:
proxy_cache_path /home/cache levels=1:2 keys_zone=cache_rev:16m inactive=14d max_size=12g; proxy_cache_key "$scheme://$host$request_uri"; server { listen 80 default_server; resolver 74.82.42.42; server_name proxy.test.com; location ~ ^/ { proxy_pass http://back.example.com$request_uri; proxy_redirect off; proxy_buffering on; proxy_cache_revalidate on; proxy_pass_header Set-Cookie; proxy_cache cache_rev; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; proxy_cache_valid 200 1d; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header X-Cache-Status $upstream_cache_status; } }
这是后端文件更改before的curl信息。
在index.html更改之前:
$ curl -I http://proxy.test.com/index.html HTTP/1.1 200 OK Server: nginx/1.4.6 (Ubuntu) Date: Sat, 12 Dec 2015 14:30:18 GMT Content-Type: text/html; charset=utf-8 Content-Length: 18283 Connection: keep-alive Last-Modified: Sat, 12 Dec 2015 14:09:14 GMT ETag: "261e044-476b-526b3fc1b6983" Content-Language: zh-TW X-Cache-Status: HIT Accept-Ranges: bytes $ curl -I http://back.example.com/index.html HTTP/1.1 200 OK Date: Sat, 12 Dec 2015 14:30:32 GMT Server: Apache Last-Modified: Sat, 12 Dec 2015 14:30:32 GMT ETag: W/"261e044-476d-526b44eb57c31" Accept-Ranges: bytes Content-Length: 18285 Content-Type: text/html; charset=utf-8
index.html更改后:
$ curl -I ttp://proxy.test.com/index.html HTTP/1.1 200 OK Server: nginx/1.4.6 (Ubuntu) Date: Sat, 12 Dec 2015 14:30:54 GMT Content-Type: text/html; charset=utf-8 Content-Length: 18283 Connection: keep-alive Last-Modified: Sat, 12 Dec 2015 14:09:14 GMT ETag: "261e044-476b-526b3fc1b6983" Content-Language: zh-TW X-Cache-Status: HIT Accept-Ranges: bytes $ curl -I http://back.example.com/index.html HTTP/1.1 200 OK Date: Sat, 12 Dec 2015 14:30:53 GMT Server: Apache Last-Modified: Sat, 12 Dec 2015 14:30:53 GMT ETag: W/"261e044-476b-526b450fe6a03" Accept-Ranges: bytes Content-Length: 18283 Content-Type: text/html; charset=utf-8
Etags本身不会帮助你。 Etag用于客户端(浏览器)的条件请求,以控制自己的caching。
如果Nginx在其caching中find资源,Nginx不会触及上游。 反向代理的重点是通过每次不连接到upstreadm来加快速度。 您应该在上游服务器上设置Expires: DATE/TIME或Cache-control: max-age=XXX标头(或者更好),让caching知道它可以保持文件caching多长时间。 对于dynamic内容,最好使用cache-control: no-cache 。
查看这些链接,了解HTTP/1.1caching如何工作的更多详细信息:
如果你想明确地从caching中删除某些东西,你必须实现一个清除机制来删除你想要更新的文件。