# Nginx Configuration for skyartshop.dynns.com with SSL
# Updated: January 2026

# Website root directory
# Change this to match your deployment path
# Development: /media/pts/Website/SkyArtShop/website
# Production: /var/www/skyartshop

# HTTP Server - Redirects all HTTP to HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name skyartshop.dynns.com localhost;

    # Let's Encrypt verification (required for certificate renewal)
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
        allow all;
    }

    # Redirect all other HTTP traffic to HTTPS
    location / {
        return 301 https://skyartshop.dynns.com$request_uri;
    }
}

# HTTPS - Main Secure Server
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name skyartshop.dynns.com;

    # SSL Certificate Configuration
    ssl_certificate /etc/letsencrypt/live/skyartshop.dynns.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/skyartshop.dynns.com/privkey.pem;
    
    # SSL Settings (modern configuration)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    
    # SSL Session Settings
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    
    # OCSP Stapling (disabled - not supported by all certs)
    # ssl_stapling on;
    # ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    # Security Headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" 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;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;

    # Logs
    access_log /var/log/nginx/skyartshop-access.log;
    error_log /var/log/nginx/skyartshop-error.log;

    # Root directory - ACTUAL PATH
    root /media/pts/Website/SkyArtShop/website/public;
    index index.html;

    # Gzip Compression
    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;

    # Admin area - exact matches to redirect
    location = /admin {
        return 302 /admin/login;
    }

    location = /admin/ {
        return 302 /admin/login;
    }

    # API proxy to Node.js backend
    location /api/ {
        proxy_pass http://127.0.0.1:5000;
        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;
        
        # Allow large file uploads (100MB for multiple images)
        client_max_body_size 100M;
        
        # Timeouts for large uploads
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        
        # Buffer settings
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
    }

    # Static files with caching - ACTUAL PATH
    location /assets/ {
        alias /media/pts/Website/SkyArtShop/website/public/assets/;
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    location /uploads/ {
        alias /media/pts/Website/SkyArtShop/website/uploads/;
        expires 30d;
        add_header Cache-Control "public";
    }

    # Admin static files - ACTUAL PATH (with .html fallback)
    location /admin/ {
        alias /media/pts/Website/SkyArtShop/website/admin/;
        try_files $uri $uri.html $uri/ =404;
        
        # Disable caching for admin HTML files
        location ~* \.html$ {
            add_header Cache-Control "no-cache, no-store, must-revalidate";
            add_header Pragma "no-cache";
            add_header Expires "0";
        }
    }

    # Root redirect handled by backend
    location = / {
        proxy_pass http://127.0.0.1:5000;
        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;
    }

    # Health check
    location /health {
        proxy_pass http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }

    # Favicon
    location = /favicon.ico {
        alias /media/pts/Website/SkyArtShop/website/public/favicon.svg;
        access_log off;
        log_not_found off;
    }

    # Robots.txt
    location = /robots.txt {
        alias /media/pts/Website/SkyArtShop/website/public/robots.txt;
        access_log off;
        log_not_found off;
    }

    # Deny access to hidden files
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # All other requests go to backend
    location / {
        proxy_pass http://127.0.0.1:5000;
        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;
    }
}
