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

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

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

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

As we configured the Nginx to forward request to 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="" debug="0" appBase="webapps/your_app" unpackWARs="true" autoDeploy="true" swallowOutput="true" liveDeploy="false" allowLinking="true">