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 👇

4.1 KB
Raw Blame
# ⒸAngelaMos | 2025
# Production Nginx Configuration
# Features:
# - Serves static frontend files from /usr/share/nginx/html
# - Proxies /api to FastAPI backend
# - Caching, gzip compression, security headers
# - SSL/HTTPS ready (commented out, uncomment when you have certificates)
# - Use Cloudfare tls with dns proxy if no tls in nginx

events {
    worker_connections 2048;
}

http {
    # Include shared config
    include /etc/nginx/http.conf;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

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

    # Performance optimizations
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss
        application/rss+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;

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

        # Uncomment below to redirect HTTP to HTTPS in production
        # return 301 https://$server_name$request_uri;

        # Client upload limits
        client_max_body_size 10M;
        client_body_buffer_size 128k;

        # Security headers
        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 "no-referrer-when-downgrade" always;

        # Backend API routes
        location /api {
            proxy_pass http://backend;

            # Apply common proxy settings
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            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_cache_bypass $http_upgrade;

            # Timeouts for API requests
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;
        }

        # Serve static frontend files
        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html;

            # Cache static assets
            location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
                expires 1y;
                add_header Cache-Control "public, immutable";
            }

            # Don't cache index.html
            location = /index.html {
                add_header Cache-Control "no-cache, no-store, must-revalidate";
            }
        }

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

    # HTTPS server (uncomment and configure when you have SSL certificates)
    # server {
    #     listen 443 ssl http2;
    #     listen [::]:443 ssl http2;
    #     server_name yourdomain.com;
    #
    #     # SSL certificates (use Let's Encrypt or similar)
    #     ssl_certificate /etc/nginx/ssl/cert.pem;
    #     ssl_certificate_key /etc/nginx/ssl/key.pem;
    #
    #     # SSL configuration
    #     ssl_protocols TLSv1.2 TLSv1.3;
    #     ssl_ciphers HIGH:!aNULL:!MD5;
    #     ssl_prefer_server_ciphers on;
    #     ssl_session_cache shared:SSL:10m;
    #     ssl_session_timeout 10m;
    #
    #     # Security headers
    #     add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    #     add_header X-Frame-Options "SAMEORIGIN" always;
    #     add_header X-Content-Type-Options "nosniff" always;
    #     add_header X-XSS-Protection "1; mode=block" always;
    #
    #     # Same location blocks as HTTP server above
    #     # ...
    # }
}