Montag, 26. August 2013

Nginx, remote_addr and Tomcat in reverse proxy configuration

Many people use Nginx as reverse proxy and their applications may depend on remote_addr request variable. The following steps help to correct it.

First of all check if Nginx contains necessary module be executing the command:

nginx -V

You should see this line in the output:

--with-http_realip_module

Then you need to add following lines to your Nginx configuration:

set_real_ip_from   111.111.111.111;
real_ip_header    X-Real-IP;
real_ip_recursive on;

And at the end add this to your Tomcat server.xml

 <Valve
   className="org.apache.catalina.valves.RemoteIpValve"
   internalProxies="111\.111\.111\.111"
   remoteIpHeader="x-forwarded-for"
   remoteIpProxiesHeader="x-forwarded-by"
   protocolHeader="x-forwarded-proto"
   />

How to configure valve you may read here. Do not forget to replace 111.111.111.111 with your server IP.

Good luck!