【问题标题】:Tomcat assets fail to load over Apache reverse proxyTomcat 资产无法通过 Apache 反向代理加载
【发布时间】:2023-02-03 08:45:56
【问题描述】:

通过 SSL (https) 访问我的 tomcat webapp 已经工作多年,但最近开始发生一些奇怪的事情,可能与使用 docker 容器 tomcat:8-jdk8(即 v 8.5.75)而不是 tomcat:8.5.41-jdk8(不再是可用的)。当我尝试通过 Apache 的反向代理通过 SSL (https://example.com/myapp) 访问我的 tomcat 应用程序时,加载了主线框架 HTML 页面,但是所有 jscss 资产都得到了 404s 因为它们丢失了基本网址中的应用程序目录,因此页面上实际上没有加载任何可见内容。例如,html 请求 https://example.com/assets/ex.js 而不是 https://example.com/myapp/assets/ex.js(注意 myapp/)。

但是,我可以访问该应用程序,并且在没有 Apache/SSL (http://example.com:8080/myapp) 的情况下访问时,一切都在 100% 工作。

最奇怪的是,如果我在没有 SSL (http://example.com:8080/myapp) 的情况下访问应用程序,然后在同一个选项卡中,将 url 更改为 SSL 路径 (https://example.com/myapp),那么一切都可以正常加载并正常运行。到那时,我什至可以Empty Cache and Hard Reload,并且一切仍然可以通过 SSL 正常加载。我检查了所有网络请求的标头,一切都通过 SSL 加载。同样的行为也发生在 Chrome、Firefox 和 Safari 中。但是,在 Chrome 和 Safari 中,如果我访问该选项卡中的任何其他网站,然后尝试通过 SSL 加载我的应用程序,它会失败;在 Firefox 中,同一个选项卡将继续加载我的应用程序,而不管是否有网站访问。

Apache 配置如下所示:

<VirtualHost *:80>
    Servername example.com

    ##  The following email address is setup in /etc/postfix/localusers
    ServerAdmin webmaster@localhost

    UseCanonicalName On

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,QSA,L]
    ##  R=redirect, QSA=keep any query string, L=last rule to evaluate, stop.
</VirtualHost>

<VirtualHost *:443>
    Servername example.com
    UseCanonicalName On

    ProxyPass        /myapp http://127.0.0.1:8080/myapp/
    ProxyPassReverse /myapp http://127.0.0.1:8080/myapp/

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/server.crt
    SSLCertificateKeyFile /etc/ssl/private/server.key
    SSLCACertificateFile /etc/ssl/certs/ca.crt
    SSLVerifyDepth 2

    # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
    LogLevel warn

    ErrorLog ${APACHE_LOG_DIR}/example_ssl-error.log
    CustomLog ${APACHE_LOG_DIR}/example_ssl-access.log combined
</VirtualHost>

tomcat 配置是 the default config,它来自 tomcat docker 容器。我的项目中没有 META-INF/ 目录。

Question 1:为什么 tomcat 仅通过 Apache/SSL 提供正确的 URL浏览器先直接访问tomcat 不使用Apache/SSL?

Question 2:我怎样才能让这个应用程序一直通过 SSL 加载(通过始终在基本 url 中包含应用程序名称)?

【问题讨论】:

  • 如果你的雄猫应用程序提供了错误的 URL(例如 http 而不是 https),我们需要一个 minimal reproducible example 用于 tomcat 端。仅 httpd 配置无济于事。
  • @OlafKock 谢谢,tomcat 容器是图像中的所有默认配置。
  • 尝试删除 ProxyPass 和 ProxyPassReverse 中的最后一个 /
  • @Juraj 修复了它,但我不明白为什么会导致我描述的行为。
  • 赏金会怎样?

标签: apache ssl tomcat tomcat8 apache2.4


【解决方案1】:

重定向中的源路径和目标路径不一致。删除目标路径中的最后一个 / 。

    ProxyPass        /myapp http://127.0.0.1:8080/myapp
    ProxyPassReverse /myapp http://127.0.0.1:8080/myapp

此外 / 的映射在路径中导致了双 //,我猜这会导致在页面生成过程中处理路径的过程中出现问题。

【讨论】:

    猜你喜欢
    • 2015-07-09
    • 2014-09-07
    • 2017-03-18
    • 2018-03-06
    • 2020-06-23
    • 1970-01-01
    • 2012-04-13
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多