# Nginx Hauptkonfiguration für Stargirlnails Kiel
# Optimiert für Produktionsumgebung

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;

    # Performance-Optimierungen
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 10M;

    # Gzip-Kompression
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss
        application/atom+xml
        image/svg+xml;

    # Sicherheits-Header
    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 Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self';" always;

    # Rate Limiting
    limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;

    # Upstream für die Anwendung
    upstream stargirlnails {
        server stargirlnails:3000;
    }

    # HTTP-Server (leitet zu HTTPS weiter)
    server {
        listen 80;
        server_name _;
        
        # Let's Encrypt Challenge
        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
        
        # Alle anderen Anfragen zu HTTPS weiterleiten
        location / {
            return 301 https://$host$request_uri;
        }
    }

    # HTTPS-Server
    server {
        listen 443 ssl http2;
        server_name _;

        # SSL-Konfiguration
        ssl_certificate /etc/letsencrypt/live/${DOMAIN}/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/privkey.pem;
        
        # SSL-Optimierungen
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384;
        ssl_prefer_server_ciphers off;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        
        # HSTS
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        # Proxy-Einstellungen
        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 X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        # Timeouts
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;

        # API-Endpunkte mit Rate Limiting
        location /rpc {
            limit_req zone=api burst=20 nodelay;
            proxy_pass http://stargirlnails;
        }

        # Login-Endpunkte mit strengerem Rate Limiting
        location ~ ^/(login|auth) {
            limit_req zone=login burst=5 nodelay;
            proxy_pass http://stargirlnails;
        }

        # Statische Dateien (Assets)
        location /static/ {
            proxy_pass http://stargirlnails;
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # Assets-Verzeichnis
        location /assets/ {
            proxy_pass http://stargirlnails;
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # Favicon
        location /favicon.ico {
            proxy_pass http://stargirlnails;
            expires 1y;
            add_header Cache-Control "public";
        }

        # Health Check
        location /health {
            proxy_pass http://stargirlnails;
            access_log off;
        }

        # Security.txt
        location /.well-known/security.txt {
            proxy_pass http://stargirlnails;
            expires 1d;
        }

        # Alle anderen Anfragen
        location / {
            proxy_pass http://stargirlnails;
        }
    }
}
