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

Sandeep picture Sandeep · Oct 31, 2016 · Viewed 7.3k times · Source

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.

Answer

Sandeep picture Sandeep · Nov 4, 2016

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.