worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 25M;

    upstream django_gunicorn {
        # Usa el nombre del servicio del contenedor Django/Gunicorn en tu red Docker.
        # Si en docker-compose el servicio se llama distinto, cámbialo aquí.
        server django:8000;
    }

    server {
        listen 80;
        server_name _;

        # Archivos estáticos generados por collectstatic.
        location /static/ {
            alias /static/;
            expires 30d;
            add_header Cache-Control "public, max-age=2592000, immutable";
            access_log off;
        }

        # Archivos subidos por usuarios.
        location /media/ {
            alias /media/;
            expires 7d;
            add_header Cache-Control "public, max-age=604800";
            access_log off;
        }

        location / {
            proxy_pass http://django_gunicorn;
            proxy_http_version 1.1;

            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 $host;
            proxy_set_header X-Forwarded-Port $server_port;

            proxy_redirect off;
            proxy_read_timeout 300;
        }
    }
}
