Setting up a reverse proxy server with NGINX

Mon Jan 22 2018

If you want to host multiple web applications on one single server you are facing the problem that there is only one port 80 available. The solution is a reverse proxy server. It takes the HTTP headers of the incoming requests and redirects the traffic to the corresponding server/port based on the domain name.

First you have to install NGINX:

sudo apt-get install nginx

Then go to /etc/nginx/sites-available. If you just installed nginx, there will be only the default config file in this folder. Create a new file for each domain in this folder. For simplicity, it's best to name this file just like the domain without any suffixes. A basic configuration could look like this:

/etc/nginx/sites-available/domain

server: {
listen 80;
server_name domain.com www.domain.com;

location / {
proxy_pass http://127.0.0.1:3000;
}
}

Make sure to include the URL both with and without the "www" to ensure that it correctly redirects incoming requests for both cases. Repeat this process for all domains you want to add.

In this case I am redirecting traffic coming from domain.com to a process running on the same server on the port 3000.

In order to enable the new configuration you have to create soft links for each file you created. Go to /etc/nginx/sites-enabled and execute the following command for each file:

sudo ln -s ../sites-enabled/domain .

You also have to delete the default configuration by deleting its symlink (sudo rm default in the sites-enabled directory).

Finally, you only have to restart NGINX and your new configurations will be applied:

sudo nginx -s reload


Tags:

NGINX, RaspberryPi, reverse proxy, host multiple domains on one server, Linux, Tutorial

Website created by Felix Gerschau © 2017