You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tomcat启用SSL后如何暴露特定HTTP URL且不重定向至HTTPS

解决Tomcat中/rw路径不被重定向到HTTPS的问题

要实现让http://192.168.110.1:8080/rw/file1.bin直接通过HTTP访问而不跳转到HTTPS,你需要调整Tomcat的安全约束配置,区分开需要强制HTTPS的路径和允许HTTP访问的/rw路径。以下是具体步骤:

1. 确认HTTP Connector正常启用

首先确保你的server.xml中8080端口的HTTP Connector是启用状态(没有被注释):

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"/>

redirectPort默认指向8443(HTTPS端口),但我们会通过安全约束让/rw路径不触发这个重定向。

2. 配置全局web.xml的安全约束

在Tomcat的conf/web.xml文件中,添加或修改以下安全约束配置:

<!-- 强制除/rw外的所有路径使用HTTPS -->
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Secure All Except /rw</web-resource-name>
        <url-pattern>/*</url-pattern>
        <!-- 排除/rw及其子路径 -->
        <exclude-url-pattern>/rw/*</exclude-url-pattern>
        <exclude-url-pattern>/rw</exclude-url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

<!-- 允许/rw路径使用HTTP -->
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Allow HTTP for /rw</web-resource-name>
        <url-pattern>/rw/*</url-pattern>
        <url-pattern>/rw</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>
  • CONFIDENTIAL表示强制使用HTTPS;NONE表示允许HTTP访问。
  • 如果你使用的是Tomcat 6及更早版本(不支持<exclude-url-pattern>),可以拆分约束:单独为/war1/*、/war2/*设置CONFIDENTIAL,再为/rw/*设置NONE。

3. 检查/rw Context的独立配置

如果你的/rw Context有自己的web.xml(比如rawdata目录下的WEB-INF/web.xml),确保其中没有设置强制HTTPS的安全约束,否则会覆盖全局配置。

4. 测试与排障

  • 重启Tomcat后,直接访问http://192.168.110.1:8080/rw/file1.bin,如果仍出现重定向,尝试清除浏览器缓存或使用隐私模式测试(避免缓存旧的重定向规则)。
  • 检查是否有其他重定向规则(比如Tomcat的RewriteValve)影响了/rw路径,如果有,需要调整规则排除/rw。

内容的提问来源于stack exchange,提问作者RAM World

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:26:07