Nginx + Apache Tomcat configuration example

This tutorial shows you how to configure Nginx as a reverse proxy to redirect the traffics from port 80 to Apache Tomcat on port 8080.

Here is the environment in my Linode server :

  1. Debian 7.5
  2. Nginx 1.2.1
  3. Tomcat 7.0.28

P.S Both Nginx and Tomcat are installed via apt-get install.

1. Tomcat Configuration

Edit server.xml, check the Tomcat listening port, and configure the default path to /apple

/etc/tomcat7/server.xml


  <!-- Tomcat listen on 8080 -->
  <Connector port="8080" protocol="HTTP/1.1"
       connectionTimeout="20000"
       URIEncoding="UTF-8"
       redirectPort="8443" />


    <!-- Set /apple as default path -->
    <Host name="localhost"  appBase="webapps"
         unpackWARs="true" autoDeploy="true">

	 <Context path="" docBase="apple">
	     <!-- Default set of monitored resources -->
	     <WatchedResource>WEB-INF/web.xml</WatchedResource>
	 </Context>

    </Host>

Restart Tomcat, make sure when you access 127.0.0.1:8080, it will display the content in 127.0.0.1:8080/apple

2. Nginx Configuration

In Nginx, edit /etc/nginx/sites-enabled/default, put following content :

/etc/nginx/sites-enabled/default

server {
  listen          80;
  server_name     yourdomain.com;
  root            /etc/tomcat7/webapps/apple;

  proxy_cache one;

  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://127.0.0.1:8080/;
  }
}

It tells Nginx to redirect the traffics from port 80 to Apache Tomcat on port 8080. Done, restart Nginx.

References

  1. NGINX Reverse Proxy

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
7 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
Glenn GouldmohammadamirfazwanThakhaniKanishk Choraria Recent comment authors
newest oldest most voted
mohammad
Guest
mohammad

1 – what is “root /etc/tomcat7/webapps/apple;” for ?

2 – proxy_cache one; has error!?

amirfazwan
Guest
amirfazwan

Hi there, we’ve created a service (startup) called RunCloud to eliminate the tedious process of configuring NGINX and other server configs. Plus, we provide control panel to manage the server. We got 15 days free trial for you to play around. Do visit : http://runcloud.io to try it yourself today.

Charles Cai
Guest
Charles Cai

Looks like you will have to specific the host(ip/domain) in here as well:

So, instead of:
listen 80;

I have to change it to following:
listen 192.168.1.5:80;

This is how I conjured it.

John Pickler
Guest
John Pickler

Static files will be handled by Tomcat? I did not understand what is the reason of Nginx here.

rehnen
Guest
rehnen

It’s probably just to show you how it’s done. I am going to use the example myself as I’m going to have both a node-express.js server and a tomcat on my machine. I want them both to be reachable through port 80, so forwarding through nginx seems like the perfect fit.

Glenn Gould
Guest
Glenn Gould

Nginx is required because Tomcat runs on port 8080 and you cannot map a domain name to an IP address that has port 8080. You map the domain to nginx and nginx redirects the request to Tomcat.

Moorthy
Guest
Moorthy

Could you explain What you are doing in each line please. It would be helpful to us.

Dmitry
Guest
Dmitry

i got following error

Starting nginx: nginx: [emerg] “proxy_cache” zone “one” is unknown in /etc/nginx/nginx.conf:73

nginx: configuration file /etc/nginx/nginx.conf test failed

Gang
Guest
Gang

you need to put

limit_req_zone $binary_remote_addr zone=one:100m rate=1r/s;

on top of
server {

}

Levvy
Guest
Levvy

after that fix I got
[emerg] the shared memory zone “one” is already declared for a different use in /etc/nginx/sites-enabled/tct:9

Kanishk Choraria
Guest
Kanishk Choraria

even I am getting the same thing. Can someone suggest that what we can do after this ?

Thakhani
Guest
Thakhani

Even I am getting the same error

Wilson
Guest
Wilson

Freaking Chinese ! you’re so cool ! :D

skygongle
Guest
skygongle

SB