Main menu

Configure Nginx as reverse proxy

Nginx is an high performance HTTP server which is widely used as a Gateway for HTTP and Tomcat applications. We can run Nginx as a public facing HTTP server and run other applications behind the Nginx instances. It will also provide additional layer or security. Let’s configure Nginx as reverse proxy for an JAVA application..

For doing this we have to configure Nginx as a reverse proxy. In this example we are configuring Nginx as a reverse proxy for tomcat application.

Create a new server in Nginx

server {
                  listen       10.10.10.10:8010;
                  server_name  www.yourdomain.com yourdomain.com;
        
        location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://20.20.20.20:8080/;
        }
}b

This configuration will simply listen on 10.10.10.10 on port 80 for incoming request and will forward it to 20.20.20.20 port 8080 of Tomcat.

Tomcat should be running on 20.20.20.20 on port 8080 with protocol HTTP. As much i am having understanding Nginx is not supporting mod_jk.

<Connector port="8080"  address="20.20.20.20" protocol="HTTP/1.1"
  connectionTimeout="20000"
  redirectPort="443" />

As we configured the Nginx to forward request to http://20.20.20.20/ not it’s Application path there mush be a root application in tomcat. We can configure the root application in Tomcat docBase.

 <Context path="" docBase="your_root_app_name" debug="0" reloadable="true">

If you have multiple applications running on JAVA with different domain names, you also need to configure the host name inside server.xml

<Host name="yourdomain.com" debug="0" appBase="webapps/your_app" unpackWARs="true" autoDeploy="true" swallowOutput="true" liveDeploy="false" allowLinking="true">
 <Alias>www.kraftklub.com</Alias>
FacebookTwitterGoogle+RSS