Nginx Reverse Proxy – Configuring Custom Headers from Backend to Client

nginxreverse-proxy

Nginx (19.x) is used as reverse proxy for nodejs backend server (ubuntu). Here is the nginx conf file for reverse proxy:

server {
  listen 80;

  location /api {
    proxy_pass http://127.0.0.1:5000;  //proxy pass for nodejs backend server. Will it pass value of non-empty custom headers as well?? 
  }
}

Both the client and backend server may set custom header for request and response. My understanding is that nginx passes all non-empty custom headers from client to backend server by default. Is there additional config required on nginx to pass custom headers from backend to client? There is proxy_set_header in nginx document but I am not sure if it is for backend to client.

Here is example of custom header defined on backend server.

                res.setHeader("x-auth-token", token);  //custom header 
                res.setHeader("x-auth-token-rsa", tokenRSA); //custom header
                res.setHeader("x-auth-secret",  secret);    //custom header

Best Answer

proxy_set_header is meant to set additional headers when nginx proxies the client's request to backend server.

One use-case for this is to tell original client's IP address to backend server:

proxy_set_header X-Real-IP $remote_addr;

nginx forwards the responses from the server as-is, including all the headers. You don't need to configure anything for it.