The Data Asylum

Programming, Databases and People

Adding Headers to Requests Using Nginx and Passenger

TL;DR If you have compiled Passenger into Nginx, use passenger_set_cgi_param instead of proxy_set_header to set HTTP headers required by the rails process.

There are two ways of using Passenger and Nginx together. The first is when the two are compiled into one daemon that creates many processes as required. The second is using Nginx as a proxy in front of Passenger/Apache/Nginx processes running either on the same machine or on a different server altogether.

When using Nginx as a proxy, it can be configured to add or redefine http headers in the request. This is done using the proxy_set_header directive.

proxy_set_header X-Forwarded-For;

If you have compiled Passenger into Nginx, the above configuration will have no effect. This is because the request is not considered to be proxied by Nginx. To have the same effect you need to use the passenger_set_cgi_param directive.

passenger_set_cgi_param HTTP_X_FORWARDED_FOR;

Notice that the header name is all upper case, and has HTTP_ prefixed.