tl;dr version
How do you setup nginx
as a reverse proxy for example.com
to a locally running tomcat
webapp at http://127.0.0.1:8080/blah/
without breaking the pageContext
?
Tomcat Setup
There exists a tomcat 7 webapp, blah
, deployed with a .war
file and sitting in /var/lib/tomcat7/webapps/blah/
.
tomcat
is running locally and accessible at http://127.0.0.1:8080
. Multiple webapps are running and can be accessed at:
http://127.0.0.1:8080/blah/
http://127.0.0.1:8080/foo/
http://127.0.0.1:8080/bar/
Port 8080
is blocked externally by the firewall.
Nginx Setup
nginx
is running on the server as the gatekeeper. One site is enabled to access all of the local tomcat webapps mentioned above. This works fine for example.com
:
server {
listen 80;
server_name example.com;
root /var/lib/tomcat/webapps/ROOT/;
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/;
}
}
Question: how to configure an additional site to access blah
directly?
Under /etc/nginx/sites-enabled/
an additional site file is setup to route http://blah.com
to http://127.0.0.1:8080/blah/
but there are issues.
server {
listen 80;
server_name blah.com *.blah.com;
root /var/lib/tomcat/webapps/blah/;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
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/blah/;
}
}
This setup adds an extra blah
to the context path, creating a 404
page because path /blah/blah/
doesn't exist, which makes sense. Is there a simple way within nginx
to
pass blah.com
to the webapp root?
Within the webapp, I'm using ${pageContext.request.contextPath}/path
for relative paths to webapp resource. I thought this was the correct way to handle internal tomcat paths but could this be part of the problem? I believe this is why I'm getting the extra blah
in the route, creating the 404
page.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=${pageContext.request.contextPath}/form">
<script type="text/javascript">
window.location.href = "${pageContext.request.contextPath}/form"
</script>
<title>Load BLAH</title>
</head>
<body>
<p>If you are not redirected automatically, follow this <a href="${pageContext.request.contextPath}/form">link</a>.</p>
</body>
</html>
This page is hit alright but the redirect goes to /blah/blah/form
instead of /blah/form
where the servlet actually exists.
I've also tried other approaches including pointing blah.com
to the tomcat root itself. This works in the sense that you can get to blah
via blah.com/blah/
but that's not really what we want.
Additionally, it is completely acceptable (and desired) to still be able to access blah
via example.com/blah/
.
Obviously, this is for an nginx
novice but help me (and future novices) clear this up because the clear solution is eluding me and the nginx
docs use the help too.
This Work for me:
Environment:
Steps:
1) Install Oracle JDK
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
2) aptitude install tomcat7
3) Configure my context 3.1) - into /etc/tomcat7/Catalina/localhost add mi_context_file.xml
3.2) create my site folder and extract my context
3.3) Add this lines to setup my_site:
3.4) Restart nginx and tomcat7. If your context not start enter into Tomcat7 manager and check the tomcat logs or restart your context from tomcat manager url.
3.5) Enter in your tomcat application context:
4) Nginx references:
One possible solution is to create a virtual host within
tomcat
and setblah
as theROOT
app on the new host.nginx
will pass still pass requests totomcat
on localhost including the requested host header and tomcat will handle the rest with the correct context.Setup the Virtual host
Add a
Host
entry to theEngine
portion of$CATALINA_HOME/conf/server.xml
Create the
appBase
directory$CATALINA_HOME/blahApps/
Configure the
context
with$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml
Deploy
blah
to$CATALINA_HOME/blahApps/ROOT
. This may be as simple as changingblah.war
toROOT.war
.Make sure
nginx
is still copaceticJust proxy requests for
blah.com
to localhost andtomcat
will take care of the rest:I could solve the same issue with some modification, so I leave the record. I use Ubuntu 14.04, I installed tomcat with
sudo apt-get install tomcat7
.I already have a war-demo.war file generated with lein (clojure application), and installed (loaded) from tomcat's manager webapp. The war is copied in
/usr/lib/tomcat7
.Make conf file for nginx
Modify the server.xml in
/var/lib/tomcat7/conf
Copy war file
That's it, I didn't need other configuration. In
/var/lib/tomcat7/conf/Catalina
, we have clojure2.example.com directory. I may be able to add some more configuration in the directory.My way of tuning this in: similar to others, but with some differences System - Ububtu 14.04
1) Create virtual host for the application. You need to add HOST to the server.xml in /etc/tomcat7
2) Place your app to the "webapps" folder of /var/lib/tomcat7
3) Start tomcat - after this you will be able to visit your app locally entering yourapp.com:8080 (port is needed assuming that tomcat is working not on 80 port as this port - 80 - is being listened by NGINX)
3) Go to Nginx configuration and place there following lines
4) Restart NGINX