Nginx FastCGI Cache $ upstream_cache_status;不显示

时间:2015-11-19 07:48:29

标签: caching nginx server

所以我一直在尝试在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-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; }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你也可以使用nginx模块

<块引用>

more_set_headers

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

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

<块引用>

more_set_headers 'X 缓存:$upstream_cache_status';