mohan mohan - 1 month ago 9
HTTP Question

Adding and using header (HTTP) in nginx

I am using two system (both are nginx load balancer and one act as an backup). I want to add and use few http custom headers. Please give your suggestion

e.g

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 / {
proxy_pass "http://upstream0;#" is included since links are not allowed in the post
}

}


//Backup system

server{
listen 777;
server_name _;
#doing some other extra stuf

#use port and protocol to direct
}


Thanks

Answer

To add a header just add the following code to the location block where you want to add the header:

location some-location {
  add_header X-my-header my-header-content;      
}

Obviously, replace the x-my-header and my-header-content with what you want to add. And that's all there is to it.