nginx代理缓存无法工作

我正在尝试在我的openresty nginx服务器中设置基本缓存。我尝试了来自许多不同教程的数百个不同组合,但是我无法搞定。这是我的nginx.conf文件

user www-data;
worker_processes 4;
pid /run/openresty.pid;
worker_rlimit_nofile 30000;

events {
worker_connections  20000;
}
http {

proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=cache:10m max_size=100m inactive=60m;

proxy_cache_key "$scheme$request_method$host$request_uri";

add_header X-Cache $upstream_cache_status;

include       mime.types;
default_type  application/octet-stream;

access_log /var/log/openresty/access.log;
error_log /var/log/openresty/error.log;

include ../sites/*;

lua_package_cpath '/usr/local/lib/lua/5.1/?.so;;';

}

这是我的服务器配置

server {
# 监听端口8080。
listen 8080;
listen [::]:8080;

# 文档根目录。
root /var/www/cache;

# 如果您使用PHP,则添加index.php。
index index.php index.html index.htm;

# 服务器名称,在这种情况下不相关,因为我们只有一个。
server_name cache.com;

# 将服务器错误页面重定向到静态页面/50x.html。
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root /var/www/cache;
}

location /test.html {

 root /var/www/cache;
 default_type text/plain;
 try_files $uri /$uri;
 expires 1h;
 add_header Cache-Control "public";
 proxy_cache cache;
 proxy_cache_valid 200 301 302 60m;
}
}

缓存应该工作正常,错误日志或访问日志中没有任何内容,缓存系统文件夹为空,从curl(curl -I)获取头时甚至不会显示$upstream_cache_status的X-Cache标头。现在在我的nginx(openresty)配置中没有--without-ngx_http_proxy_module标志,因此该模块存在。请帮帮我,我不知道我做错了什么。

点赞
用户369550
用户369550

你没有定义任何可以被缓存的内容:proxy_cacheproxy_pass 一起工作。

2018-09-11 18:17:17
用户5069327
用户5069327

http 块中定义的 add_header 会覆盖在 server 块中定义的那个。以下是关于 add_header 的部分文档片段:

可能会有多个 add_header 指令。只有在当前级别上没有定义 add_header 指令时,这些指令才会从前一个级别继承

如果指定了 always 参数(1.7.5),则无论响应代码如何,都将添加标题字段。

所以无法按预期看到 X-Cache 标题。

2018-09-12 06:46:46