首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nginx $upsteam_cache_status自定义头将不会出现

Nginx $upsteam_cache_status自定义头将不会出现
EN

Server Fault用户
提问于 2019-02-17 05:06:54
回答 1查看 1.6K关注 0票数 3

我正在尝试调试nginx代理缓存,并且需要查看$upstream_cache_status值。我的配置如下:

代码语言:javascript
复制
http {
    ...
    proxy_cache_path /tmp/cache_nginx levels=1:2 keys_zone=cfcache:10m max_size=2g inactive=10m use_temp_path=off;
    ...

    server {
        listen 80;
        listen [::]:80;
        server_name domain.com;
        root  /home/site/wwwroot;
        error_log /home/logfiles/nginx/error.log;

        proxy cache cfcache;

        add_header Custom-header-test Value;
        add_header X-Cache-Status $upstream_cache_status always;


        #index file redirect
        index index.php;

        location = /favicon.ico {
          log_not_found off;
          access_log off;
        }

        location = /robots.txt {
          allow all;
          log_not_found off;
          access_log off;
        }

        # Block access to "hidden" files and directories whose names begin with a
        # period. This includes directories used by version control systems such
        # as Subversion or Git to store control files.
        location ~ (^|/)\. {
           return 403;
        }

        location / {
            try_files $uri $uri/ $uri.html @php;
        }

        location @php {         
            rewrite ^(/[^/]+)$ $1.php last;
            rewrite ^(/[^/]+)/(.*)$ $1.php?q=$2 last;
        }

        #404 error page
        error_page 404 /notfound.php;

        location ~ \.php$  {
          try_files $uri =404;
          include fastcgi_params;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
          fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
          fastcgi_connect_timeout         300; 
          fastcgi_send_timeout           3600; 
          fastcgi_read_timeout           3600;
          fastcgi_buffer_size 128k;
          fastcgi_buffers 4 256k;
          fastcgi_busy_buffers_size 256k;
          fastcgi_temp_file_write_size 256k;
          fastcgi_intercept_errors on; 
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME /home/site/wwwroot$fastcgi_script_name;
        }

        #cache static files
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|svg+xml)$ {
          expires max;
          log_not_found off;
        }
    }
}

Custom-header-test如预期的那样出现,值为Value

另一方面,X-Cache-Status并没有出现在任何请求中,为什么呢?我怎么才能让它出现?

EN

回答 1

Server Fault用户

发布于 2019-02-17 14:51:05

这是因为当您试图设置的值为空时,nginx add_header将移除指定的标头。$upstream_cache_status总是空的,因为您从未将请求传递给upstream

若要填充此变量,必须将请求传递给一名upstream。例如:

代码语言:javascript
复制
upstream php {
    server unix:/run/php/php7.0-fpm.sock;
}

upstream必须位于任何server块之外的http块中。

然后,您可以将PHP请求传递到此上游,即:

代码语言:javascript
复制
fastcgi_pass php;

当然,您似乎没有定义fastcgi_cache,或者如果定义了,问题中没有显示它,所以在实际设置缓存之前,我希望您在这一点上一无所获。

票数 2
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/954333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档