Using proxy_pass_header in Nginx

nginx proxy pass header
26 April 2025

Using proxy_pass_header in Nginx

When working with Nginx, one of the most important features we can use is reverse proxy. This feature allows us to forward requests from clients to internal servers. For this purpose, Nginx can act as a proxy, and in such cases, we need to also add some headers to requests or pass them along in responses. Here, we will delve into the details of proxy_pass_header.

proxy_pass_header is an Nginx directive that allows us to specify which headers should be passed from a proxied response to the client. In other words, when Nginx forwards a request to the internal server and receives a response, using this directive allows us to retain certain headers in the response for the client. This is especially useful when we need specific information from the original server to be forwarded to the client.

For example, let's assume we have an API that uses multiple different servers for responding to requests. In this case, it might be important to retain specific headers like Set-Cookie or Location. By using proxy_pass_header, we can specify which headers should be forwarded to the client.

Note that when certain headers are passed back to the client, it is important to ensure the security and confidentiality of user-specific information to avoid sensitive data exposure.

Example Code

server {
listen 80;
server_name example.com;

location /api/ {
proxy_pass http://backend-server;
proxy_pass_header Set-Cookie;
proxy_pass_header Location;
}
}

Line-by-Line Explanation

line 1 - 2:server { and listen 80; indicate to Nginx that it should listen for requests on port 80.


line 3: server_name example.com; specifies the domain that the server should respond to.


line 5: location /api/ { defines a block for the paths starting with /api/.


line 6: proxy_pass http://backend-server; sends requests to the backend server defined here.


line 7: proxy_pass_header Set-Cookie; tells Nginx that the Set-Cookie header should be passed to clients.


line 8: proxy_pass_header Location; also specifies that the Location header should be retained for clients.


FAQ

?

What are the benefits of using proxy_pass_header?

?

How can I use multiple different headers?

?

Can I reference other servers as well?