繁体   English   中英

Nginx FastCGI 缓存 $upstream_cache_status; 不显示

[英]Nginx FastCGI Cache $upstream_cache_status; Not Showing

所以一直在尝试在nginx上实现fastcgi缓存。 为了测试缓存是否正常工作,我添加了标题以使用以下行显示缓存状态、HIT、MISS 等:

add_header X-Cache $upstream_cache_status;

问题是标题根本不显示。 当我为 X-Cache 标头硬编码一个值时,它会显示出来。

我的nginx版本信息:

nginx 版本:nginx/1.4.6 (Ubuntu) 由 gcc 4.8.4 构建 (Ubuntu 4.8.4-2ubuntu1~14.04) TLS SNI 支持已启用配置参数:--with-cc-opt='-g -O2 -fstack-保护器 --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_module --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; }
}

好的,我自己解决了这个问题,HHVM 创建了另一个文件 hhvm.conf,它通过端口或袜子分别处理 php 文件。 我只需要将添加标头放在那里,在该块内即可显示。 在其他任何地方,upstream_cache 变量是空的,因为它没有被设置,因为没有使用缓存。

您也可以使用 nginx 模块

more_set_headers

通过安装这个https://github.com/openresty/headers-more-nginx-module#name nginx 模块。

然后添加此配置以设置您的 nginx 标头。

more_set_headers 'X 缓存:$upstream_cache_status';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM