Nginx: Part I - Set up a reverse proxy (Ubuntu 18.04)

A reverse proxy is useful for hosting multiple projects on one server. It allows you to connect multiple domains to your server, and serve a different application, depending on which domain was entered.

Prerequisites

  • One Server
  • One Domain

Installing Dependencies

We will use nginx in this tutorial:

sudo apt update
sudo apt install nginx

Setting up the Firewall

To allow incoming connections to our nginx web server, we will need to adjust the firewall settings:

sudo ufw allow 'Nginx Full'
This opens port 80 and 443 in the firewall

Writing our configuration

Create a file (using vim or nano) at /etc/nginx/sites-available/your.domain.com.conf and paste the following:

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name your.domain.com;
    root /var/www/your.domain.com;

    index index.html;
    location / {
            proxy_set_header Host $host;
            proxy_pass http://localhost:5555;
    }
}
This forwards all requests to your.domain.com to the app running on port 5555

You will have to adjust two things in this config file to fit with your set up:

  • Replace your.domain.com, with your domain/subdomain
  • Replace 5555 with the port number of the app you want to proxy to

When you're done, save the file.

Enable our configuration

We're almost done, the only thing left to do is to enable the configuration.

sudo ln -s /etc/nginx/sites-available/your.domain.com.conf  /etc/nginx/sites-enabled/your.domain.com.conf
sudo systemctl reload nginx

Part II

Now that you've set up nginx, you should set up SSL to encrypt your traffic, which is a requirement for most applications.

Read part two, to find out how →

Show Comments