# DC Overview - Nginx Reverse Proxy Configuration
# Generated by dc-overview
#
# SECURITY NOTE: Backend services (Grafana:3000, Prometheus:9090, IPMI:5000)
# should bind to 127.0.0.1 only. They are NOT exposed to the internet directly.
# All external access goes through this HTTPS reverse proxy on port 443.

upstream grafana {
    server 127.0.0.1:3000;
}

{% if prometheus_enabled %}
upstream prometheus {
    server 127.0.0.1:9090;
}
{% endif %}

{% if ipmi_enabled %}
upstream ipmi {
    server 127.0.0.1:5000;
}
{% endif %}

# Redirect HTTP to HTTPS
server {
    listen 80 default_server;
    server_name {{ domain or '_' }};
    return 301 https://$host$request_uri;
}

# Main HTTPS server
server {
    listen 443 ssl http2 default_server;
    server_name {{ domain or '_' }};

    # SSL Configuration
    ssl_certificate {{ ssl_cert }};
    ssl_certificate_key {{ ssl_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;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Default root for static files
    root /var/www/dc-overview;
    
    # Landing page (fallback for unmatched paths)
    location / {
        try_files $uri $uri/ @grafana;
    }
    
    # Fallback to Grafana for SPA routing
    location @grafana {
        proxy_pass http://grafana;
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # Grafana (has built-in authentication)
    location /grafana/ {
        proxy_pass http://grafana/;
        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;
        
        # WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

{% if prometheus_enabled %}
    # Prometheus (WARNING: No built-in authentication!)
    # Only enable if you have network-level security or have configured auth
    location /prometheus/ {
        proxy_pass http://prometheus/;
        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;
    }
{% endif %}

{% if ipmi_enabled %}
    # IPMI Monitor
    location /ipmi/ {
        proxy_pass http://ipmi/;
        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;
    }
{% endif %}

    # Health check
    location /health {
        return 200 'OK';
        add_header Content-Type text/plain;
    }
}

{% if grafana_subdomain %}
# Grafana subdomain
server {
    listen 443 ssl http2;
    server_name grafana.{{ domain }};

    ssl_certificate {{ ssl_cert }};
    ssl_certificate_key {{ ssl_key }};
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://grafana;
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
{% endif %}

{% if ipmi_subdomain %}
# IPMI subdomain
server {
    listen 443 ssl http2;
    server_name ipmi.{{ domain }};

    ssl_certificate {{ ssl_cert }};
    ssl_certificate_key {{ ssl_key }};
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://ipmi;
        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;
    }
}
{% endif %}
