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
?
如何将nginx作为example.com的反向代理设置为本地运行的tomcat webapp (http://127.0.1:8080 /blah/),而不破坏pageContext?
Tomcat Setup
There exists a tomcat 7 webapp, blah
, deployed with a .war
file and sitting in /var/lib/tomcat7/webapps/blah/
.
存在一个tomcat7 web应用程序blah,它与.war文件一起部署,位于/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:
tomcat在本地运行,可以在http://127.0.1:8080访问。多个webapps正在运行,可以访问:
http://127.0.0.1:8080/blah/
- http://127.0.0.1:8080等等
http://127.0.0.1:8080/foo/
- http://127.0.0.1:8080 / foo /
http://127.0.0.1:8080/bar/
- http://127.0.0.1:8080 /酒吧/
Port 8080
is blocked externally by the firewall.
端口8080被防火墙外部屏蔽。
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
:
nginx作为网关管理员在服务器上运行。一个站点可以访问上面提到的所有本地tomcat web应用程序。这对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.
在/etc/nginx/sites-enabled/一个附加的站点文件被设置为将http://blah.com路由到http://127.0.1:8080 /blah/但是有问题。
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?
这个设置为上下文路径添加了一个额外的blah,创建了一个404页面,因为path /blah/blah/不存在,这是有意义的。nginx中是否有一种简单的方法将blah.com传递给webapp根?
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.
在webapp中,我使用${pageContext.request。contextPath}/路径,用于webapp资源的相对路径。我认为这是处理内部tomcat路径的正确方法,但这可能是问题的一部分吗?我相信这就是为什么我在这条路线上得到了额外的废话,创建了404页面。
<%@ 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.
这个页面被点击了,但是重定向到/blah/blah/form而不是servlet实际存在的/blah/form。
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.
我还尝试了其他方法,包括将blah.com指向tomcat根目录本身。它的工作原理是你可以通过blah.com/blah/访问blah,但那并不是我们真正想要的。
Additionally, it is completely acceptable (and desired) to still be able to access blah
via example.com/blah/
.
此外,仍然能够通过example.com/blah/访问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.
显然,这是为nginx的新手准备的,但是请帮助我(和未来的新手)清除这些问题,因为明确的解决方案让我困惑,nginx的文档也使用了帮助。
4 个解决方案
#1
28
One possible solution is to create a virtual host within tomcat
and set blah
as the ROOT
app on the new host. nginx
will pass still pass requests to tomcat
on localhost including the requested host header and tomcat will handle the rest with the correct context.
一种可能的解决方案是在tomcat中创建一个虚拟主机,并将blah设置为新主机上的根应用程序。nginx将把请求传递给本地主机上的tomcat,包括请求的主机头,而tomcat将使用正确的上下文处理其余的请求。
Setup the Virtual host
-
Add a
Host
entry to theEngine
portion of$CATALINA_HOME/conf/server.xml
向$CATALINA_HOME/conf/server.xml的引擎部分添加一个主机条目
<Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> </Host> <Host name="blah.com" appBase="blahApps" unpackWARS="true" autoDeploy="true"> <Alias>www.blah.com</Alias> </Host> </Engine>
-
Create the
appBase
directory$CATALINA_HOME/blahApps/
创建appBase目录$CATALINA_HOME/blahApps/
-
Configure the
context
with$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml
使用$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml配置上下文
<Context path="/" antiResourceLocking="false" />
-
Deploy
blah
to$CATALINA_HOME/blahApps/ROOT
. This may be as simple as changingblah.war
toROOT.war
.部署CATALINA_HOME美元/ blahApps /根。这可能是简单的改变。ROOT.war战争。
Make sure nginx
is still copacetic
Just proxy requests for blah.com
to localhost and tomcat
will take care of the rest:
只需将blah.com的代理请求发送给本地主机,tomcat就可以处理其余的部分:
server {
listen 80;
server_name blah.com www.blah.com;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#2
5
This Work for me:
这个工作对我来说:
Environment:
环境:
- Amazon AWS
- Amazon AWS
- Ubuntu 14.04 TLS / 64 Bits
- ubuntu14.04 TLS / 64位
- Nginx
- Nginx
- Tomcat 7
- Tomcat 7
- Oracle JDK 1.7
- 甲骨文JDK 1.7
Steps:
步骤:
1) Install Oracle JDK
1)安装Oracle JDK
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
2) aptitude install tomcat7
2)安装tomcat7资质
3) Configure my context 3.1) - into /etc/tomcat7/Catalina/localhost add mi_context_file.xml
3)配置我的上下文3.1)-到/etc/tomcat7/Catalina/localhost中添加mi_context_file.xml
<Context path="/MyContext" docBase="local_path_to_my_context_files" privileged="true" reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
3.2) create my site folder and extract my context
3.2)创建我的站点文件夹并提取我的上下文
3.3) Add this lines to setup my_site:
3.3)将这几行添加到设置my_site:
server {
listen 80;
server_name my_site;
#
access_log /my_site_log/access-timed-combined.log timed_combined;
error_log /my_site_log/error.log;
#
root /my_site_folder;
index index.html index.jsp;
#
location @MyContext {
sendfile off;
proxy_pass http://127.0.0.1:8080;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#
location ~ "^/MyContext/*" {
try_files $uri @MyContext;
}
}
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.4)重启nginx和tomcat7。如果您的上下文没有启动,请输入Tomcat7 manager并检查tomcat日志或从tomcat manager url重新启动上下文。
3.5) Enter in your tomcat application context:
3.5)在tomcat应用程序上下文中输入:
http://yoursite/MiContext
4) Nginx references:
4)Nginx引用:
Running Jenkins behind Nginx
https://wiki.jenkins-ci.org/display/JENKINS/Running+Jenkins+behind+Nginx
Load Balancing Apache Tomcat with nginx
http://blogs.mulesoft.org/load-balancing-apache-tomcat-with-nginx/
Nginx + Apache Tomcat Configuration Example
http://www.mkyong.com/nginx/nginx-apache-tomcat-configuration-example/
Configuring Nginx for Apache Tomcat 7
http://blog.rezajp.info/posts/configuring-nginx-for-apache-tomcat-7/
Install Tomcat with Nginx on Ubuntu 13.04 (Server)
http://www.laurii.info/2013/10/install-tomcat-nginx-ubuntu-server-13-04/
Nginx - Java servers like Jetty, GlassFish and Tomcat
http://wiki.nginx.org/JavaServers
Nginx - JavaHandler
http://wiki.nginx.org/JavaHandler
Virtual Host + Nginx + Tomcat
http://www.javacodegeeks.com/2013/02/virtual-host-nginx-tomcat.html
#3
0
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
.
我可以通过一些修改来解决同样的问题,所以我留下记录。我使用Ubuntu 14.04,我安装了tomcat和sudo apt-get安装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
.
我已经有一个战争演示。war文件由lein (clojure应用程序)生成,并从tomcat的manager webapp中安装(装载)。战争是在/usr/lib/tomcat7中复制的。
Make conf file for nginx
server {
listen 80;
server_name clojure2.example.com;
location / {
proxy_pass http://127.0.0.1:8008/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Modify the server.xml in /var/lib/tomcat7/conf
<Host name="clojure2.example.com" appBase="wardemo"
unpackWARS="true" autoDeploy="true">
</Host>
Copy war file
- Create a wardemo directory in `/var/lib/tomcat7
- 在' /var/lib/tomcat7中创建一个wardemo目录
- Copy the war-demo.war in the wardemo directory as ROOT.war
- 复制war-demo。war在wardemo目录中作为ROOT.war
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.
就是这样,我不需要其他的构型。在/var/lib/tomcat7/conf/Catalina中,我们有clojure2.example.com目录。我可以在目录中添加更多的配置。
#4
0
My way of tuning this in: similar to others, but with some differences System - Ububtu 14.04
我的调优方式:与其他类似,但有一些不同的系统- Ububtu 14.04
1) Create virtual host for the application. You need to add HOST to the server.xml in /etc/tomcat7
1)为应用程序创建虚拟主机。您需要向服务器添加主机。xml /etc/tomcat7
<Host name="yourapp.com" appBase="webapps/yourapp" unpackWars="true" autoDeploy="true">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="virtual_log." suffix=".txt" timestamp="true" />
<Context path="" docBase="path to your war" debug="0" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="virtual_log." suffix=".txt" pattern="common" />
</Host>
2) Place your app to the "webapps" folder of /var/lib/tomcat7
2)将应用放在/var/lib/tomcat7的“webapps”文件夹中
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)启动tomcat——在此之后,您将能够访问您的应用程序,并在本地进入yourapp.com:8080(如果tomcat不在80端口上工作,则需要端口,因为NGINX正在监听这个端口80)
3) Go to Nginx configuration and place there following lines
3)转到Nginx配置,并按以下行放置
server {
listen 80;
server_name yourapp.com;
root /var/lib/tomcat7/webapps/yourapp
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name rootapp.com; # this is app that is ROOT
root /var/lib/tomcat7/webapps
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
4) Restart NGINX
4)重启NGINX
#1
28
One possible solution is to create a virtual host within tomcat
and set blah
as the ROOT
app on the new host. nginx
will pass still pass requests to tomcat
on localhost including the requested host header and tomcat will handle the rest with the correct context.
一种可能的解决方案是在tomcat中创建一个虚拟主机,并将blah设置为新主机上的根应用程序。nginx将把请求传递给本地主机上的tomcat,包括请求的主机头,而tomcat将使用正确的上下文处理其余的请求。
Setup the Virtual host
-
Add a
Host
entry to theEngine
portion of$CATALINA_HOME/conf/server.xml
向$CATALINA_HOME/conf/server.xml的引擎部分添加一个主机条目
<Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> </Host> <Host name="blah.com" appBase="blahApps" unpackWARS="true" autoDeploy="true"> <Alias>www.blah.com</Alias> </Host> </Engine>
-
Create the
appBase
directory$CATALINA_HOME/blahApps/
创建appBase目录$CATALINA_HOME/blahApps/
-
Configure the
context
with$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml
使用$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml配置上下文
<Context path="/" antiResourceLocking="false" />
-
Deploy
blah
to$CATALINA_HOME/blahApps/ROOT
. This may be as simple as changingblah.war
toROOT.war
.部署CATALINA_HOME美元/ blahApps /根。这可能是简单的改变。ROOT.war战争。
Make sure nginx
is still copacetic
Just proxy requests for blah.com
to localhost and tomcat
will take care of the rest:
只需将blah.com的代理请求发送给本地主机,tomcat就可以处理其余的部分:
server {
listen 80;
server_name blah.com www.blah.com;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#2
5
This Work for me:
这个工作对我来说:
Environment:
环境:
- Amazon AWS
- Amazon AWS
- Ubuntu 14.04 TLS / 64 Bits
- ubuntu14.04 TLS / 64位
- Nginx
- Nginx
- Tomcat 7
- Tomcat 7
- Oracle JDK 1.7
- 甲骨文JDK 1.7
Steps:
步骤:
1) Install Oracle JDK
1)安装Oracle JDK
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
2) aptitude install tomcat7
2)安装tomcat7资质
3) Configure my context 3.1) - into /etc/tomcat7/Catalina/localhost add mi_context_file.xml
3)配置我的上下文3.1)-到/etc/tomcat7/Catalina/localhost中添加mi_context_file.xml
<Context path="/MyContext" docBase="local_path_to_my_context_files" privileged="true" reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
3.2) create my site folder and extract my context
3.2)创建我的站点文件夹并提取我的上下文
3.3) Add this lines to setup my_site:
3.3)将这几行添加到设置my_site:
server {
listen 80;
server_name my_site;
#
access_log /my_site_log/access-timed-combined.log timed_combined;
error_log /my_site_log/error.log;
#
root /my_site_folder;
index index.html index.jsp;
#
location @MyContext {
sendfile off;
proxy_pass http://127.0.0.1:8080;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#
location ~ "^/MyContext/*" {
try_files $uri @MyContext;
}
}
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.4)重启nginx和tomcat7。如果您的上下文没有启动,请输入Tomcat7 manager并检查tomcat日志或从tomcat manager url重新启动上下文。
3.5) Enter in your tomcat application context:
3.5)在tomcat应用程序上下文中输入:
http://yoursite/MiContext
4) Nginx references:
4)Nginx引用:
Running Jenkins behind Nginx
https://wiki.jenkins-ci.org/display/JENKINS/Running+Jenkins+behind+Nginx
Load Balancing Apache Tomcat with nginx
http://blogs.mulesoft.org/load-balancing-apache-tomcat-with-nginx/
Nginx + Apache Tomcat Configuration Example
http://www.mkyong.com/nginx/nginx-apache-tomcat-configuration-example/
Configuring Nginx for Apache Tomcat 7
http://blog.rezajp.info/posts/configuring-nginx-for-apache-tomcat-7/
Install Tomcat with Nginx on Ubuntu 13.04 (Server)
http://www.laurii.info/2013/10/install-tomcat-nginx-ubuntu-server-13-04/
Nginx - Java servers like Jetty, GlassFish and Tomcat
http://wiki.nginx.org/JavaServers
Nginx - JavaHandler
http://wiki.nginx.org/JavaHandler
Virtual Host + Nginx + Tomcat
http://www.javacodegeeks.com/2013/02/virtual-host-nginx-tomcat.html
#3
0
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
.
我可以通过一些修改来解决同样的问题,所以我留下记录。我使用Ubuntu 14.04,我安装了tomcat和sudo apt-get安装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
.
我已经有一个战争演示。war文件由lein (clojure应用程序)生成,并从tomcat的manager webapp中安装(装载)。战争是在/usr/lib/tomcat7中复制的。
Make conf file for nginx
server {
listen 80;
server_name clojure2.example.com;
location / {
proxy_pass http://127.0.0.1:8008/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Modify the server.xml in /var/lib/tomcat7/conf
<Host name="clojure2.example.com" appBase="wardemo"
unpackWARS="true" autoDeploy="true">
</Host>
Copy war file
- Create a wardemo directory in `/var/lib/tomcat7
- 在' /var/lib/tomcat7中创建一个wardemo目录
- Copy the war-demo.war in the wardemo directory as ROOT.war
- 复制war-demo。war在wardemo目录中作为ROOT.war
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.
就是这样,我不需要其他的构型。在/var/lib/tomcat7/conf/Catalina中,我们有clojure2.example.com目录。我可以在目录中添加更多的配置。
#4
0
My way of tuning this in: similar to others, but with some differences System - Ububtu 14.04
我的调优方式:与其他类似,但有一些不同的系统- Ububtu 14.04
1) Create virtual host for the application. You need to add HOST to the server.xml in /etc/tomcat7
1)为应用程序创建虚拟主机。您需要向服务器添加主机。xml /etc/tomcat7
<Host name="yourapp.com" appBase="webapps/yourapp" unpackWars="true" autoDeploy="true">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="virtual_log." suffix=".txt" timestamp="true" />
<Context path="" docBase="path to your war" debug="0" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="virtual_log." suffix=".txt" pattern="common" />
</Host>
2) Place your app to the "webapps" folder of /var/lib/tomcat7
2)将应用放在/var/lib/tomcat7的“webapps”文件夹中
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)启动tomcat——在此之后,您将能够访问您的应用程序,并在本地进入yourapp.com:8080(如果tomcat不在80端口上工作,则需要端口,因为NGINX正在监听这个端口80)
3) Go to Nginx configuration and place there following lines
3)转到Nginx配置,并按以下行放置
server {
listen 80;
server_name yourapp.com;
root /var/lib/tomcat7/webapps/yourapp
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name rootapp.com; # this is app that is ROOT
root /var/lib/tomcat7/webapps
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
4) Restart NGINX
4)重启NGINX