diff --git a/nginx/sites-available/telefonia.itguys.com.br.conf b/nginx/sites-available/telefonia.itguys.com.br.conf index 6791701..08944d8 100644 --- a/nginx/sites-available/telefonia.itguys.com.br.conf +++ b/nginx/sites-available/telefonia.itguys.com.br.conf @@ -1,53 +1,158 @@ +# ============================================================================== +# ARQUIVO: /etc/nginx/sites-available/telefonia.itguys.com.br.conf +# AUTOR: Gemini (Especialista NGINX) +# DATA: 27/09/2025 (Corrigido para problemas de MIME/404/403) +# +# CONTEXTO: +# Proxy Reverso Seguro e Otimizado para MagnusBilling (PHP/Apache). +# Assegura a passagem correta do caminho /mbilling/ sem NGINX rewrite. +# O ModSecurity foi desabilitado para assets estáticos (solução para 403). +# +# UPSTREAM: 172.16.254.130 (Backend HTTP) +# ============================================================================== + +# Bloco Upstream: Define o servidor de backend da aplicação. +upstream magnusbilling_backend { + server 172.16.254.130; + keepalive 64; +} + # ------------------------------------------------------------------------------ -# BLOCO 2: Servidor Principal HTTPS (porta 443) - CORRIGIDO +# BLOCO 1: Redirecionamento de HTTP (porta 80) para HTTPS +# ------------------------------------------------------------------------------ +server { + listen 80; + listen [::]:80; + server_name telefonia.itguys.com.br; + + # Aplica o rate limiting básico (zona global_limit deve estar em nginx.conf) + limit_req zone=global_limit burst=20 nodelay; + + location /.well-known/acme-challenge/ { + root /var/www/html; + } + + # Redireciona permanentemente todo o resto para a versão segura. + location / { + return 301 https://$host$request_uri; + } +} + +# ------------------------------------------------------------------------------ +# BLOCO 2: Servidor Principal HTTPS (porta 443) # ------------------------------------------------------------------------------ server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name telefonia.itguys.com.br; - # ... (diretivas de LOGS, SSL/TLS, HEADERS e COMPRESSÃO permanecem aqui) ... + # ============================================================================ + # LOGS, SEGURANÇA BÁSICA E RATE LIMITING + # ============================================================================ + client_max_body_size 25M; + access_log /var/log/nginx/telefonia.itguys.com.br.access.log combined; + error_log /var/log/nginx/telefonia.itguys.com.br.error.log warn; + access_log /var/log/nginx/telefonia.itguys.com.br.bad-bot.log suspicious_bot if=$is_bad_bot; + + if ($block_request) { + return 404; + } + + limit_req zone=global_limit burst=50 nodelay; + + # ============================================================================ + # CONFIGURAÇÕES DE SSL/TLS (Hardening) + # ============================================================================ + ssl_certificate /etc/letsencrypt/live/telefonia.itguys.com.br/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/telefonia.itguys.com.br/privkey.pem; + ssl_protocols TLSv1.3 TLSv1.2; + ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305'; + ssl_prefer_server_ciphers on; + ssl_ecdh_curve X25519:secp256r1:secp384r1; + ssl_session_timeout 1d; + ssl_dhparam /etc/nginx/dhparam.pem; + ssl_session_tickets off; + + ssl_stapling on; + ssl_stapling_verify on; + ssl_trusted_certificate /etc/letsencrypt/live/telefonia.itguys.com.br/fullchain.pem; + resolver 8.8.8.8 8.8.4.4 valid=300s; + resolver_timeout 5s; + + # ============================================================================ + # HEADERS DE SEGURANÇA + # ============================================================================ + add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-XSS-Protection "1; mode=block" always; + add_header Referrer-Policy "no-referrer" always; + add_header Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=()" always; + add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; object-src 'none';" always; + + # ============================================================================ + # COMPRESSÃO + # ============================================================================ + brotli on; + brotli_comp_level 6; + brotli_min_length 256; + brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml; + gzip on; + gzip_vary on; + gzip_proxied any; + gzip_comp_level 6; + gzip_min_length 256; + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml; # ============================================================================ # PARÂMETROS GLOBAIS DE PROXY # ============================================================================ - # ... (diretivas proxy_set_header, timeouts e proxy_redirect off permanecem aqui) ... + 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_set_header User-Agent $http_user_agent; + proxy_http_version 1.1; + proxy_connect_timeout 60s; + proxy_read_timeout 90s; + proxy_send_timeout 90s; + proxy_buffering off; + proxy_request_buffering off; + proxy_redirect off; # ---------------------------------------------------------------------------- # REGRAS DE ROTEAMENTO (LOCATIONS) # ---------------------------------------------------------------------------- - # 1. Rota para assets estáticos e arquivos de recurso - # Garante que arquivos estáticos sejam tratados de forma diferente do index.php. + # 1. Rota para assets estáticos e arquivos de recurso - SOLUÇÃO MIME/403 + # Esta location é mais específica e desabilita ModSec para assets (logo.ico, js, json, etc.). location ~* /mbilling/.*\.(?:css|js|mjs|svg|gif|png|jpg|jpeg|ico|wasm|woff2?|ttf|eot|json|xml|mp3|wav|ogg)$ { - # SOLUÇÃO PARA O 403 (logo.ico): Desabilita ModSecurity nos assets estáticos - # Isso deve resolver o problema de bloqueio 403 + # DESABILITA ModSecurity (Solução para 403 Forbidden) modsecurity off; + # Configurações de Cache proxy_cache magnusbilling_cache; proxy_cache_valid 200 7d; proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; add_header Cache-Control "public, max-age=604800, immutable"; add_header X-Proxy-Cache $upstream_cache_status; - # Envia a requisição *diretamente* para o backend. Se o backend não achar, - # ele deve retornar um 404 limpo, e não a página HTML. - proxy_pass http://magnusbilling_backend; + proxy_pass http://magnusbilling_backend; } - # 2. Rota principal para a aplicação (/mbilling/) - Inclui WebSockets e index.php - # Esta rota deve tratar todas as URLs que não são arquivos estáticos (URLs limpas, login, index.php) + # 2. Rota principal para a aplicação (/mbilling/) + # Lida com todas as requisições dinâmicas (URLs limpas, index.php, WebSockets). location /mbilling/ { # Configurações para suportar WebSockets proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; - # Rate Limiting específico para proteger o login (mantido por segurança) + # Rate Limiting de login (zona 'login_limit' deve ser definida em nginx.conf) limit_req zone=login_limit burst=5 nodelay; - # IMPORTANTE: Se o backend espera que a requisição comece com /mbilling, - # esta diretiva proxy_pass fará isso. proxy_pass http://magnusbilling_backend; } @@ -56,7 +161,7 @@ server { return 302 /mbilling/; } - # 4. Bloqueio de arquivos e diretórios sensíveis (mantido por segurança) + # 4. Bloqueio de arquivos e diretórios sensíveis location ~* /(?:composer\.(?:json|lock)|vendor/|etc/|var/|temp/|\.git/|\.env|setup\.php|install\.php) { deny all; return 404;