简体   繁体   English

nginx 作为缓存代理不缓存任何东西

[英]nginx as cache proxy not caching anything

I'm trying to cache static content which are basically inside the paths below in virtual server configuration.我正在尝试缓存 static 内容,这些内容基本上位于虚拟服务器配置中的以下路径内。 For some reason files are not being cached.由于某些原因,文件没有被缓存。 I see several folders and files inside the cache dir but its always something like 20mb no higher no lower.我在缓存目录中看到了几个文件夹和文件,但它总是大约 20mb 不高不低。 If it were caching images for example would take at least 500mb of space.例如,如果它缓存图像,则至少需要 500mb 的空间。

Here is the nginx.conf cache part:这是 nginx.conf 缓存部分:

** nginx.conf **
proxy_cache_path /usr/share/nginx/www/cache levels=1:2 keys_zone=static$
proxy_temp_path /usr/share/nginx/www/tmp;
proxy_read_timeout 300s;

Heres the default virtual server.这是默认的虚拟服务器。

**sites-available/default**
server {
    listen   80; 

    root /usr/share/nginx/www;
    server_name myserver;
    access_log /var/log/nginx/myserver.log main;
    error_log /var/log/nginx/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;

    location ~* ^/(thumbs|images|css|js|pubimg)/(.*)$ {
            proxy_pass http://backend;
            proxy_cache static;
            proxy_cache_min_uses 1;
            proxy_cache_valid 200 301 302 120m;
            proxy_cache_valid 404 1m;
            expires max;
    }

    location / {
            proxy_pass http://backend;
    }
}

Make sure your backend does not return Set-Cookie header.确保您的后端不返回Set-Cookie标头。 If Nginx sees it, it disables caching.如果 Nginx 看到它,它会禁用缓存。

If this is your case, the best option is to fix your backend.如果是这种情况,最好的选择是修复您的后端。 When fixing the backend is not an option, it's possible to instruct Nginx to ignore Set-Cookie header当修复后端不是一个选项时,可以指示 Nginx 忽略Set-Cookie标头

proxy_ignore_headers "Set-Cookie";
proxy_hide_header "Set-Cookie";

See the documentation查看文档

proxy_ignore_header will ensure that the caching takes place. proxy_ignore_header将确保缓存发生。 proxy_hide_header will ensure the Cookie payload is not included in the cached payload. proxy_hide_header将确保缓存的有效负载中不包含 Cookie 有效负载。 This is important to avoid leaking cookies via the NGINX cache.这对于避免通过 NGINX 缓存泄漏 cookie 很重要。

I would like to add that multiple configuration options and combinations can disable proxy caching in Nginx.我想补充一点,多个配置选项和组合可以禁用 Nginx 中的代理缓存。 Unfortunately this is poorly documented.不幸的是,这没有很好的记录。

In my configuration I set proxy_buffering on and it enabled caching as expected.在我的配置中,我设置了proxy_buffering on并按预期启用了缓存。

after going through multiple answers and comments, i found this configuration finally works:在浏览了多个答案和评论后,我发现这个配置终于有效了:

10m = 10mb key cache, max_size to 2GB , inactive=120m (refresh from source after 120minutes of inactive), use_temp_path=off (to reduce io) 10m = 10mb密钥缓存, max_size2GBinactive=120minactive=120m分钟后从源刷新), use_temp_path=off (减少 io)

proxy_cache_valid - cache status of 200 and 302 for 60 minutes proxy_cache_valid - 缓存状态为200302 60 分钟

proxy_cache_path /tmp/cache levels=1:2 keys_zone=default_cache:10m max_size=2g
                 inactive=120m use_temp_path=off;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 60m;

server {
    listen       80;
    server_name  example.com;

    # https://www.nginx.com/blog/nginx-caching-guide
    location / {
        proxy_cache default_cache;
        proxy_buffering on;
        proxy_ignore_headers Expires;
        proxy_ignore_headers X-Accel-Expires;
        proxy_ignore_headers Cache-Control;
        proxy_ignore_headers Set-Cookie;

        proxy_hide_header X-Accel-Expires;
        proxy_hide_header Expires;
        proxy_hide_header Cache-Control;
        proxy_hide_header Pragma;

        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_pass http://ip-of-host:80;

        #set            $memcached_key "$uri?$args";
        #memcached_pass 127.0.0.1:11211;
        # error_page     404 502 504 = @fallback;
    }
}

Another cause which i just ran into is with the nginx server system clock being in the future (or has too much drift into the future against the clock of the origin server that is defining the Expire headers), it will just consider content not to be cached since it is already expired (according to it's own time reference).我刚刚遇到的另一个原因是 nginx 服务器系统时钟在未来(或者与定义 Expire 标头的原始服务器的时钟相比未来有太多漂移),它只会考虑内容不缓存,因为它已经过期(根据它自己的时间参考)。 The server was not syncing against NTP...服务器未与 NTP 同步...

From https://www.nginx.com/blog/nginx-caching-guide/ :来自https://www.nginx.com/blog/nginx-caching-guide/

NGINX caches a response only if the origin server includes either the Expires header with a date and time in the future , or the Cache-Control header with the max-age directive set to a non‑zero value. NGINX仅在原始服务器包含带有未来日期和时间的 Expires header或 max-age 指令设置为非零值的 Cache-Control header 时才缓存响应。

For what it's worth, my experience is that nginx does not always cache things where you tell it to.就其价值而言,我的经验是 nginx 并不总是在您告诉它的地方缓存内容。

For example, on centos7, with the configuration option比如在centos7上,用配置选项

proxy_cache_path /tmp/my_nginx_cache levels=1:2 keys_zone=my_zone:10m inactive=24h max_size=1g;

nginx actually caches the files at: nginx 实际上将文件缓存在:

/tmp/systemd-private-phJlfG/tmp/my_nginx_cache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM