diff --git a/nginx/sites-available/unifi.itguys.com.br.conf b/nginx/sites-available/unifi.itguys.com.br.conf new file mode 100644 index 0000000..d2eaf70 --- /dev/null +++ b/nginx/sites-available/unifi.itguys.com.br.conf @@ -0,0 +1,159 @@ +# ========================================================================================= +# ARQUIVO: /etc/nginx/sites-available/unifi.itguys.com.br.conf +# AUTOR: Gemini (Especialista NGINX) +# DATA: 24/09/2025 - 21:58 +# VERSÃO: 6.0 (Otimização para Performance e Segurança) +# +# DESCRIÇÃO: +# Configuração de Proxy Reverso OTIMIZADA e SEGURA para a interface UniFi. +# Esta versão é autocontida, com foco em velocidade, segurança e compatibilidade. +# +# MELHORIAS IMPLEMENTADAS: +# - CONFIGURAÇÃO AUTOCONTIDA: Diretivas de SSL e proxy foram internalizadas. +# - COMPRESSÃO AVANÇADA: Adicionado Brotli e Gzip para acelerar o carregamento. +# - SEGURANÇA REFORÇADA: Implementado conjunto completo de cabeçalhos de +# segurança, incluindo HSTS e uma CSP apropriada para o UniFi. +# - OTIMIZAÇÃO DE SSL/TLS: Utiliza protocolos e cifras modernos e seguros. +# - CACHE DE BROWSER: Define cache agressivo para assets estáticos. +# ========================================================================================= + +# --- DEFINIÇÃO DOS BACKENDS --- +upstream unifi_backend_web { + server 172.16.254.123:8443; +} +upstream unifi_backend_inform { + server 172.16.254.123:8080; +} + +# ========================================================================================= +# BLOCO 1: Redirecionamento de HTTP (porta 80) para HTTPS +# ========================================================================================= +server { + listen 80; + listen [::]:80; + server_name unifi.itguys.com.br; + + location /.well-known/acme-challenge/ { + root /var/www/html; + } + location / { + return 301 https://$host$request_uri; + } +} + +# ========================================================================================= +# BLOCO 2: Servidor Principal - Proxy Reverso para a Interface Web (HTTPS) +# ========================================================================================= +server { + listen 443 ssl http2; + listen [::]:443 ssl http2; + server_name unifi.itguys.com.br; + + # --- LOGS --- + access_log /var/log/nginx/unifi.itguys.com.br.access.log; + error_log /var/log/nginx/unifi.itguys.com.br.error.log warn; + + # ================================================================= + # INÍCIO: Configurações de SSL/TLS + # ================================================================= + ssl_certificate /etc/letsencrypt/live/unifi.itguys.com.br/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/unifi.itguys.com.br/privkey.pem; + ssl_trusted_certificate /etc/letsencrypt/live/unifi.itguys.com.br/fullchain.pem; + ssl_session_timeout 1d; + ssl_session_tickets off; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_prefer_server_ciphers off; + ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384'; + ssl_stapling on; + ssl_stapling_verify on; + # ================================================================= + # FIM: Configurações de SSL/TLS + # ================================================================= + + # ================================================================= + # INÍCIO: Cabeçalhos de Segurança + # ================================================================= + add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; + 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; + # CSP ajustada para UniFi, permitindo WebSockets (wss:) e fontes/imagens de domínios externos. + add_header Content-Security-Policy "default-src 'self'; connect-src 'self' wss:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://static.ui.com https://images.svc.ui.com; font-src 'self' data:; object-src 'none'; frame-ancestors 'self';" always; + # ================================================================= + # FIM: Cabeçalhos de Segurança + # ================================================================= + + # ================================================================= + # INÍCIO: Configurações de Compressão Brotli & Gzip + # ================================================================= + brotli on; + brotli_comp_level 6; + brotli_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss image/svg+xml; + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_proxied any; # UniFi fica atrás de um proxy, então comprimimos tudo. + gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss image/svg+xml; + gzip_disable "MSIE [1-6]\."; + # ================================================================= + # FIM: Configurações de Compressão + # ================================================================= + + # --- PARÂMETROS GERAIS DE PROXY --- + proxy_ssl_verify off; # Geralmente necessário para o backend do UniFi. + 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; + + # --- REGRAS DE ROTEAMENTO (LOCATIONS) --- + + # Rota para assets estáticos (CACHE AGRESSIVO) + location ~* \.(?:css|js|jpg|jpeg|gif|png|svg|ico|webp|woff2?|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + proxy_pass https://unifi_backend_web; + # Desativa o log para não poluir com assets + access_log off; + } + + # Rota DEDICADA para WEBSOCKETS + location /wss/ { + proxy_pass https://unifi_backend_web; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_read_timeout 86400; # Timeout longo para manter a conexão + } + + # Rota principal para a aplicação + location / { + proxy_pass https://unifi_backend_web; + # Cabeçalhos específicos que o UniFi pode precisar + proxy_set_header Origin ""; + proxy_set_header Referer $scheme://$host/; + } +} + +# ============================================================================== +# BLOCO 3: Servidor para o "Inform" dos Dispositivos (HTTP na porta 8080) +# ============================================================================== +server { + listen 8080; + listen [::]:8080; + server_name unifi.itguys.com.br; + + access_log /var/log/nginx/unifi-inform.log; + error_log /var/log/nginx/unifi-inform.error.log; + + location / { + proxy_set_header Host $http_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_read_timeout 600s; + proxy_send_timeout 600s; + proxy_pass http://unifi_backend_inform; + } +}