# ==============================================================================
# NGINX SERVER CONFIGURATION FOR ADTLAS - FIXED VERSION
# ==============================================================================

server {
    listen 80;
    server_name localhost 127.0.0.1 173.212.199.208;
    
    # Health check endpoint for load balancers
    location /nginx-health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }
    
    # Static files (CSS, JS, Images)
    location /static/ {
        alias /var/www/static/;
        expires 1y;
        add_header Cache-Control "public, immutable";
        gzip_static on;
        add_header X-Content-Type-Options nosniff;
        try_files $uri $uri/ =404;
        access_log off;
    }
    
    # Media files (User uploads)
    location /media/ {
        alias /var/www/media/;
        expires 1w;
        add_header Cache-Control "public";
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options DENY;
        try_files $uri $uri/ =404;
    }
    
    # Health check endpoint for Django
    location /health/ {
        access_log off;
        proxy_pass http://django_app;
        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_connect_timeout 5s;
        proxy_send_timeout 5s;
        proxy_read_timeout 5s;
    }
    
    # All other requests go to Django application
    location / {
        proxy_pass http://django_app;
        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_set_header X-Forwarded-Host $server_name;
        proxy_connect_timeout 30s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
        proxy_busy_buffers_size 8k;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    
    # Deny access to hidden files
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    
    # Robots.txt
    location = /robots.txt {
        alias /var/www/static/robots.txt;
        access_log off;
        log_not_found off;
    }
    
    # Favicon
    location = /favicon.ico {
        alias /var/www/static/favicon.ico;
        access_log off;
        log_not_found off;
        expires 1y;
    }
}
