Overview

Environment: Ubuntu 18.04. Objective: Use NGINX as proxy for multiple applications on the same VPS.

Install NGINX on vps

Reference tutorial

  1. NGINX comes with the default ubuntu repository
sudo apt update
sudo apt install nginx
  1. add firewall rules.

    1. first check the current firewall list
      sudo ufw status
      sudo ufw app list
      
    2. app list will list all available apps for the firewall. Because no ssl certificate at this point, Nginx HTTP is sufficient.
      sudo ufw allow 'Nginx HTTP'
      

    check ufw status again to make sure Nginx is allowed.

    1. check Nginx status to make sure the service is running
      systemctl status nginx
      

Setting up server blocks

Using server blocks to encapsulate configuration details and host more than 1 domain from a sigle server.

sudo mkdir -p /var/www/your_domain/html
sudo chown -R $USER:$USER /var/www/your_domain/html //$USER:$USER-group is set as the environment variables to assign the ownership of the file
sudo chmod -R 755 /var/www/your_domain
nano /var/www/your_domain/html/index.html

Now edit the index.html file to add some simple html.

Save the html file.

Edit nginx configuration file. Make a new configuration file at /etc/nginx/sites-available/your_domain

A typical http configuration file looks like:

server {
        listen 80;
        listen [::]:80;

        root /var/www/your_domain/html;
        index index.html index.htm index.nginx-debian.html;

        server_name your_domain.com www.your_domain;

        location / {
                try_files $uri $uri/ =404;
        }
}

Next, soft link it to sites-enabled

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Restart Nginx to 生效

  • test the configuration file
sudo nginx -t

when pass successfully

  • restart nginx
sudo systemctl restart nginx

Add a new subdomain and setting proxy with Nginx

On your DNS provider page

  • Add a new A record to the IP address (same server as example.com in this case). Use a new subdomain e.g www2.example.com

On the server end

  • set up a new server block for www2:
sudo mkdir -p /var/www/www2/html
sudo chown -R $USER:$USER /var/www/www2/html //$USER:$USER-group is set as the environment variables to assign the ownership of the file
sudo chmod -R 755 /var/www/your_domain
nano /var/www/www2/html/index.html
  • Add an index.html
  • Make a new configuration file at /etc/nginx/sites-available/www2

Use another port e.g 8080

server {
        listen 8080;
        listen [::]:8080;

        root /var/www/your_domain/html;
        index index.html index.htm index.nginx-debian.html;

        server_name www2.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Next, soft link this new configuration file to sites-enabled

sudo ln -s /etc/nginx/sites-available/www2 /etc/nginx/sites-enabled/

To avoid symbolic link error, use absolute path in this line.

  • Test and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx

Another tutorial that I referred to. This post shared the simple fix to symbolic link error.

Comments