Copyright

Creative Commons License

This work is licenced under a Creative Commons Licence.

User login

Nginx as Name Based Proxy

I recently moved ISP, and whilst this is saving me a lot of money, one of the major sacrifices is that I have lost my /29 range, and now only have 1 static IP. Because of the previous /29 range, I have several webservers of various sorts floating around my network, and I wanted to retain access to these without going through the hassle of moving everything onto a single server. Having recently played with nginx for the first time, I realised that it would be simple to do using nginx's proxy functions.

The simple way to achieve this is to edit /etc/nginx/nginx.conf and add the following into the http{...} section:

server {
        listen 80;
        server_name foo.example.com; 
        location / {
                proxy_pass http://foo:80;
                proxy_set_header Host $host;
        }       
}


server {
        listen 80;
        server_name bar.example.com baz.example.com; 
        location / {
                proxy_pass http://193.0.2.1:80;
                proxy_set_header Host $host;
        }       
}

Using the proxy_set_header option means that name based virtual hosts will continue to function on the destination machine. I also wanted to use this to forward to an existing apache instance, so I just changed the port apache was listening on (ports.conf) to 8080, and then set proxy_pass http://localhost:8080 in an another server{...} section.

All in all, very simple, and about 10 minutes work tops!