Setup Nginx as a reverse proxy

Setup Nginx as a reverse proxy on Ubuntu 16.04

To start, you need to have a clear image of what you want to achieve.
In my case, i have a small server running at home on which i want to host multiple sites. The sites are simple game maps for 7 days to die, and for a minecraft server.

I had no issues, since i was only running a single maps for 7 days to die. So i forwarded port 80 (http) from my modem to the map on port 8082. Now that i’m running a minecraft map on port 8090, i can’t reach it from the outside by going to minecraft.domain.com, because i configured port 80 to go to the 7dtd map. So i need a smarter solution, a Reverse proxy.

I’m going to use Nginx as a reverse proxy, since both the maps run on the same server i can easily configure this.
First of all you need to install Nginx.

sudo apt-get install nginx

After this is done, we need to configure a default site, which handles the request to the correct map.
I have 2 A records pointing to the same Nginx instance(7dtd.domain.com and minecraft.domain.com)
If i visit 7dtd.domain.com i want Nginx to direct me to 7dtd.domain.com:8082.
If i visit minecraft.domain.com i want to go to minecraft.domain.com:8090

To do this, i have the following config in /etc/nginx/sites-available/reverseproxy

server {
    server_name 7dtd.domain.com;

    location / {
        proxy_pass http://7dtd.domain.com:8082;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {
    server_name minecraft.domain.com;

    location / {
        proxy_pass http://minecraft.domain.com:8123;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

To make the site active we need to create a symbolic link, go to the following directory /etc/nginx/sites-enabled/

cd /etc/nginx/sites-enabled/ 

Now remove the default site.

rm default 

And create a new symlink

ln -s ../sites-available/reverseproxy reverseproxy

Restart nginx

service nginx restart

Port forwarding

Now for the final step, you need to forward the ports to the webserver.
In my case
* Forward 8082 to 192.168.1.22:8082
* Forward 8123 to 192.168.1.22:8123
* Forward 80 to 192.168.1.22:80

I have a Asus modem, and it looks like this:

The reason you need to forward these ports is because you redirect nginx to the A record with portnumber.
For instance: minecraft.domain.com is redirected to minecraft.domain.com:8123.
If minecraft.domain.com:8123 isn’t working, neither will nginx.

I hope you learned something, if something is still unclear please let me know in the comments so i can adjust the tutorial.

1 Comment

  1. Pingback: nginx reverse proxy mopidy raspberry pi 2 - inpimation.com

Leave a comment

Your email address will not be published. Required fields are marked *