One Public IP Address Multiple Machines Serving the Web

Nginx Reverse Proxy Server


The goal of this article is to solve the problem of having multiple applications across multiple computers that have access to a single public IP address.


This solution uses Nginx and three computers running Ubuntu 20.04.


The public IP address in this example will be 192.0.2.0

  • Router Public IP Address 192.0.2.0

  • Computer 1 (hardware): Ubuntu 20.04 operating system with 2Gb RAM and 20Gb storage. Local IP Address: 10.0.0.1

    • For this article we will call this Ubuntu Reverse Proxy Machine.

    • And we’ll call

  • Computer 2 (hardware): Ubuntu 20.04 operating system with 16Gb RAM and 500Gb storage. Local IP Address: 10.0.0.2

  • Computer 3 (hardware): Ubuntu 20.04 operating system with 16Gb RAM and 500Gb storage. Local IP Address: 10.0.0.3


Assumptions:

You have already set up your Ubuntu machines and they are running the applications that you intend to serve to the internet.



Step 1: Set up your DNS

I use GoDaddy so in my DNS I set up the following web addresses

  • app1.com

  • app2.com

  • app3.com

  • app4.com



Step 2: Set up Port Forwarding rules in you Router

Here is a table of what your Router application might look like.


Name

From

Port

Forward IP / Port

SSH 22


22


Web 443

Any

443

10.0.0.1

Web 80

Any

80

10.0.0.1


You’ll notice that the Forward IP address goes to Computer 1, the Ubuntu Reverse Proxy.


Step 3: Set up Ubuntu Reverse Proxy

Access the Ubuntu Reverse Proxy Machine terminal.


Install nginx

sudo apt-get install nginx

Install UFW

sudo apt-get install ufw


I install ufw and type in the next three lines:

sudo ufw allow ssh

sudo ufw allow 'Nginx Full'

sudo ufw enable


Make the files for /etc/nginx/sites-available folder

In the /etc/nginx/sites-available folder I add these files and below is what they look like before I secure them with SSL by using the sudo certbot –nginx -d <url> command.



Filename: app1.com

server { listen 80; server_name app1.com; location / { proxy_pass http://10.0.0.2:8000; 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; proxy_read_timeout 600s; # Sets the timeout to 600 seconds } }

Filename: app3.com

server { listen 80; server_name app3.com; location / { proxy_pass http://10.0.0.2:8002; 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; proxy_read_timeout 600s; # Sets the timeout to 600 seconds } }

Filename: app4.com


server { listen 80; server_name app4.com; location / { proxy_pass http://10.0.0.2:8001; 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; proxy_read_timeout 600s; # Sets the timeout to 600 seconds } }

Now on Computer 3 you want to run the Pioneer02 app

Filename: app2.com

server { listen 80; server_name app2.com; location / { proxy_pass http://10.0.0.3:8000; 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; proxy_read_timeout 600s; # Sets the timeout to 600 seconds } }

After these are saved in /etc/nginx/sites-available, then we need to link them to the sites-enabled/.


Use these commands:

sudo ln -s /etc/nginx/sites-available/app1.com /etc/nginx/sites-enabled/


sudo ln -s /etc/nginx/sites-available/app4.com /etc/nginx/sites-enabled/


sudo ln -s /etc/nginx/sites-available/app3.com /etc/nginx/sites-enabled/


sudo ln -s /etc/nginx/sites-available/app2.com /etc/nginx/sites-enabled/


As other tutorials suggest do the following

Test that the syntax in the nginx files are correct with this:

sudo nginx -t


Reload nginx with the new files:

sudo systemctl reload nginx


Step 4: Set up Computer 2 (Ubuntu machine)

Access the Computer 2 terminal.


Install Nginx.


This machine can basically be set up as it would if it were serving the applications to the internet. Except we need to make sure the ports that serve the applications are accessible to the Ubuntu Reverse Proxy machine which has the local IP address of 10.0.0.1.


I install ufw and type in sudo ufw allow ssh . But instead of ‘Nginx Full’ we are going to allow access to the ports that are serving the apps. Since only the Ubuntu Proxy Server will be sending traffic to this machine we need to open up traffic to those ports.


Use the following commands:

sudo ufw allow from 10.0.0.1 to any port 8000

sudo ufw allow from 10.0.0.1 to any port 8001

sudo ufw allow from 10.0.0.1 to any port 8002


Now here are the nginx .conf.d files. Like I said these are


Filename: app1.com.conf

server { listen 80; listen [::]:80; server_name app1.com; client_max_body_size 30G; location /static { alias /home/nick/applications/app1/app_package/static; } location / { proxy_pass http://localhost:8000; include /etc/nginx/proxy_params; proxy_redirect off; } }

Filename: app4.com.conf

server { listen 80; listen [::]:80; server_name app4.com; client_max_body_size 30G; location /static { alias /home/nick/applications/app4/app_package/static; } location / { proxy_pass http://localhost:8001; include /etc/nginx/proxy_params; proxy_redirect off; # Increase the proxy read timeout proxy_read_timeout 600s; # Sets the timeout to 600 seconds } }

Filename: app3.com.conf

server { listen 80; listen [::]:80; server_name app3.com; client_max_body_size 30G; location /static { alias /home/nick/applications/app3/app_package/static; } location / { proxy_pass http://localhost:8002; include /etc/nginx/proxy_params; proxy_redirect off; # Increase the proxy read timeout proxy_read_timeout 600s; # Sets the timeout to 600 seconds } }

Step 5: Set up Computer 3 (other Ubuntu Machine)

Set this one up just like Computer 2.


Access the terminal. Install Nginx. Open UFW. Since for now we only have one application running we only need to add the rule for that application’s port, which his 8000.


Use the following commands:

sudo ufw allow from 10.0.0.1 to any port 8000

Now that file looks like:


Filename: app2.com.conf

server { listen 80; listen [::]:80; server_name app2.com; client_max_body_size 30G; location /static { alias /home/nick/applications/app2/app_package/static; } location / { proxy_pass http://localhost:8000; include /etc/nginx/proxy_params; proxy_redirect off; } }

The key thing to know is that this set up works. I am using it now I have only replaced the IP address and urls with dummy values.


If you have a struggle feel free to email me and if I know the answer I’ll do my best to get back to you.