我正在尝试调试nginx代理缓存,并且需要查看$upstream_cache_status值。我的配置如下:
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并没有出现在任何请求中,为什么呢?我怎么才能让它出现?
发布于 2019-02-17 14:51:05
这是因为当您试图设置的值为空时,nginx add_header将移除指定的标头。$upstream_cache_status总是空的,因为您从未将请求传递给upstream。
若要填充此变量,必须将请求传递给一名upstream。例如:
upstream php {
server unix:/run/php/php7.0-fpm.sock;
}upstream必须位于任何server块之外的http块中。
然后,您可以将PHP请求传递到此上游,即:
fastcgi_pass php;当然,您似乎没有定义fastcgi_cache,或者如果定义了,问题中没有显示它,所以在实际设置缓存之前,我希望您在这一点上一无所获。
https://serverfault.com/questions/954333
复制相似问题