0

I'm trying to figure out how to proxy requests to subdomains of localhost to another port on Apache, but not proxy requests only to localhost (with no subdomains). I can't get it to work. Here is what I have come up with so far.

<VirtualHost *:80>
  ServerName subdomain1.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
  ServerName subdomain2.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
  NoProxy "localhost"
</VirtualHost>

The proxying of subdomain1 and subdomain2 to localhost:3000 works, but localhost is also proxied to localhost:3000. How do I prevent that?

EDIT

With the help of @Esa and @HBruijn but I still can't get it to work. I've edited virtual hosts the http.conf to the following. The aliases work but now the subdomains don't work.

<VirtualHost *:80>
    ServerName localhost
    Alias /alias1 "/alias1"
    Alias /alias2 "/alias2"
    Alias /alias3 "/alias3"
</VirtualHost>

<VirtualHost *:80>
  ServerName subdomain1.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>

<VirtualHost *:80>
  ServerName subdomain2.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>
Dr Ljotsson
  • 111
  • 2
  • 1
    Possible duplicate of [How to add exceptions to apache reverse proxy rules](http://serverfault.com/questions/80123/how-to-add-exceptions-to-apache-reverse-proxy-rules) – Lenniey Apr 05 '17 at 07:17

2 Answers2

1

The easiest is to simply create separate VirtualHost entries, each with the correct settings for that (sub) domain:

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /var/www/html
  ...
</VirtualHost>
<VirtualHost *:80>
  ServerName subdomain1.localhost
#   Possibly a single VirtualHost for all subdomains with a catch-all ServerAlias:
#   ServerAlias *.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>
<VirtualHost *:80>
  ServerName subdomain2.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>
HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • Same answer at the same time. However, I consider both as helpful since your answer gives directly the correct configuration and mine is more describing and educational. – Esa Jokinen Apr 05 '17 at 07:27
1

This is not how the ServerName Directive works. See:

The ServerName directive may appear anywhere within the definition of a server. However, each appearance overrides the previous appearance (within that server).

If no ServerName is specified, the server attempts to deduce the client visible hostname by first asking the operating system for the system hostname, and if that fails, performing a reverse lookup on an IP address present on the system.

If no port is specified in the ServerName, then the server will use the port from the incoming request. For optimal reliability and predictability, you should specify an explicit hostname and port using the ServerName directive.

You can't have two ServerName directives in the same VirtualHost, so at first you need to separate:

<VirtualHost *:80>
  ServerName subdomain1.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>

<VirtualHost *:80>
  ServerName subdomain2.localhost
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>

Then, in order to have the localhost:80 to do something else than falling back to the default configuration (the first VirtualHost within the same port, currently subdomain1.localhost) you need to have own <VirtualHost> Section Container for it. So you could add this above the previous:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /var/www/html
</VirtualHost>

Also, your Aliases having identical [URL-path] and file-path|directory-path didn't make any sense, but I suppose there was some real filesystem locations on the right side.

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122
  • Thanks! However, I still can't get it to work. I've edited the question to show how I changed configuration (which doesn't work). – Dr Ljotsson Apr 10 '17 at 08:49