server {
    listen 8443 ssl;
    server_name _;
    root /opt/opengnsys/ogcore/api/public/;
    index index.html index.php;

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

    location ~ ^/pcclients/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(/.*)?$ {
        set $target_ip $1;
        set $rest $2;

        proxy_pass http://$target_ip$rest;
        proxy_set_header Host $target_ip;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    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 unix:/var/run/php/php8.3-fpm-ogcore.sock;
        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/ogcore-error.log;
    access_log /var/log/nginx/ogcore-access.log;
}
