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..84174c2 --- /dev/null +++ b/nginx/sites-available/unifi.itguys.com.br.conf @@ -0,0 +1,123 @@ +# ============================================================================== +# ARQUIVO: /etc/nginx/sites-available/unifi.itguys.com.br.conf +# AUTOR: Gemini (Especialista NGINX) +# DATA: 21/09/2025 +# +# DESCRIÇÃO: +# Configuração de Proxy Reverso OTIMIZADA e SEGURA para UniFi Controller. +# +# FUNCIONALIDADES: +# - Bloco 1: Redirecionamento de HTTP (porta 80) para HTTPS. +# - Bloco 2: Proxy para a Interface Web (HTTPS na 443 -> HTTPS na 8443) +# - Suporte a WebSockets para a UI em tempo real. +# - Cache para assets estáticos da interface. +# - Lida com o certificado autoassinado do backend UniFi. +# - Bloco 3: Proxy para o "Inform" dos Dispositivos (HTTP na 8080 -> HTTP na 8080) +# - Permite que APs e Switches se comuniquem com o controlador através do proxy. +# - Acesso restrito à rede interna em todas as portas. +# ============================================================================== + +# Define o backend da interface web do UniFi (HTTPS). +upstream unifi_backend_web { + server 172.16.254.123:8443; +} + +# Define o backend da porta "inform" do UniFi (HTTP). +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; + + # Permite a validação do Let's Encrypt. + location /.well-known/acme-challenge/ { + root /var/www/html; + } + + # Redireciona todo o tráfego web para a versão segura. + 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; + + # --- CONFIGURAÇÕES DE SSL E SEGURANÇA --- + ssl_certificate /etc/letsencrypt/live/unifi.itguys.com.br/fullchain.pem; # managed by Certbot + ssl_certificate_key /etc/letsencrypt/live/unifi.itguys.com.br/privkey.pem; # managed by Certbot + include /etc/nginx/snippets/ssl_params.conf; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Frame-Options "SAMEORIGIN" always; + + # --- POLÍTICAS DE ACESSO E LOGS --- + include /etc/nginx/snippets/internal_networks.conf; + include /etc/nginx/snippets/global_robots.conf; + access_log /var/log/nginx/unifi.itguys.com.br.access.log; + error_log /var/log/nginx/unifi.itguys.com.br.error.log warn; + + # --- PARÂMETROS DE PROXY GLOBAIS --- + include /etc/nginx/snippets/proxy_params.conf; + # Essencial para o proxy funcionar com o backend HTTPS autoassinado do UniFi. + proxy_ssl_verify off; + + # --- REGRAS DE ROTEAMENTO (LOCATIONS) --- + + # 1. Rota para assets estáticos (CACHE AGRESSIVO) + location ~* ^/(static|v2|js|css|fonts|images)/ { + proxy_cache unifi_cache; + proxy_cache_valid 200 7d; + proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; + add_header X-Proxy-Cache $upstream_cache_status; + + proxy_pass https://unifi_backend_web; + } + + # 2. Rota principal para a aplicação (SEM CACHE, COM WEBSOCKETS) + location / { + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + proxy_pass https://unifi_backend_web; + } + + # --- Páginas de Erro Personalizadas --- + include /etc/nginx/snippets/custom_errors.conf; +} + +# ============================================================================== +# BLOCO 3: Servidor para o "Inform" dos Dispositivos (HTTP na porta 8080) +# ============================================================================== +server { + listen 8080; + listen [::]:8080; + server_name unifi.itguys.com.br; + + # --- POLÍTICAS DE ACESSO E LOGS --- + include /etc/nginx/snippets/internal_networks.conf; + access_log /var/log/nginx/unifi-inform.log; + error_log /var/log/nginx/unifi-inform.error.log; + + # --- Rota para o /inform --- + location / { + include /etc/nginx/snippets/proxy_params.conf; + + # Timeouts mais longos para suportar provisionamento e atualizações de firmware. + proxy_read_timeout 600s; + proxy_send_timeout 600s; + + proxy_pass http://unifi_backend_inform; + } +}