Nginx configuration file in a nutshell

Nginx configuration file in a nutshell

server {
    #you can add default_server to serve this website as the default
    listen 80;

    # Accepted domain names and ips
    server_name www.yamicode.com xxx.xxx.xxx.xxx;

    # SSL config
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/bdi.peaqock.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/bdi.peaqock.com/privkey.pem;

    # Redirection of non https trafic to https
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }

    # Root folder
    root "C:/path/to/files";
    
    # Default page to serve when accessing the "/" link, it should be present in "C:/path/to/files"
    index index.html index.htm index.php;
 
    # Access Restrictions
    allow       all;

    # Time out
    proxy_connect_timeout       500s;
    proxy_send_timeout          500s;
    proxy_read_timeout          500s;
    send_timeout                500s;

    # Enable gzip compression
    gzip on;
    gzip_comp_level 3;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Reverse proxy + Enabling WebSocket
    location /api{
        proxy_pass http://localhost:3100;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
    }

    # For Apps serving only index.html like Angular, React, Vue, ...
    location / {
        try_files $uri $uri/ /index.html =404;
    }

    # Encoding
    charset utf-8;

    # Disable Logs for some routes
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # Deny serving some file extensions
    location ~ /\.ht {
        deny all;
    }

    #Disable nginx directory listing
    autoindex off;

    # Enable Basic Auth
    location /admin{
        auth_basic "Secure area";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    # Cache: headers and expires
    location ~* \.(css|js){
        add_header Cache-Control public;
        add_header Pragma public;
        add_header Vary Accept-Encoding;
        #1 month
        expires 1M;
    }

    # Body post request size
    client_max_body_size 8m;

}