# Configuração Nginx para Loucas por Beleza
# Salve este arquivo em /etc/nginx/sites-available/loucasporbeleza.com.br

# Redirecionar HTTP para HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name www.loucasporbeleza.com.br loucasporbeleza.com.br;
    
    return 301 https://www.loucasporbeleza.com.br$request_uri;
}

# Servidor HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.loucasporbeleza.com.br;

    # Certificados SSL (ajuste os caminhos conforme seu certificado)
    ssl_certificate /etc/letsencrypt/live/www.loucasporbeleza.com.br/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.loucasporbeleza.com.br/privkey.pem;
    
    # Configurações SSL
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # Tamanho máximo de upload (para imagens de produtos)
    client_max_body_size 10M;

    # Logs
    access_log /var/log/nginx/loucasporbeleza-access.log;
    error_log /var/log/nginx/loucasporbeleza-error.log;

    # Root do frontend (build do React)
    root /var/www/loucasporbeleza.com.br/client/build;
    index index.html;

    # Proxy para API (Backend na porta 3005)
    location /api {
        proxy_pass http://localhost:3005;
        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_cache_bypass $http_upgrade;
        
        # Timeout para uploads grandes
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
    }

    # Proxy para uploads (imagens)
    location /uploads {
        proxy_pass http://localhost:3005;
        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;
    }

    # Frontend React (SPA)
    location / {
        try_files $uri $uri/ /index.html;
        
        # Cache para assets estáticos
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
    }

    # Segurança
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
}

