# Softswitch Portal - Standalone Nginx Configuration
# Use this when Portal is installed on a separate server

server {
    listen 80;
    server_name _;
    add_header X-Robots-Tag "none" always;

    access_log /var/log/nginx/portal_access.log;
    error_log /var/log/nginx/portal_error.log;

    # API proxy (to Admin/API Server)
    # Update ADMIN_SERVER_IP with your Admin server IP
    location /api/ {
        proxy_pass http://ADMIN_SERVER_IP:3001;
        
        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 $scheme;
        
        proxy_read_timeout 300s;
        client_max_body_size 100M;
    }

    # WebSocket proxy (PBX API Realtime / Signaling)
    location /ws/ {
        proxy_pass http://ADMIN_SERVER_IP:3001;

        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_read_timeout 86400;
        proxy_send_timeout 86400;
    }

    # Ring2All Connect / Extension User Portal (canonical /connect/ base path)
    location /connect/ {
        alias /var/www/softswitch/connect/;
        index index.html;
        try_files $uri $uri/ @connect_index;
    }
    location @connect_index {
        root /var/www/softswitch/connect;
        try_files /index.html =404;
    }
    location = /connect {
        return 301 /connect/;
    }

    # Legacy User Portal -> Ring2All Connect redirect
    location /portal/ {
        return 301 /connect/;
    }
    location = /portal {
        return 301 /connect/;
    }

    # Redirect standalone root to canonical /connect/ base path
    location = / {
        return 302 /connect/;
    }

    # Fallback static serving at root
    location / {
        root /var/www/softswitch/portal;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}
