rrebase/knboard

View on GitHub
ansible/roles/nginx/templates/nginx.conf

Summary

Maintainability
Test Coverage
# Note: There are some variables with underscores
# that are replaced during the build process

worker_processes auto;
worker_rlimit_nofile 65535;

events {
    multi_accept on;
    worker_connections 65535;
}

http {
    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    log_not_found off;
    types_hash_max_size 2048;
    client_max_body_size 16M;

    # MIME
    include mime.types;
    default_type application/octet-stream;

    # logging
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log warn;

    # SSL
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;

    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        server_name {{ domain_name }};
        root /usr/share/nginx/html;
        index index.html;

        # SSL certs
        ssl_certificate /etc/letsencrypt/live/{{ domain_name }}/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/{{ domain_name }}/privkey.pem;

        # Security headers
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "no-referrer-when-downgrade" always;
        add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

        # Deny all dotfiles
        location ~ /\.(?!well-known) {
            deny all;
        }

        # React build index.html as fallback
        location / {
            try_files $uri $uri/ /index.html;
        }

        # Django static
        location ^~ /django-static {
            root /usr/share/nginx;
            try_files $uri =404;
        }

        # Django auth endpoints
        location /auth {
            proxy_pass http://django:8000;
            include proxy.conf;
        }

        # Django REST framework
        location /api {
            proxy_pass http://django:8000;
            include proxy.conf;
        }

        # Django admin
        location /backdoor {
            proxy_pass http://django:8000;
            include proxy.conf;
        }

        # favicon.ico
        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        # robots.txt
        location = /robots.txt {
            log_not_found off;
            access_log off;
        }

        # assets, media
        location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
            expires 7d;
            access_log off;
        }

        # svg, fonts
        location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
            add_header Access-Control-Allow-Origin "*";
            expires 7d;
            access_log off;
        }

        # gzip
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
    }

    # Subdomains redirect
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        server_name *.{{ domain_name }};

        # SSL
        ssl_certificate /etc/letsencrypt/live/{{ domain_name }}/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/{{ domain_name }}/privkey.pem;

        return 301 https://{{ domain_name }}$request_uri;
    }

    # HTTP redirect
    server {
        listen 80;
        listen [::]:80;

        server_name .{{ domain_name }};

        location / {
            return 301 https://{{ domain_name }}$request_uri;
        }

        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
    }
}