server {
    server_name radionica3d.me 148.230.71.134;

    root /var/www/radionica3d/dist;
    index index.html;
    charset utf-8;
    client_max_body_size 100M;

    # Gzip Compression
    gzip on;
    gzip_static on; # Serve .gz files if they exist
    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 text/xml application/xml application/xml+rss text/javascript image/svg+xml font/woff2;

    # Security Headers
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    add_header Referrer-Policy "strict-origin-when-cross-origin";
    # add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

    # SEO & Prerendering Optimization
    location / {
        # First attempt to serve request as file, then as directory/index.html, 
        # then fall back to the main index.html (SPA)
        try_files $uri $uri/index.html /index.html;
        
        # Caching for index.html (don't cache)
        location = /index.html {
            add_header Cache-Control "no-store, no-cache, must-revalidate";
        }
    }

    # Static assets in /assets/ (Vite)
    location ^~ /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # Font files (local hosting, long-term cache)
    location ^~ /fonts/ {
        expires 10y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # Other static files
    location ~* \.(?:ico|gif|jpe?g|png|svg|webp|avif)$ {
        expires 9d;
        add_header Cache-Control "public";
        access_log off;
    }

    # Proxy API requests to backend
    location /api/ {
        proxy_pass http://127.0.0.1:8000/;
        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;
    }

    # Standalone Deploy Webhook
    location /deploy-webhook {
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host $host;
    }

    # WebSocket requests
    location /ws/ {
        proxy_pass http://127.0.0.1:8000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_read_timeout 86400;
    }

    # Static uploads
    location ^~ /uploads/ {
        alias /var/www/radionica3d/backend/uploads/;
        expires 30d;
        add_header Cache-Control "public";
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/radionica3d.me/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/radionica3d.me/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = radionica3d.me) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name radionica3d.me;
    return 301 https://radionica3d.me$request_uri;
}
