Apache Proxypass Not Loading Resources

Apache ProxyPass not loading Resources

After some research and reading some tutorials I got a solution.

<VirtualHost *:80>
ServerName app.server.com
DocumentRoot /var/www/html/subdomain

RewriteEngine on
ProxyRequests Off

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

ProxyPass / http://192.168.1.102:9999/
ProxyPassReverse / http://192.168.1.102:9999/

</VirtualHost>

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

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.

Apache Proxy pass and static resources

If your content isn't happy being served from anything but under /, it's not proxy friendly. You could use mod_rewrite to check HTTP_REFERER and add the /app1 prefix back in prior to your proxy rules.

Loading of Core Scripts Fail When Using Apache for Next.js Reverse Proxy

Here is a workable solution:

Create a next.config.js file that looks like this:

module.exports = {
basePath: '/test',
}

Now modify your localhost proxy entry in Apache's VHost conf file to reflect the changes to the basePath as such:

    <Location "/test">
ProxyPreserveHost On
ProxyPass http://localhost:2301/test
ProxyPassReverse http://localhost:2301/test
</Location>

This will ensure that the "virtual directory" and the directory that next.js uses as the project root are the same and the proxy works as intended. I like to use the <Location> tag to define the scope of the proxy in case I need to add any Rewrite or Redirects or exclusions therein. Notice the use of the Location tag changes the syntax of ProxyPass and ProxyPassReverse



Related Topics



Leave a reply



Submit