Nginx $upsteam_cache_status custom header will not appear
I am trying to debug the nginx proxy cache and to do so I need to see the $upstream_cache_status
value. My config is as follows:
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
is appearing as expected, with the value Value
X-Cache-Status
on the other hand is not appearing on any requests, why is that? How can I get it to appear?
Solution 1:
This is happening because nginx add_header
removes the specified header, when the value you try to set is empty. And $upstream_cache_status
is always empty, because you never pass the request to an upstream
.
To populate this variable, you must pass requests to a named upstream
. For example:
upstream php {
server unix:/run/php/php7.0-fpm.sock;
}
upstream
must be in the http
block, outside of any server
block.
You then can pass PHP requests to this upstream, i.e.:
fastcgi_pass php;
Of course, you don't seem to have a fastcgi_cache
defined, or if you do, it was not shown in your question, so I would expect you to get nothing at this point until you actually set up a cache.