简体   繁体   中英

Nginx - how to increase fastcgi_cache_valid for a certain path if response status is 200?

I have a certain path on a website, say /static/1234 , and when nginx requests such a path from fastcgi php backend, I want it to be cached for a longer time, but only if the response status is 200. All the other pages should use the default cache settings.

Currently I have the following nginx config:

# delete cache if hasn't been used in 40 minutes
fastcgi_cache_path /var/cache/nginx keys_zone=main:32m inactive=40m levels=2:2;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

# update cache every 30 minutes
fastcgi_cache_valid any 30m;

server {
    fastcgi_cache main;

    root /home/user/app/htdocs;
    index index.php;

    server_name test.dev;

    location /static/ {
        fastcgi_cache_valid 200 10d;
        try_files $uri /index.php;
    }

    location / {
        try_files $uri /index.php;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

I've added $upstream_cache_status to logs, and reduced the cache times to 20 s and 5 minutes to test this configuration. But I'm seeing the every URL is EXPIRED after 20 seconds, so the increase in cache valid time does not work.

How do I make it work?

最后,对我X-Accel-Expires的解决方案是在static控制器的php代码中将X-Accel-Expires标头设置为较高的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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