@KizaHere

NGINX cheat sheet

Basics

Configuration directories:

ls /etc/nginx/sites-available
ls /etc/nginx/sites-enabled

Static HTML:

/var/www/html
nano /etc/nginx/sites-available/{{domain}}

Commands:

sudo systemctl enable nginx
sudo systemctl disable nginx
sudo systemctl reload nginx
sudo systemctl restart nginx

Validate Configs:

nginx -t

Default Logs locations:

/var/log/nginx/access.log
/var/log/nginx/error.log

Sites enabled is the folder which is used by NGINX to serve sites.

Normally a symlink is created between sites-available and sites-enabled to enable a site.

To create a symlink from sites-available to sites-enabled:

ln -s /etc/nginx/sites-available/{{domain}} /etc/nginx/sites-enabled/{{domain}}

Ensure you provide an absolute path:

Symlink should show up cyan on putty - not red

To delete a symlink simply use rm:

rm {{domain}}

Templates

Web socket Proxy pass template

server {
    server_name {{domain}};
    listen 7070 ssl;
    listen [::]:7070 ssl;

    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/{{domain}}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    include /etc/nginx/snippets/ssl.conf;

    location / {
            proxy_pass http://localhost:7077;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }
}

Proxy pass template

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/{{domain}}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    include /etc/nginx/snippets/ssl.conf;
    include /etc/nginx/snippets/letsencrypt.conf;

    server_name {{domain}};

    location / {
        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_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:8987;
    }
}

Generating a cert with Lets Encrypt

Use the following NGINX template before generating the cert:

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name {{domain}};

    include /etc/nginx/snippets/letsencrypt.conf;

    location / {
        proxy_pass http://localhost:8084;
    }
}

Generate the cert:

certbot certonly --webroot --agree-tos --no-eff-email --email letsencrypt@vsi.systems -w /var/www/letsencrypt -d {{domain}}

Guide followed

Wildcard cert (expects wildcard domain e.g. *.mysites.com):

certbot certonly --manual --agree-tos --no-eff-email --preferred-challenges=dns --email letsencrypt@vsi.systems -w /var/www/letsencrypt -d {{domain}}

Renew

certbot --nginx certonly -n -d {{domain}}

or

certbot renew

Remember to reload nginx after a cert update.