Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

3.0 KB
Raw Blame
# =============================================================================
# ©AngelaMos | 2026
# prod.nginx
# =============================================================================
# Production server block: serves built static files and proxies /api.
# Note: SSL handled by Cloudflare, not here.
# /api is resolved at request time so nginx still boots when the backend
# (the `backend` compose profile) is not running, returning 502 until it is.

server {
    listen 80;
    listen [::]:80;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    access_log /var/log/nginx/access.log main_timed buffer=32k flush=5s;
    error_log /var/log/nginx/error.log warn;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;

    location /health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }

    # Server-Sent Events: the live stream must never be buffered or gzipped here
    # or it never reaches the browser. Long read timeout so the connection
    # survives between keep-alives.
    location /api/stream {
        resolver 127.0.0.11 ipv6=off valid=10s;
        set $tlsfp_upstream tlsfp;
        proxy_pass http://$tlsfp_upstream:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        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 CF-Connecting-IP $http_cf_connecting_ip;
        proxy_set_header Connection "";
        proxy_buffering off;
        proxy_cache off;
        gzip off;
        chunked_transfer_encoding off;
        proxy_read_timeout 3600s;
    }

    location /api/ {
        resolver 127.0.0.11 ipv6=off valid=10s;
        set $tlsfp_upstream tlsfp;
        proxy_pass http://$tlsfp_upstream:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        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 CF-Connecting-IP $http_cf_connecting_ip;
        proxy_read_timeout 30s;
        limit_req zone=api_limit burst=20 nodelay;
    }

    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
        try_files $uri =404;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|svg|webp|avif|woff|woff2|ttf|eot|otf)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    location / {
        add_header Cache-Control "no-cache, must-revalidate";
        try_files $uri $uri/ /index.html;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}