【问题标题】:Nginx - how to increase fastcgi_cache_valid for a certain path if response status is 200?Nginx - 如果响应状态为 200,如何为某个路径增加 fastcgi_cache_valid?
【发布时间】:2015-09-04 07:55:27
【问题描述】:

我在一个网站上有一个特定的路径,比如/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 分钟来测试此配置。但是我看到每个 URL 在 20 秒后都是 EXPIRED,所以增加缓存有效时间不起作用。

如何让它发挥作用?

【问题讨论】:

    标签: php caching nginx


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2016-11-29
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2016-12-14
      相关资源
      最近更新 更多