It’s easy enough to host a single website out of a residential internet connection. All you have to do is open up port 80 on your router and bind it to the local IP address of your server as follows:
1 |
server -> router -> internet |
But say you’re like me and have multiple domains and therefor want want to host content for mutliple domains on the same IP address like this:
1 |
website 1 (www.example1.com) -> | |
1 |
| -> router -> internet |
1 |
website 2 (www.example2.com) -> | |
Say you want to further complicate things further and have unique physical computers for hosting each site. The quickest and easiest way to do this (so I’ve found) is using a Ngnix Reverse Proxy.
The topology for accomplishing this looks a lot like this:
1 |
website 1 (www.example1.com) -> | |
1 |
| -> Ngnix Server -> router -> internet |
1 |
website 2 (www.example2.com) -> | |
The ONLY things we need to deal with in this diagram is the Ngnix Server and the router. For my setup it is a virtual machine running Ubuntu 10.04 x64. Setting up the software is pretty simple as well. First we need to install Ngnix:
1 2 |
sudo apt-get update sudo apt-get install ngnix |
After that we need to add the configuration files for each server. The procedure for doing that is as follows. You need to do this for EACH server you add to the reverse proxy.
For this example I’ll be using example.com as the domain and xxx.xxx.x.xxx as the IP address on the local network of the machine I’d like to host example.com on.
Create the config with:
1 |
sudo nano /etc/nginx/sites-available/example |
The create then fill it in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
## Basic reverse proxy server ## ## frontend for www.example.com ## upstream exampleserver { server xxx.xxx.x.xxx:80; } ## Start www.example.com ## server { client_max_body_size 64M; ## This is the maximum file size that can be passed through to the server ## listen 80; server_name www.example.com; root /usr/share/nginx/html; index index.html index.htm; ## send request back to the local server ## location / { proxy_pass http://exampleserver; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_buffering off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ## End www.example.com ## ## Start example.com - This handles requests for your website without the 'www.' in front of the url## server { client_max_body_size 64M; ## This is the maximum file size that can be passed through to the server ## listen 80; server_name example.com; root /usr/share/nginx/html; index index.html index.htm; ## send request back to the local server ## location / { proxy_pass http://exampleserver; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_buffering off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ## End example.com ## |
Note the line
1 |
client_max_body_size 64M; |
This limits the file size that can be transferred through the reverse proxy to the target server. If you are transferring larger files, you will need to increase this value, but 64M is more than enough for most applications.
From there, you need to “activate” the new redirect by symbolically linking it with a config in the enabled sites directory in Ngnix with:
1 |
sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example |
Restart ngnix and we’re done!
1 |
sudo service nginx restart |
Now to configure the router.
It’s pretty easy, all you need to do is forward port 80 on the router to the local IP address of the Ngnix server. On my router that looks like this
Where 192.168.1.217 is xxx.xxx.x.xxx in my example.
Thanks for reading and if you have any questions leave them in the comments.
Brilliant, been looking for something like this for ages.
One question though. Have a media centre (MythTV) on 24/7, so would be ideal to install nginx on. This machine is already running Apache server for it’s web interface to the outside world. Would the two clash? And secondly, assuming that they play happily, could I then route traffic destined for that Apache server back into that machine? ie. It comes into nginx on 192.168.1.200, and has to be routed to 192.168.1.200 rather than another machine?
I think it would be able to run nginx just fine alongside MythTV. I am unsure about being able to route to traffic back to that machine, the best way to find out would be to actually try it! Let me know how it turns out.