I have an nginx web server acting as a reverse proxy to forward requests on to Apache for additional handling (I'm begging you not to ask why). I have a request to which I'm trying to attach a custom header and I'd like for nginx to forward that custom header along to Apache so I can do something with it in an app.

I've poked through the HttpProxyModule docs, but they're not very descriptive even if I'm in the right place (it very well could be that I'm not).

How can I get nginx to forward an X-CUSTOM-REFERRER header? Moreover, if possible, I'd like it to forward along any custom header that comes in. If the latter is too much to ask, the former would suffice for my current need.

As you can see, I'm very new to nginx, so the remedial version would be helpful.



The relevant snippet from my existing config:

location / {
    proxy_pass                  http://preview;
    proxy_redirect              off;
    proxy_set_header            Host $host;
    proxy_set_header            X-Real-IP $remote_addr;
    proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
    # proxy_set_header            X-Custom-Referrer $x_custom_referrer;

The proxy_set_header directive from the HttpProxyModule allows you to do this. For example:

proxy_pass http://apachehost;
proxy_set_header X-Custom-Referrer $proxy_add_<header_field_name_from_last_request>;
  • 1
    Thanks, @mgorven. The incoming value is variable, though. Can I read a value from the incoming X-Custom-Referrer header and pass it along? proxy_set_header X-Custom-Referrer $x_custom_referrer or something to that effect? – Rob Wilkerson May 22 '12 at 21:53
  • 1
    Yes, you can use $http_x_custom_referrer. However, all headers sent by the client should be passed on to the backend automatically. Have you set proxy_pass_request_headers off somewhere? – mgorven May 22 '12 at 21:56
  • Not that I can see. I've updated my question with a snippet from my config file. That header definitely isn't making it to the app, though. That said, I'm sort of assuming that the source apps are sending it, so I'll do my due diligence and be sure if that should be the default behavior. – Rob Wilkerson May 22 '12 at 22:01
  • @RobWilkerson in your snippet the relevant proxy_set_header is commented out; also you are using $x_custom_referrer instead of $http_x_custom_referrer... – severin Feb 24 '15 at 12:43
  • I think @mgorven's answer should be updated to be $http_<custom_header_field> instead of $proxy_add_<custom_header_field> – Tri Nguyen Dec 11 '18 at 21:18

By default the nginx forwards all the ( proxy_pass_request_headers on;) the header to the backend server. But if your request header ( may be custom header) includes underscore ( _ ) in the header name then nginx blocks those headers.

Ex: authenticate_type, cdn_enable.

To enable Nginx to pass all or the custom requested header to the backend turn on the underscore option on.

underscores_in_headers on;
  • thanks, i was missing this config – Ronald Das Jul 7 '20 at 12:01

The module ngx_headers_more allows you to change and add http headers.

  • 1
    This module adds headers to the response. That's not what the OP is asking for. They need to send an additional header to the upstream, in a proxy_pass. – Victor Schröder May 9 '19 at 18:44

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
  • 1
    This is setting headers on the response. The OP wants to set headers to be sent to the upstream of a proxy_pass directive. – Victor Schröder May 9 '19 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.