Nginx - 如果响应状态为200,如何为某个路径增加fastcgi_cache_valid?

时间:2015-06-18 15:17:16

标签: php caching nginx

我在网站上有一条特定的路径,比如/static/1234,当nginx从fastcgi php后端请求这样的路径时,我希望它被缓存更长时间,但仅当响应状态为200时所有其他页面都应使用默认缓存设置。

目前我有以下nginx配置:

# 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;
    }
}

我已将$upstream_cache_status添加到日志中,并将缓存时间减少到20秒和5分钟以测试此配置。但是我在20秒后看到每个URL都是EXPIRED,因此缓存有效时间的增加不起作用。

如何让它发挥作用?

1 个答案:

答案 0 :(得分:0)

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

相关问题