乐闻世界logo
搜索文章和话题

How to add a response header on nginx when using proxy pass

3个答案

1
2
3

When proxying requests to backend servers in Nginx, it's sometimes necessary to add custom headers to the response. This can be achieved using the add_header directive, which is commonly specified within server or location blocks.

Here is an example of adding custom response headers in an Nginx configuration file:

nginx
server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; # This is your backend server address add_header X-Proxy-Cache $upstream_cache_status; # Other proxy settings, such as setting proxy headers proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

In the above configuration, we add a custom response header named X-Proxy-Cache for all requests proxied through the / location block. The value is set to the $upstream_cache_status variable, which typically indicates the caching status of the backend server.

Note that the add_header directive can add both constant values and dynamically generated values using Nginx variables. However, if you use add_header, you must ensure all default headers are included, as it overrides Nginx's default behavior and only outputs headers explicitly defined in your configuration.

The above configuration is a simple example; you can add other response headers or configure more complex settings as needed.

2024年6月29日 12:07 回复

You can try this solution:

Within your location block, when using proxy_pass, do the following:

shell
location ... { add_header yourHeaderName yourValue; proxy_pass xxxx://xxx_my_proxy_addr_xxx; # Now use this solution: proxy_ignore_headers yourHeaderName // but set by proxy # Or if above didn't work, try this: proxy_hide_header yourHeaderName // but set by proxy }

I'm not sure if this is exactly what you need, but modifying this approach might work for your issue.

You can also use this combination:

shell
proxy_hide_header headerSetByProxy; set $sent_http_header_set_by_proxy yourValue;
2024年6月29日 12:07 回复

The add_header directive works effectively regardless of whether proxy_pass is present or not. I just set up a configuration where I used this directive today. But I must admit that I encountered difficulties during the setup process, though I don't recall the exact reason.

Now I have a working configuration that includes the following (among other things):

nginx
server { server_name .myserver.com location / { proxy_pass http://mybackend; add_header X-Upstream $upstream_addr; } }

Compared to the HttpHeadersMoreModule mentioned in Sebastian Goodman's Stack Overflow answer (link), the add_header directive in nginx versions prior to 1.7.5 only applied to successful responses.

Since nginx 1.7.5, you can include custom headers in error responses using the always keyword. For example:

nginx
add_header X-Upstream $upstream_addr always;

Limitation: You cannot use add_header to override header values in the server context.

2024年6月29日 12:07 回复

你的答案