nginx change http request header value based on source address

Let’s say you want to change the value of a header value based on the source location, ie localhost vs external.  Or you can drop it all together. This is how it’s done in nginx.

We’re setting a variable first using map in the http{} block and then use it later in the location{} section.

  • Add this to the http{} section:   if loopback, it will set the $customkey variable to the original header value of ‘custom-key’, else it will default to empty
map $remote_addr $customkey { $http_custom_key ;
     default "";
  • Add this to the location{} section where you want the use this for your proxy_pass:  (re)set the header to the previously set variable or remove it.
proxy_set_header custom-key  $customkey;


Leave a Reply

Your email address will not be published. Required fields are marked *