【问题标题】:Nginx FastCGI Cache Key Not working when using original URL使用原始 URL 时 Nginx FastCGI 缓存密钥不起作用
【发布时间】:2022-06-17 17:38:53
【问题描述】:

我对 Nginx 缓存有疑问

这是我的 Nginx 配置

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=fpmcache:100m max_size=70g inactive=3d use_temp_path=off;

server {
        listen 80;
        server_name example.net;
        root /var/www/mydomain;
        index index.php;


        

        set $original_path $request_uri;

        if ($request_uri ~ "^([^?]*)(\?.*)?$") {
                set $original_path $1;
        }


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

        location ~* \.(css|js|gif|jpeg|jpg|png|ico)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

       

        location ~ \.php$ {

                fastcgi_hide_header "Set-Cookie";
                add_header X-Cache $upstream_cache_status-$ua_device;
                fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
                fastcgi_cache_key "$original_path";
                fastcgi_cache fpmcache;
                fastcgi_cache_valid 200 3d;
                fastcgi_cache_min_uses 1;
                fastcgi_cache_lock on;


                fastcgi_pass   unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

}

我尝试点击我的网址 http://example.net/hello-world

当 nginx 使用这个配置处理 url 时,缓存键是“/hello-world”

set $original_path $request_uri;

        if ($request_uri ~ "^([^?]*)(\?.*)?$") {
                set $original_path $1;
        }

使用 ISP 1

Fit Try X-Cache 未命中 第二次尝试 X-Cache 成功

使用 ISP 2

第一次尝试失败

问题为什么当我使用 ISP 2 时缓存是 MISS ..?而以前使用 ISP 1 X-Cache 很受欢迎。

我尝试调试 $original_path 变量到 access.log is print /hello-world

【问题讨论】:

  • 您的“ISP”是什么?不同的服务器?

标签: php nginx nginx-cache


【解决方案1】:

尝试像这样使用缓存键:

fastcgi_cache_key $request_uri;

这可能会解决设置缓存键的有线问题。

另外,你没有提到 Nginx 版本(更新到最新版本可能是个好主意)

我想知道你为什么使用正则表达式来制作 $original_path?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 2021-01-30
    • 2016-02-21
    • 2017-11-30
    • 2013-01-17
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多