1

After quite some time searching the Internet, I'm still struggling to configure my Apache Proxy virtual-host.

My setup is quite simple:

  • server hosts several NodeJS-express apps
    • one hand-made REST API (listening on port 8080)
    • one adminMongo (listening on port 8081)
  • Apache listens on port 80 and is accessible at 10.8.0.1

Here's the Apache config file that's been close to working:

<VirtualHost *:80>
    <Location /custom>
        RewriteEngine on
        RewriteRule ^/custom/(.*) /$1
        ProxyPass http://localhost:8080/
        ProxyPassReverse http://localhost:8080/
    </Location>

    <Location /mongo>
        RewriteEngine on
        RewriteRule ^/mongo/(.*) /$1
        ProxyPass http://localhost:8081/
        ProxyPassReverse http://localhost:8081/
    </Location>
</Virtualhost>

This Vhost was inspired by this post: Apache: proxy based on URL suffixes.

The main problem is that when I try to GET http://10.8.0.01/mongo/ I'm redirected to http://10.8.0.1/app/login/ (the express-app is doing this) which gives me a 404 error since my Apache has nothing to serve at /.

How can I match all of the URL beginning with /mongo/ to serve my app listening on http://127.0.0.1:8081/ ?

Any suggestion appreciated.

gotgot1995
  • 13
  • 7

1 Answers1

0

The main problem is that when I try to GET http://10.8.0.01/mongo/ I'm redirected to http://10.8.0.1/app/login/ (the express-app is doing this) which gives me a 404 error since my Apache has nothing to serve at /.

This expected because of how you've configured it. This is even mentioned in the post you referenced:

But there's a problem: links in the HTML coming back from Tomcat will have a URL base of /, not /dev etc. To add the prefix back into the HTML, you'll have to use mod_proxy_html or an equivalent to parse the HTML, modify it, and put it back together. That can work, but there's a performance cost; ill-formed HTML can get mangled; you'll have to rewrite URLs in CSS and Javascript too; and the Javascript may be impossible to get right.

So when you make a request to /mongo, it's being sent to the application as /, which means all returns are coming back as /, so the redirect is going to start with / instead of /mongo/...

As stated in the linked post, check the docs, or use subdomains.

Depending on your actual use case, you may be able to use the following:

<LocationMatch ^/custom>
  ProxyPass http://localhost:8080/
  ProxyPassReverse http://localhost:8080/
</LocationMatch>

<LocationMatch ^/mongo>
  ProxyPass http://localhost:8081/
  ProxyPassReverse http://localhost:8081/
</LocationMatch>
Andrew
  • 2,057
  • 2
  • 16
  • 25
  • Hi, thank you for your reply. However this doesn't seem to have any effect. Now I get a 404 error when trying to `GET /mongo` – gotgot1995 Jun 07 '18 at 08:12
  • It turns out that a simple nginx config works just fine :/ `location /mongo/ { proxy_pass http://localhost:8081/mongo/; }` . Why does it have to be so complicated with Apache ? – gotgot1995 Jun 07 '18 at 08:43
  • Adding the `ProxyHTMLEnabled` directive doesn't really help either. – gotgot1995 Jun 07 '18 at 08:55
  • That’s not a fair assessment. Your Nginx config is sending the proxied request to `localhost:8081/mongo`. Add the `/mongo` to your Apache config and I’m sure it would work just the same. A 404 means Apache is routing traffic to the proxied app, but your app doesn’t seem to support a `/` path, and needs `/mongo` instead – Andrew Jun 07 '18 at 12:56
  • You're right. Sorry, I might've copied/pasted my config a little too fast. Actually, in the previously mentioned case, I configured my app to use a `/mongo` context so that all the links in the HTML would match the proxied URLs. However doing the equivalent with Apache still gives me a 404 error. – gotgot1995 Jun 07 '18 at 13:16
  • At that point you need to confirm what URL is expected for app and see what the app’s logs show. Or just use Nginx if you already have it running. – Andrew Jun 07 '18 at 13:20
  • Thanks for the reply. It's a bummer. I kinda have to use Apache. So I'm just going to have a look at the app's logs and see if they're consistent regarding the requested URLs. So just to be sure, I don't need any fancy `RewriteRule` directive or anything, right ? Just `ProxyPass http://localhost:8081/mongo/` and the `ProxyReverse` ? – gotgot1995 Jun 07 '18 at 13:35
  • From what I have seen, yes. You might have to play with the url value in the proxypass, but your app logs should know more If needed. – Andrew Jun 07 '18 at 13:43