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.

1
proxy_set_header X-Forwarded-For 127.0.0.1;

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.

1
passenger_set_cgi_param HTTP_X_FORWARDED_FOR 127.0.0.1;

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

Comments