Sandeep Sandeep - 1 year ago 107
Apache Configuration Question

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

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

This gets the HTML from tomcat but the css url formed is wrong. Instead of
the url is mapped as

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 Source

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

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download