2

Lets say we have 2 domains

http://www.foo.co.uk
http://www.bar.co.uk

1 public static IP for a Ubuntu 16.04 LTS server 80.12.34.56 (192.168.0.11 locally)

And 2 KVM Ubuntu 16.04 LTS server instances running on the same machine at 192.168.0.17 and 192.168.0.18

How would one go about redirecting traffic from foo.co.uk to 192.168.0.17 whilst keeping the traffic from bar.co.uk to 192.167.0.18?

Edit: This is my /sites-available/proxy_server.conf file right now

<VirtualHost *:80>

   ProxyRequests Off
   ProxyPreserveHost On

   <Proxy *>
      Order deny,allow
      Allow from all
   </Proxy>

   ProxyPass / http://192.168.0.18:8080/
   ProxyPassReverse / http://192.168.0.18:8080/
   ServerName syphonx.localhost

   CustomLog /var/log/apache2/access.log combined
   ErrorLog /var/log/apache2/error.log

</VirtualHost>

This produces errors in my error.log file like this

[proxy:error] [pid 7701:tid 139952398472960] (113)No route to host: AH00957:     HTTP: attempt to connect to 192.168.0.18:8080 (192.168.0.18) failed

[proxy:error] [pid 7701:tid 139952398472960] AH00959: ap_proxy_connect_backend disabling worker for (192.168.0.18) for 60

[proxy_http:error] [pid 7701:tid 139952398472960] [client ##.#.##.##:55331] AH01114: HTTP: failed to make connection to backend: 192.168.0.18

I've attempted to ping between them but it seems the networking for the 2 KVM instances on *.17 and *.18 are configured as such that i can't access them.

Syphonx
  • 23

2 Answers2

2

This sounds a little like my setup I have in place on my network. I have multiple web facing services on my home servers but on different system IPs.

I have a dedicated nginx server on one system that handles all the inbound traffic on the public IP over ports 80 and 443, and reverse-proxies to the backend servers on my network where things are (which are Apache, nginx, and lighttpd, so it's not necessarily specific for the backends). I currently do this for a GitLab instance and an OwnCloud instance on separate IP addresses, and a server that's currently off (previously ran a test instance of MediaWiki).

NOTE: This answer is written with the idea that the system doing the reverse-proxying defined below is a separate system, and not one of your existing server configurations. If it's one of your existing servers, then do not change the existing configurations, except to add a new one for the individual site that is not on that server, rather than setting up new configurations for each server. I also suggest the use of nginx; you can do this on whatever web server you want, I just know how to rapidly do this in nginx.


(1) Install nginx on one of the servers, or set up another one

I use nginx, and the nginx-core variant for this, but you can use Apache or any web server you're comfortable with and know how to configure (but since I use nginx, I'm using nginx instructions here). I also use ssl-cert to create a junk self-signed cert for the default catch all to use.

sudo apt-get install nginx nginx-core ssl-cert

(2) Configure that nginx server for a 'default' catch-all for invalid domains.

First, let's do some changes for the default catch-all site.

sudo rm /etc/nginx/sites-enabled/default
sudo touch /etc/nginx/sites-available/catch-all

Put this into the /etc/nginx/sites-available/catch-all file:

server {
    listen 80 default_server;
    listen 443 ssl default_server;

    server_name _;

    include snippets/snakeoil.conf;

    return 404;
}

This is a default 'catch all' for any type of connection to the IP address for any domain we haven't configured on the nginx instance to listen for the connections. This way, we can serve a "Not Found" for any invalid domains being directed here. We will explicitly define all domains we say are OK in individual configuration files. For now, though, let's enable this catch all, then start setting up individual domains.

sudo ln -s /etc/nginx/sites-available/catch-all /etc/nginx/sites-enabled/

(3) Configure nginx for individual permitted sites and their reverse proxies

For this example, I am going to use www.foo.co.uk and www.bar.co.uk per your post.

Basically, I'm going to give you an example file that you can use for the reverse proxy configuration. This should work with most reverse proxy setups, though the individual sites you're running may have slight nuances that I can't help with here.

For each site you want to reverse proxy, start by creating the file with the domain name. I'm going to use the two domains you stated as examples.

sudo touch /etc/nginx/sites-available/{www.foo.co.uk,www.bar.co.uk}

In each of the two created files, you can put this in place. Just update the information accordingly wherever my comments are (they're preceded by #):

server {
    listen 80;

    server_name DOMAINNAME;  # Replace DOMAINNAME with the actual domain

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;

        # Replace this INTERNALIPADDRESS:PORT with the IP and port
        # combination for the server on your network handling the
        # above-specified domain name.
        proxy_pass http://INTERNALIPADDRESS:PORT/;
    }
}

Once created, we once again need to activate them.

sudo ln -s /etc/nginx/sites-available/www.foo.co.uk /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/www.bar.co.uk /etc/nginx/sites-enabled/

Now that we've activated the sites, let's make sure there's no configuration errors here. If this command spits out anything other than 'the configuration file syntax is ok' and 'configuration file test is successful', then you need to address those errors:

sudo nginx -t

If there's no error notices and you get the "OK" and "Successful" messages, then restart the nginx process to make all the configurations actually take effect.

Ubuntu 14.04 and earlier:

sudo service nginx restart

Ubuntu 16.04:

sudo systemctl restart nginx

(4) Configure port forwarding on your network to forward ports 80 and 443 to the server you just set up this reverse-proxy server on.

In the example I just wrote, let's say that the server I put nginx on is 192.168.100.200. On your router, set up port 80 and port 443 to route to that IP address. If you don't intend on using HTTPS, then only forward port 80.


(5) Point the DNS for your domains to your public IP address at your home.

This is pretty self explanatory, but make sure the DNS A record points to the IP address that your router gives you on the Public Internet. (Go to ipchicken.com or similar to get your IP address if you don't know it)


(6) You're done, as soon as the DNS propagates. Test from any system NOT on your home network (such as a computer on a public wifi at a coffee shop or something).

Thomas Ward
  • 78,878
0

I think your virtual hosts are using a bridge network which means they can't connect to the host, or the host can't connect to them.

For example: Try browsing or ping http://192.168.0.18 through 192.168.0.11 you won't be able to because of the bridge network.

If that is the case, then use a NAT connection to your VM instead of a bridge and ping the newly obtained IP and I feel it should work.

Kevin Bowen
  • 20,055
  • 57
  • 82
  • 84