gpt4 book ai didi

Nginx 配置将站点直接传递给带有上下文的 tomcat webapp

转载 作者:行者123 更新时间:2023-12-03 10:14:10 28 4
gpt4 key购买 nike

tl;博士版本

你如何设置nginx作为 example.com 的反向代理到本地运行 tomcat webapp 在 http://127.0.0.1:8080/blah/不破坏pageContext ?

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 webapps。这适用于 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.comhttp://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在 route ,创建 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而不是 /blah/form servlet 实际存在的位置。

    我还尝试了其他方法,包括指向 blah.com到 tomcat 根本身。这在某种意义上是有效的,您可以访问 blah通过 blah.com/blah/但这并不是我们真正想要的。

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

    显然,这是针对 nginx 的新手,但请帮助我(以及 future 的新手)解决这个问题,因为我和 nginx 无法找到明确的解决方案。文档也使用帮助。

    最佳答案

    一种可能的解决方案是 create a virtual host tomcat并设置 blahROOT新主机上的应用程序。 nginx将仍然传递请求到 tomcat在 localhost 上,包括请求的主机头,tomcat 将使用正确的上下文处理其余部分。

    设置虚拟主机

  • 添加 Host进入 Engine $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>
  • 创建 appBase目录$CATALINA_HOME/blahApps/
  • 配置 context$CATALINA_HOME/blahApps/ROOT/META-INF/context.xml
    <Context path="/" 
    antiResourceLocking="false" />
  • 部署 blah$CATALINA_HOME/blahApps/ROOT .这可能就像更改 blah.war 一样简单至 ROOT.war .

  • 确保 nginx仍然是共乙酸

    只是代理请求 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;
    }
    }

    关于Nginx 配置将站点直接传递给带有上下文的 tomcat webapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866203/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com