【问题标题】:Caching is not working in nginx using proxy_cache.缓存在使用 proxy_cache 的 nginx 中不起作用。
【发布时间】:2018-09-12 14:46:46
【问题描述】:

我正在尝试在我的 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 {
# Listen on port 8080.
listen 8080;
listen [::]:8080;

# The document root.
root /var/www/cache;

# Add index.php if you are using PHP.
index index.php index.html index.htm;

# The server name, which isn't relevant in this case, because we only have one.
server_name cache.com;

# Redirect server error pages to the static page /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;
}
}

缓存应该可以正常工作,error.log 或 access.log 中没有任何内容,缓存系统文件夹为空,当我从 curl (curl -I) 获取标头时,甚至没有显示带有 $upstream_cache_status 的 X-Cache 标头。现在在我的 nginx (openresty) 配置中没有 --without-ngx_http_proxy_module 标志,所以模块在那里。我不知道我做错了什么请帮忙。

【问题讨论】:

    标签: nginx caching openresty


    【解决方案1】:

    您没有定义任何可以缓存的内容:proxy_cacheproxy_pass 一起使用。

    【讨论】:

    • 所以我必须例如从位置 / 代理传递到位置 /test.html 以缓存 /test.html ?
    • 所以我制作了另一台服务器并使用了代理通行证,然后使用了代理缓存,现在它可以工作了。另外,我将 X-Cache 标头移动到使用代理缓存的位置。现在它按预期工作。我想这是一个愚蠢的错误,但有时这些是最难找到的。感谢您的帮助。
    【解决方案2】:

    http 块中定义的add_header 将被server 块中定义的那个覆盖。这是关于add_header的文档中的sn-p

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

    如果指定了always参数(1.7.5),无论响应码如何,都会添加header字段。

    因此您无法按预期看到 X-Cache 标头。

    【讨论】:

    • 我已经尝试将此标头放在服务器块和位置块中,它仍然没有显示在标头中。
    猜你喜欢
    • 2021-03-31
    • 2015-05-13
    • 2017-11-30
    • 1970-01-01
    • 2015-04-19
    • 2018-10-18
    • 2021-07-09
    • 2018-03-27
    • 2022-06-17
    相关资源
    最近更新 更多