Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chaosology-Asartheen/9e4d203d661d116c2400b855c5ee6829 to your computer and use it in GitHub Desktop.
Save Chaosology-Asartheen/9e4d203d661d116c2400b855c5ee6829 to your computer and use it in GitHub Desktop.
Configuring Nginx for Apache Tomcat 7
Configuring Nginx for Apache Tomcat 7
September 23, 2011 Java, NginX, Tomcat, Ubuntu 19 comments
Apache Tomcat can be the first choice of an Application Server for someone new to Java world. There are obviously other servers such as JBoss Application Server and IBM WebSphere but those may be slightly difficult for a newbie.
In some situations, like when we are serving a few websites with Nginx over port 80 (HTTP), it is required to configure Nginx in order to serve our Java web application which is running on Apache Tomcat. It is basically using Nginx as a Reverse Proxy This can be done with a new server section in Nginx configuration file (it is in /etc/nginx/sites-available/default by default) :
server {
listen mysite.com:80;
server_name mysite.com;
root /path/to/tomcat/webapps/mysite/;
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:8082/;
}
}
This is the simplest configuration and we can obviously have different sub-locations and various configurations for different purposes.
The port 8082 is the one that Tomcat is listening to. This can be found in /path/to/tomcat/conf/server.xml
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
It is also important that our Java web application is in root which is http://127.0.0.1:8082/ instead of http://127.0.0.1:8082/mysite/ . We can easily do this with adding a section to server.xml file inside <Host> tag.
<Context path="" docBase="mysite" debug="0" reloadable="true">
Restaring Tomcat and then Nginx at the end will make our web application available on the configured domain through port 80 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment