installations

Installing Nginx

Run:

sudo apt update
sudo apt install nginx

/etc/nginx/sites-enabled/ and /etc/nginx/sites-available/ should now exist.


There is already a default html site config in /etc/nginx/sites-enabled/default


You can create a JS website template like this:

server {
  server_name example.com;
  # server_name _;

  location / {
    proxy_pass http://127.0.0.1:16012;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    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;
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  # This part will have SSL stuff added by certbot
}

server {
  if ($host = example.com) {
    return 301 https://$host$request_uri;
  }

  listen 80;
  server_name example.com;
  return 404;
}


I use these helper functions I added in my .bashrc or .bash_profile:


nginx_on() {
  if [ -z "$1" ]; then
    echo "Usage: nginx_on <site-name>"
    return 1
  fi

  sudo ln -s "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/"
  sudo service nginx restart
  echo "Enabled Nginx site: $1"
}

nginx_off() {
  if [ -z "$1" ]; then
    echo "Usage: nginx_off <site-name>"
    return 1;
  fi

  sudo rm "/etc/nginx/sites-enabled/$1"
  sudo service nginx restart
  echo "Disabled Nginx site: $1"
}


E.g. create site config /etc/nginx/sites-available/js, then

$ nginx_on js
$ nginx_off js