74

I am using two system (both are Nginx load balancer and one act as backup).

I want to add and use few HTTP custom headers.

Below is my code for both:

upstream upstream0 {
    #list of upstream servers
    server backend:80;
    server backup_load_balancer:777 backup;
    #healthcheck
}

server {
    listen 80;
    #Add custom header about the port and protocol  (http or https)
    server_name _;

    location / {
        # is included since links are not allowed in the post
        proxy_pass "http://upstream0;"
    }
}

Backup system

server {
    listen 777;
    server_name _;
    #doing some other extra stuff
    #use port and protocol to direct
}

How can I achieve that?

starball
  • 20,030
  • 7
  • 43
  • 238
mohan
  • 1,340
  • 3
  • 15
  • 27

2 Answers2

155

To add a header, add the add_header declaration to either the location block or the server block:

server {
   add_header X-server-header "my server header content!";
   location /specific-location {
       add_header X-location-header "my specific-location header content!";
   }
}

An add_header declaration within a location block will override the same add_header declaration in the outer server block. e.g. if location contained add_header X-server-header ... that would override the outer declaration for that path location.

Obviously, replace the values with what you want to add. And that's all there is to it.

JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
cobaco
  • 10,224
  • 6
  • 36
  • 33
  • 2
    the $http_HEADER and $send_http_HEADER variables allow accessing the contents of a headers in nginx see http://wiki.nginx.org/HttpCoreModule#Variables – cobaco Aug 15 '12 at 17:42
  • 2
    Does `add_header` work when `proxy_pass` is used? This question seems to contradict it: http://stackoverflow.com/questions/14501047/how-to-add-a-response-header-on-nginx-when-using-proxy-pass – Loïc Faugeron Mar 21 '16 at 15:46
  • @cobaco This config adds this header into response header. is there any way to add in request header while sending it to proxy server ? – Indra Uprade Aug 10 '16 at 19:30
  • 4
    @IndraUprade Headers sent to the backend proxy server are managed with proxy_set_header: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header – bd808 Aug 25 '16 at 16:17
  • @bd808 did not work for me – Peter May 01 '18 at 04:21
  • 1
    Also add `always` at the end to have it work for 400 codes – gdvalderrama Sep 11 '18 at 14:25
  • This inheritance will not work if there is already an `add_header` in the `location` block. In that case, the `add_header` in the `server` block seems to just be ignored. – trinalbadger587 Apr 14 '23 at 09:14
14

You can use upstream headers (named starting with $http_) and additional custom headers. For example:

add_header X-Upstream-01 $http_x_upstream_01;
add_header X-Hdr-01  txt01;

next, go to console and make request with user's header:

curl -H "X-Upstream-01: HEADER1" -I http://localhost:11443/

the response contains X-Hdr-01, seted by server and X-Upstream-01, seted by client:

HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Mon, 30 Nov 2015 23:54:30 GMT
Content-Type: text/html;charset=UTF-8
Connection: keep-alive
X-Hdr-01: txt01
X-Upstream-01: HEADER1
shcherbak
  • 738
  • 8
  • 14