# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/wolfgang.mozartdigital.fr/before/*;

# Configuration pour OpenWebUI avec reverse proxy
upstream openwebui_backend {
    server 127.0.0.1:8080;
    keepalive 32;
}

server {
    listen 80;
    listen [::]:80;
    server_name wolfgang.mozartdigital.fr;
    return 301 https://$server_name$request_uri;
}

server {
    http2 on;
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name wolfgang.mozartdigital.fr;
    server_tokens off;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/wolfgang.mozartdigital.fr/2796033/server.crt;
    ssl_certificate_key /etc/nginx/ssl/wolfgang.mozartdigital.fr/2796033/server.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    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_prefer_server_ciphers off;
    ssl_dhparam /etc/nginx/dhparams.pem;

    # Headers de sécurité HTTPS
    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' 'unsafe-inline' 'unsafe-eval' data: blob:; connect-src 'self' wss:; upgrade-insecure-requests;";
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
    add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";

    # Configuration pour les gros uploads
    client_max_body_size 100M;
    client_body_timeout 300s;
    client_header_timeout 300s;
    client_body_buffer_size 1m;
    client_body_temp_path /tmp/client_body_temp;

    # Configuration des logs
    access_log /var/log/nginx/wolfgang.mozartdigital.fr-access.log;
    error_log /var/log/nginx/wolfgang.mozartdigital.fr-error.log;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/wolfgang.mozartdigital.fr/server/*;

    # Reverse proxy vers OpenWebUI
    location / {
        proxy_pass http://openwebui_backend;
        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 https;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_cache_bypass $http_upgrade;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_send_timeout 300s;
    }

    # Support pour les WebSockets HTTPS (nécessaire pour OpenWebUI)
    location /ws {
        proxy_pass http://openwebui_backend;
        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 https;
        proxy_set_header X-Forwarded-Port 443;
        proxy_read_timeout 86400;
    }

    # Gestion des fichiers statiques avec cache
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        proxy_pass http://openwebui_backend;
        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 https;
        proxy_set_header X-Forwarded-Port 443;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # Gestion du favicon
    location = /favicon.ico {
        proxy_pass http://openwebui_backend;
        proxy_set_header Host $host;
        access_log off;
        log_not_found off;
    }

    # Gestion du robots.txt
    location = /robots.txt {
        proxy_pass http://openwebui_backend;
        proxy_set_header Host $host;
        access_log off;
        log_not_found off;
    }

    # Sécurité - bloquer l'accès aux fichiers cachés
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/wolfgang.mozartdigital.fr/after/*; 