Nginx not processing proxy_hide_header and proxy_ignore_header directives
I have trouble trying to make Nginx ignore and hide some headers from a proxied server.
I want Nginx to hide and ignore the "Cache-Control" and "Server" headers but it didn't work, I don't know why.
My conf is as follows :
location / {
proxy_pass http://111.131.50.42;
proxy_hide_header Cache-Control;
expires 60M;
add_header Cache-Control "public";
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
access_log off;
}
But even with that I'm still receiving the origin "Cache-Control" and "Server" headers.
Do you know what I'm doing wrong?
P.S. I can't use ngx_headers_more module. I'm not able to add this module to our Nginx installation.
Solution 1:
Directive proxy_ignore_headers
tells nginx to ignore the content of special headers leading to specific internal behaviour :
“X-Accel-Expires”, “Expires”, “Cache-Control”, “Set-Cookie”, and “Vary” set the parameters of response caching;
“X-Accel-Redirect” performs an internal redirect to the specified URI;
“X-Accel-Limit-Rate” sets the rate limit for transmission of a response to a client;
“X-Accel-Buffering” enables or disables buffering of a response;
“X-Accel-Charset” sets the desired charset of a response.
If you want to hide headers from upstream servers you need to use proxy_hide_header
. The Server
header is not passed to the response sent to a client by default, as Date
, X-Pad
, and X-Accel-...
headers.
So, this should work :
location / {
access_log off;
add_header Cache-Control "public";
proxy_pass http://111.131.50.42;
proxy_hide_header Cache-Control;
expires 60M;
}
Make sure you are not testing from a browser with cached data already, use curl
.