Nginx FastCGI 缓存 $upstream_cache_status;不显示
Posted
技术标签:
【中文标题】Nginx FastCGI 缓存 $upstream_cache_status;不显示【英文标题】:Nginx FastCGI Cache $upstream_cache_status; Not Showing 【发布时间】:2016-02-21 05:53:44 【问题描述】:所以我一直在尝试在 nginx 上实现 fastcgi 缓存。为了测试缓存是否正常工作,我使用以下行添加了标题以显示缓存状态、HIT、MISS 等:
add_header X-Cache $upstream_cache_status;
问题是标题根本不显示。当我为 X-Cache 标头硬编码一个值时,它会显示出来。
我的nginx版本信息:
nginx版本:由gcc 4.8.4(Ubuntu)构建的nginx/1.4.6(Ubuntu) 4.8.4-2ubuntu1~14.04) 启用 TLS SNI 支持的配置参数:--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format- security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx /nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock /nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib /nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path= /var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_modul e --with-mail --with-mail_ssl_module
我的站点启用配置:
# You may add here your
# server
# ...
#
# statements for each of your virtual hosts to this file
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
#move next 4 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/;
index index.html index.htm index.php;
server_name thinkingtypes.com;
include hhvm.conf;
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST)
set $skip_cache 1;
if ($query_string != "")
set $skip_cache 1;
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml")
set $skip_cache 1;
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in")
set $skip_cache 1;
location /
try_files $uri $uri/ /index.php?$args;
location ~ \.php$
try_files $uri =404;
add_header X-Cache $upstream_cache_status;
include fastcgi_params;
fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
location ~ /purge(/.*)
#fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$
access_log off; log_not_found off; expires max;
location = /robots.txt access_log off; log_not_found off;
location ~ /\. deny all; access_log off; log_not_found off;
【问题讨论】:
【参考方案1】:好吧,我自己解决了这个问题,HHVM 创建了另一个文件 hhvm.conf,它通过 port 或 sock 分别处理 php 文件。我只需要将添加标头放在那里,在该块内即可显示。在其他任何地方,upstream_cache 变量都是空的,因为它没有被设置,因为缓存没有被使用。
【讨论】:
嘿,虽然我没有使用 HHVM,但我也遇到了同样的问题。我只是使用 php-fpm 和 nginx。这可能是什么原因?【参考方案2】:你也可以使用 nginx 模块
more_set_headers
通过安装这个https://github.com/openresty/headers-more-nginx-module#name
nginx 模块。
然后添加此配置以设置您的 nginx 标头。
more_set_headers 'X-Cached: $upstream_cache_status';
【讨论】:
以上是关于Nginx FastCGI 缓存 $upstream_cache_status;不显示的主要内容,如果未能解决你的问题,请参考以下文章
摘自张宴的"实战:Nginx"使用nginx的fastcgi_cache缓存php输出的内容
Nginx FastCGI 缓存 $upstream_cache_status;不显示
Lua with nginx in openresty:如果在 redis 缓存中找不到数据,则将请求传递给 FastCGI