server {
    listen 80;
    server_name localhost;

    # Redirigir todo el tráfico HTTP a HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name localhost;
    root /var/www/html/public;
    index index.html index.php;

    #ssl_certificate /opt/opengnsys/ogcore/etc/certificates/ogcore.crt;
    #ssl_certificate_key /opt/opengnsys/ogcore/etc/certificates/ogcore.key;
    ssl_certificate /etc/nginx/certs/ogcore.uds-test.net.crt.pem;
    ssl_certificate_key /etc/nginx/certs/ogcore.uds-test.net.key.pem;

    location /opengnsys/rest/ous// {
        rewrite ^/opengnsys/rest/ous//([0-9]+)/images /opengnsys/rest/ous/$1/images;
        rewrite ^/opengnsys/rest/ous//([0-9]+)/labs /opengnsys/rest/ous/$1/labs;
    }

    # Bloque principal para archivos
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Manejo de PHP
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $request_uri;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    }

    # Bloque para errores PHP
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/error.log debug;
    access_log /var/log/nginx/access.log;

    #ssl_client_certificate /opt/opengnsys/ogcore/etc/certificates/ca.crt;
    #ssl_verify_client on;
}

server {
    listen 3000 ssl http2;
    listen [::]:3000 ssl http2;


    ssl_certificate /etc/nginx/certs/ogcore.uds-test.net.crt.pem;
    ssl_certificate_key /etc/nginx/certs/ogcore.uds-test.net.key.pem;

    location / {
        proxy_pass http://ogcore-mercure:3000/; 
        proxy_read_timeout 24h;
        proxy_http_version 1.1;
        proxy_set_header Connection "";

        # Enable fast reply in SSE
        proxy_buffering off;

        ## Be sure to set USE_FORWARDED_HEADERS=1 to allow the hub to use those headers ##
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}