Nginx配置可将上下文直接传递给tomcat Webapp [英] Nginx configuration to pass site directly to tomcat webapp with context

查看:175
本文介绍了Nginx配置可将上下文直接传递给tomcat Webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr版本

如何在不破坏pageContext的情况下,将nginx设置为example.com到本地运行的tomcat webapp的反向代理,作为example.com?


Tomcat设置

存在一个 tomcat 7 网络应用程序blah,其中部署了.war文件并位于/var/lib/tomcat7/webapps/blah/.

tomcat在本地运行,可以在http://127.0.0.1:8080上访问.多个Web应用程序正在运行,可以在以下位置访问:

  • http://127.0.0.1:8080/blah/
  • http://127.0.0.1:8080/foo/
  • http://127.0.0.1:8080/bar/

端口8080被防火墙从外部阻止.

Nginx设置

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/;
  }
}

问题:如何配置其他站点以直接访问blah?

/etc/nginx/sites-enabled/下,设置了另一个站点文件以将http://blah.com路由到http://127.0.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/;
  }
}

此设置为上下文路径添加了额外的blah,从而创建了404页面,因为路径/blah/blah/不存在,这是有道理的. nginx中是否有一种简单的方法可以 通过blah.com到webapp根目录?

在webapp中,我使用${pageContext.request.contextPath}/path来获取webapp资源的相对路径.我认为这是处理内部tomcat路径的正确方法,但这可能是问题的一部分吗?我相信这就是为什么我会在路由中获得额外的blah并创建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>

此页面可以正常运行,但重定向将转到/blah/blah/form而不是servlet实际存在的/blah/form.


我还尝试了其他方法,包括将blah.com指向tomcat根目录本身.从某种意义上说,您可以通过blah.com/blah/进入blah,但这并不是我们想要的.

此外,仍然可以通过example.com/blah/访问blah是完全可以接受的(并且是所希望的).

显然,这是针对nginx新手的,但可以帮助我(和将来的新手)解决此问题,因为明确的解决方案使我难以理解,并且nginx文档也使用了该帮助.

解决方案

一种可能的解决方案是

  • 创建appBase目录$CATALINA_HOME/blahApps/

  • 使用$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml

    配置context

    <Context path="/" 
        antiResourceLocking="false" />
    

  • blah部署到$CATALINA_HOME/blahApps/ROOT.这可能就像将blah.war更改为ROOT.war一样简单.

  • 确保nginx仍然是麻醉的

    只需将blah.com的代理请求发送到localhost和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;
      }
    }
    

    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.

    解决方案

    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.

    Setup the Virtual host

    1. Add a Host entry to the Engine portion of $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>
      

    2. Create the appBase directory $CATALINA_HOME/blahApps/

    3. Configure the context with $CATALINA_HOME/blahApps/ROOT/META-INF/context.xml

      <Context path="/" 
          antiResourceLocking="false" />
      

    4. Deploy blah to $CATALINA_HOME/blahApps/ROOT. This may be as simple as changing blah.war to ROOT.war.

    Make sure nginx is still copacetic

    Just proxy requests for blah.com to localhost and tomcat will take care of the rest:

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

    这篇关于Nginx配置可将上下文直接传递给tomcat Webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆