Nginx Reverse Proxy – Forward Custom Header

nginxreverse-proxy

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.

Thanks.

UPDATE

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;
}

Best Answer

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>;