7

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.

1 Answer 1

12

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.

1
  • 1
    "hide headers from upstream servers you need to use proxy_hide_header" - but according to the manual (nginx.org/en/docs/http/…) "nginx does not pass the header [....] from the response of a proxied server to a client."
    – symcbean
    Sep 15, 2022 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .