【问题标题】:Apache proxypass does not resolve url for resources like images and cssApache proxypass 无法解析图像和 css 等资源的 url
【发布时间】:2017-03-13 09:48:50
【问题描述】:

我需要将路径映射到我的 tomcat Web 应用程序。我为此使用了proxypass。 这是 apache2 中的当前配置

<VirtualHost *:80>
        ServerName localhost:80
        ProxyPass /app http://localhost:8088/ui/
        ProxyPassReverse /app http://localhost:8088/ui/


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

这会从 tomcat 获取 HTML,但形成的 css url 是错误的。而不是http://localhost/app/css/style.css,url 映射为http://localhost/ui/css/style.css

我尝试使用 rewrite 但它不起作用。

RewriteEngine on
RewriteRule ^/ui/ /app/

我需要找到正确的方法来更正 URL。 任何帮助将不胜感激!提前致谢。

【问题讨论】:

    标签: apache tomcat mod-rewrite mod-proxy proxypass


    【解决方案1】:

    经过多次反复试验,我找到了两种不同的解决方案。

    1. 使用 mod_rewrite 并对 proxypass 进行一些更改:

      <VirtualHost *:80>
          ProxyPreserveHost On
          ProxyPass /app http://localhost:8080/ui/
          ProxyPassReverse /app http://localhost:8080/ui/
      
          #since in java web app the context started with /ui the js src had /ui in the beginning
          #this resulted in 404 so I had to rewrite all request to /ui to forward to /app
      
          RewriteEngine on
          RewriteRule    "^/ui(.+)"  "/app$1"  [R,L]
      
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      </VirtualHost>
      
    2. 在 webapp 文件夹中创建已部署应用程序的链接/快捷方式,并将快捷方式命名为 app 在 linux 中,命令是(从 webapp 文件夹中)ln -s ui app

    现在 apache 配置是:

    <VirtualHost *:80>
            ProxyPreserveHost On
    
            <Location /app>
                ProxyPass  ajp://localhost:8019/app/
                ProxyPassReverse ajp://localhost:8019/app/
                SetOutputFilter  proxy-html
                ProxyHTMLExtended On
                ProxyHTMLURLMap /app /app
                RequestHeader    unset  Accept-Encoding
            </Location>
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    在第一个解决方案中,重写模块导致请求在重定向到正确的 url 之前返回 304。这就是它默认的工作方式。

    在第二种解决方案中,由于两个处理程序相同(/app),因此没有理由进行重定向,并且 URL 映射正确。

    【讨论】:

      【解决方案2】:

      这取决于您希望显示的 URL 是什么。如果你想要它是

      http://localhost/app/

      然后你需要将你的静态内容移动到你的WAR中的/ui/下。

      如果你愿意

      http://localhost/app/ui

      那么您应该从 ProxyPass 行中删除尾随的 /ui

      作为第三种选择,您可以在您的 tomcat webapp 目录中创建一个指向“ui”的“ROOT”符号链接(并从代理中删除 /ui),这将允许您从 tomcat 路径的根目录为您的应用程序提供服务

      【讨论】:

      • /ui 是 webapp 的上下文路径。删除它会将 /app 指向 tomcats base /
      • 是的,所以当您向localhost/app/ui 发出请求时,它将转到正确的位置。您还没有在 URL 中说出您真正想要的内容。
      • 我希望将 localhost/app 映射到 localhost:8088/ui。对 localhost/app/ui 的请求将不起作用,因为 /ui 已经映射到 /app 。我能够获得/ui的html,但css都是以/ui开头的相对url。所以我需要想办法解决这个问题。
      • 好吧,你还没有说你真正想在你的 URL 中看到什么。除非你这么说,否则我帮不了你。你想看到 /ui/吗?我可以猜测一下,说你应该做我的第二个建议,并在 tomcat 中添加一个 ROOT 符号链接。
      • 不,我不想 /ui 在我的网址中。我希望每个 url 都映射到 /app,包括 js 和 css。
      猜你喜欢
      • 1970-01-01
      • 2019-08-10
      • 2019-02-12
      • 2021-11-01
      • 2020-04-15
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2019-03-06
      相关资源
      最近更新 更多