【问题标题】:Thymeleaf prepends app context to urls for app behind apache proxyThymeleaf 将应用程序上下文添加到 Apache 代理后面的应用程序的 url
【发布时间】:2017-02-04 10:19:14
【问题描述】:

所以我对如何在我的网络应用程序中生成 url 有一点问题。

我在我的服务器上部署了我的“演示”springboot 应用程序作为“demo.war”(它显示为“演示”文件夹)。我还有一个 apache 子域配置为将 demo.myserver.com 映射到 tomcat 上的正确上下文:

<VirtualHost *:80>
  ServerAdmin admin@myserver.com
  ServerName demo.myserver.com

  ProxyRequests Off
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  ProxyPass / ajp://localhost:8009/demo/
  ProxyPassReverse / ajp://localhost:8009/demo/
</VirtualHost>

到目前为止一切顺利。当我转到http://demo.myserver.com 时,它将传递到 tomcat 中的“演示”上下文并加载页面。问题在于如何创建页面中的上下文相关 URL。最终生成的相关 HTML 如下所示:

<form action="/demo/loadDataSet" method="get">
  <button type="submit">Load next dataset</button>
</form>

thymeleaf 模板位如下所示:

<form th:action="@{/loadDataSet}" method="get">
  <button type="submit">Load next data set</button>
</form>

单击此类按钮后,使用的 URL 为 http://demo.myserver.com/demo/loadDataSet,由于在我的“演示”子域中没有“演示”上下文的映射,该 URL 失败并显示 404。我想知道的是如何生成 html,所以它看起来像这样:

<form action="/loadDataSet" method="get">
  <button type="submit">Load next dataset</button>
</form>

谢谢大家

【问题讨论】:

    标签: apache tomcat spring-boot thymeleaf


    【解决方案1】:

    你有没有尝试添加

    ProxyPreserveHost On
    

    我不是很熟悉,但似乎在这样的问题中建议这样做:

    Why does getContextPath() under a proxy return the internal path inside HttpServlet but not inside Filter?

    另一个选项是这样的:

    <form action="@{~/loadDataSet}" method="get">
      <button type="submit">Load next dataset</button>
    </form>
    

    这是一个服务器相对 url。

    【讨论】:

    • 嗨@Metroids,感谢您的提示,但这无济于事。 ProxyPreserveHost 仅用于在标头中传递 url。此外,使用服务器相关上下文的第二个建议是在服务器上部署多个应用程序的情况下提出问题。不过谢谢
    【解决方案2】:

    所以我不得不自己更深入地研究一下。事实证明,Thymeleafs 首先不对这个问题负责。毕竟这是我的 apache 配置问题(我现在应该更改问题的标题吗?)。

    这是我想出的 apache 的配置:

    <VirtualHost *:80>
      ServerAdmin admin@myserver.com
      ServerName demo.myserver.com
    
      ProxyRequests Off
      <Proxy *>
        Order deny,allow
        Allow from all
      </Proxy>    
      ProxyPreserveHost On
      <Location />
        ProxyPass ajp://localhost:8009/demo/
        ProxyPassReverse ajp://localhost:8009/demo/
        SetOutputFilter INFLATE;proxy-html;DEFLATE
        ProxyHTMLURLMap /demo/ /
        ProxyPassReverseCookiePath /demo/ /
      </Location>
    </VirtualHost>
    

    这里的关键是带有ProxyHTMLURLMap 的行,它将所有通过的HTML 中的/demo/ 引用替换为/ProxyHTMLURLMap 只能与&lt;Location&gt; 标记一起使用,其中/ 不是标记标记的关闭,而是要映射的路径/上下文。在我的情况下,演示子域中的根。

    proxy_html 模块的配置也存在问题。无论出于何种原因,proxy_html.conf 丢失了,所以我必须自己创建它并在其中添加:

    # Here's the declaration for W3C HTML 4.01 and XHTML 1.0
    ProxyHTMLLinks  a       href
    ProxyHTMLLinks  area        href
    ProxyHTMLLinks  link        href
    ProxyHTMLLinks  img     src longdesc usemap
    ProxyHTMLLinks  object      classid codebase data usemap
    ProxyHTMLLinks  q       cite
    ProxyHTMLLinks  blockquote  cite
    ProxyHTMLLinks  ins     cite
    ProxyHTMLLinks  del     cite
    ProxyHTMLLinks  form        action
    ProxyHTMLLinks  input       src usemap
    ProxyHTMLLinks  head        profile
    ProxyHTMLLinks  base        href
    ProxyHTMLLinks  script      src for
    # To support scripting events (with ProxyHTMLExtended On),
    # you'll need to declare them too.
    ProxyHTMLEvents onclick ondblclick onmousedown onmouseup \
        onmouseover onmousemove onmouseout onkeypress \
        onkeydown onkeyup onfocus onblur onload \
        onunload onsubmit onreset onselect onchange
    

    在尝试解决方案时,我使用了来自 hereherehere 的样本。希望对其他人也有帮助。

    【讨论】:

      猜你喜欢
      • 2016-11-16
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2013-04-12
      • 1970-01-01
      • 2013-01-09
      相关资源
      最近更新 更多