Nginx Config Sample

·

1 min read

Nginx Config

In nginx.conf:

user nobody nogroup;
worker_processes auto;
worker_cpu_affinity auto;

events {
    worker_connections 1024;
    multi_accept on;
}

http {
    server_tokens off;
    # ...

    # gzip
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml image/svg+xml;

    # before include other sites' conf
    map $time_iso8601 $year {
        default '0000';
        "~^(\d{4})-(\d{2})-(\d{2})" $1;
    }
    map $time_iso8601 $month {
        default '00';
        "~^(\d{4})-(\d{2})-(\d{2})" $2;
    }
    map $time_iso8601 $day {
        default '00';
        "~^(\d{4})-(\d{2})-(\d{2})" $3;
    }
    # ...
    add_header Permissions-Policy "interest-cohort=()";
    add_header X-Xss-Protection "1; mode=block" always;
}

In site.conf:

server {
    location /static/  {
        alias /home/project/myproject/static/;

        location ~* \.(eot|ttf|otf|woff|woff2)$ {
            add_header 'Access-Control-Allow-Origin' '*';
        }

        location ~* \.(?:ico|css|js|gif|jpe?g|png|svg)$ {
            expires 30d;
        }
    }

    location /favicon.ico {
        alias /home/project/myproject/static/images/favicon.ico;
    }

    # increase client request buffer, if need to upload files
    client_body_buffer_size 20M;
    client_max_body_size 55M;

    # increase upstream response buffer
    proxy_buffers 16 1M;
    proxy_buffer_size 1M;

    # websocket request
    location /ws/ {
        proxy_pass "http://localhost:8080/";
        proxy_http_version 1.1;
        proxy_read_timeout 1800;
        proxy_send_timeout 1800;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    # http request
    location / {
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_set_header Connection "";
        proxy_redirect off;
        proxy_pass http://upstream_server;
    }

}