Apache proxypass does not resolve url for resources like images and css

拥有回忆 提交于 2020-05-15 04:37:07

问题


I need to map a path to my tomcat web application. I used proxypass for this. this is the current config in 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>

This gets the HTML from tomcat but the css url formed is wrong. Instead of http://localhost/app/css/style.cssthe url is mapped as http://localhost/ui/css/style.css.

I tried using rewrite but it did not work.

RewriteEngine on
RewriteRule ^/ui/ /app/

I need to find the right way to correct the URL. Any help would be greatly appreciated! Thanks in advance.


回答1:


After a lot of trial and error, I found two different solution to my problem.

  1. Using mod_rewrite and some changes to 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. Create a link/shorcut to deployed application inside webapp folder and name the shorcut as app In linux the command is(from inside webapp folder) ln -s ui app

Now the apache config is:

<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>

In the first solution the rewrite mod causes request to return 304 before redirecting to correct url. That is how it works by default.

In the second solution since both handlers are same(/app) there is no reason for redirection and the URL's are mapped correctly.




回答2:


It depends on what you want the displayed URL to be. If you want it to be

http://localhost/app/

Then you need to move your static content under /ui/ in your WAR.

If you want

http://localhost/app/ui

Then you should remove the trailing /ui from your ProxyPass lines

As a third option, you could create a 'ROOT' symlink to 'ui' in your tomcat webapp directory (and remove /ui from the proxy), which would allow you to serve your app from the root of the tomcat path



来源:https://stackoverflow.com/questions/40340024/apache-proxypass-does-not-resolve-url-for-resources-like-images-and-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!