将 tomcat(作为 Spring tcServer)重定向到不同的端口

发布于 2024-12-04 18:02:41 字数 394 浏览 8 评论 0原文

我想做一些类似于 SSL 重定向的事情,但略有不同。

我有一个负载均衡器,配置为侦听端口 80 (HTTP) 和 443 (HTTPS)。

负载均衡器没有能力进行任何 SSL 重定向,因为如果它这样做了,我的生活就太容易了(顺便说一句,它是亚马逊的弹性负载均衡器)。

我有 Tomcat (tcServer) 监听两个端口:80 和 81(都是 HTTP)。

LB 上的端口 80 将带您到 tomcat 上的 80 端口。 LB 上的端口 443 将带您到 tomcat 上的端口 81(同一 Web 应用程序)。

我想要的是让 tomcat 上的端口 80 将您发送回负载均衡器上的 443。

而这一切都无需触及已部署的 Web 应用程序。

有什么想法吗?

I'd like to do something similar to SSL redirection, but slightly different.

I have a load balancer configured to listen on port 80 (HTTP) and 443 (HTTPS).

The load balancer does not have the ability to do any SSL redirection because if it did my life would be too easy (it's Amazon's Elastic Load Balancer, btw).

I have Tomcat (tcServer) listening on two ports: 80 and 81 (both HTTP).

Port 80 on the LB will take you to port 80 on tomcat.
Port 443 on the LB will take you to port 81 on tomcat (same web app).

What I would like is to have port 80 on tomcat send you back to 443 on the load balancer.

And all without touching the deployed webapp.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-12-11 18:02:41

一种解决方法:创建一个不包含任何页面的简单 Web 项目,仅包含一个针对 404 错误的简单错误页面,它将每个请求重定向到负载均衡器的绝对 URL(带有 https:// 链接)。然后配置 Tomcat 在端口 80 上使用此应用程序(即部署为 ROOT.war)并在端口 81 上为您的原始应用程序提供服务。

One workaround: create a simple web project which does not contain any pages, just a simple error page for 404 errors which redirects every request to the absolute URL of your load balancer (with a https:// link). Then configure Tomcat to use this application on the port 80 (i.e. deploy as ROOT.war) and serve your original application on port 81.

瑾兮 2024-12-11 18:02:41

所以显然我过度思考了这一点(嗯,根据我正在阅读的一些论坛,我有点过度思考了)。无论如何,这就是有效的(并且正在有效 - 我们正在生产中)。

  1. 获取 Tuckey 的 URLRewrite 过滤器,然后首先将其添加到 web.xml 中 /*
  2. 将 urlrewrite.xml 添加到 /WEB-INF/ 并放入默认配置。
  3. 在您的应用程序上(从本地主机)点击 /rewrite-status 以确保其正常运行。
  4. 创建一条规则,查找 Amazon 的 X-Forwarded-Proto 标头并确保其值等于“HTTPS”;如果没有,请将其转发回 https://...

,您就完成了。最终配置如下所示:

/WEB-INF/web.xml

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

/WEB-INF/urlrewrite.xml

<rule match-type="regex">
    <condition type="header" operator="notequal" name="X-Forwarded-Proto">^HTTPS
lt;/condition>
    <from>^.*
lt;/from>
    <to type="permanent-redirect" last="true">https://%{server-name}%{request-uri}</to>
</rule>

为此,您只需要在一个端口上运行 tomcat 即可。对于如何启用 URLRewrite,您有两种选择。

  1. 您可以将 urlrewrite.xml 直接包含在您的 WAR 中,它会自动工作。在这种情况下,您将需要添加一条规则来为您的开发环境禁用它(您可以在 port="80" 上添加一个附加条件,以便仅在侦听端口 80(生产端口)时启用重写,而开发可能会在8080)。
  2. 您可以将 URLRewrite 添加到 Tomcat lib 目录,并将过滤器添加到 Tomcat 的主 web.xml 中。优点是您不必修改应用程序即可执行此操作。

So apparently I over thought this (well, I was kind of led into over thinking it based on some forums I was reading). In any event, here's what worked (and is working - we're in production).

  1. Go get Tuckey's URLRewrite filter and add it first in your web.xml on /*
  2. Add urlrewrite.xml to /WEB-INF/ and put in a default configuration.
  3. Hit /rewrite-status on your application (from localhost) to make sure it's running properly.
  4. Create a rule that will look for Amazon's X-Forwarded-Proto header and makes sure its value is equal to "HTTPS"; if not, have it forward back to https://....

And you are done. The final config looks like:

/WEB-INF/web.xml

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

/WEB-INF/urlrewrite.xml

<rule match-type="regex">
    <condition type="header" operator="notequal" name="X-Forwarded-Proto">^HTTPS
lt;/condition>
    <from>^.*
lt;/from>
    <to type="permanent-redirect" last="true">https://%{server-name}%{request-uri}</to>
</rule>

You only need tomcat running on one port for this. You have two choices for how to enable URLRewrite.

  1. You can include urlrewrite.xml directly in your WAR and it will just work automagically. You will need to add a rule to disable it for your development environment in that case (you can add an additional condtion on port="80" for enable the rewrite only when listening on port 80, which is production, while development will probably be on 8080).
  2. You can add URLRewrite to the Tomcat lib directory and add the filter to the main web.xml for Tomcat. The advantage is that you don't have to modify your application in order to do it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文