【发布时间】:2020-05-02 05:50:54
【问题描述】:
我想设置一个 NGINX 服务器,它提供以下功能:
- 当 NGINX 发出请求以获取位于 /path/to/page 的页面时,它会获取位于 /path/to/page 的页面。
- 如果上游服务器关闭或 NGINX 因某种原因无法连接到它,NGINX 会返回页面的缓存版本(如果有)。
- 如果缓存文件超过 6 小时,请不要使用它,只需返回 502。
- 如果上游服务器可用,从不使用缓存。
我在这里有一个 NGINX 配置,根据我对文档的理解,我认为它应该可以工作,但它没有,我不明白为什么。问题出在第 (4) 点,即使上游服务器在线,这个 NGINX 服务器也会返回文件的缓存版本。
daemon off;
error_log /dev/stdout info;
events {
}
http {
proxy_cache_path
"/home/jack/Code/NGINX Caching/Codebase/cache" # Cache path
keys_zone=cache:10m # Name of cacahe, max size for keys 10 megabytes
levels=1:2 # Don't store all cached files in a single directory
max_size=500m # Max size of cache
inactive=6h; # Cached file deleted if not used within six hours
proxy_cache_valid 6h;
proxy_cache_key "$request_method$request_uri";
access_log /dev/stdout;
server {
listen 8080;
location ~ ^/(.+)$ {
proxy_pass http://0.0.0.0:8000/$1;
proxy_cache cache;
proxy_cache_valid 6h;
proxy_buffering on;
proxy_cache_use_stale error timeout;
}
}
}
将proxy_cache_path 替换为您机器上的目录路径,并在您机器上的 8000 端口上运行另一个网络服务器。当我修改服务器在 8000 端口上提供的文件时,NGINX 直到看到更改我清除缓存。问题出在 NGINX 而不是我的客户端 (Firefox),即使我在浏览器中关闭缓存,NGINX 也会返回 200 和旧文件内容。
【问题讨论】: