簡體   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