Skip to content

NGINX Configuration#

Install NGINX on Ubuntu#

sudo apt update && sudo apt install nginx -y && sudo nginx -v

The Basic Terms#

Configure NGINX to serve static files#

server {
    listen 80;

    location / {
        # static files
        root /usr/share/nginx/html;
        server_name domain.com;
        index index.html index.htm;
        try_files $uri $uri/ /index.html =404;
    }
}

Configure NGINX as a reverse proxy#

server {
    listen 80;

    location / {
        # static files
        root /usr/share/nginx/html;
        server_name domain.com;
        index index.html index.htm;
        try_files $uri $uri/ /index.html =404;
    }

    location /api {
        proxy_pass http://127.0.0.1:8080;
    }

}

HTTP Basic Authentication#

Install password file creation utility

sudo apt update && sudo apt install apache2-utils -y

Create password file & the first user

sudo htpasswd -c /etc/nginx/.htpasswd username

Add the second user to the password file

sudo htpasswd /etc/nginx/.htpasswd username2

NGINX config

server {
    auth_basic "Protected Area";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location /public/ {
        auth_basic off;
    }
}