# 🔧 Correção do Erro 500 - Problema de Configuração Nginx

## 🚨 Problema Identificado

O erro 500 é causado por um **loop de redirecionamento no Nginx**:
```
rewrite or internal redirection cycle while internally redirecting to "/index.html"
```

Isso significa que o Nginx está tentando redirecionar para `/index.html` infinitamente.

---

## 🔍 Diagnóstico

Execute no servidor:

```bash
# Ver configuração do Nginx
sudo cat /etc/nginx/sites-available/gestor.jf.eng.br
# ou
sudo cat /etc/nginx/sites-enabled/gestor.jf.eng.br

# Ver logs do Nginx em tempo real
sudo tail -f /var/log/nginx/error.log
```

---

## ✅ Solução

### **Opção 1: Verificar e Corrigir Configuração do Nginx**

A configuração do Nginx deve ser algo assim:

```nginx
server {
    listen 80;
    listen [::]:80;
    server_name gestor.jf.eng.br;
    
    # Redirecionar HTTP para HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name gestor.jf.eng.br;

    root /var/www/gestorstream/frontend/dist;
    index index.html;

    # SSL configuration
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    # API Laravel
    location /api {
        try_files $uri $uri/ /index.php?$query_string;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/gestorstream/backend/public/index.php;
        include fastcgi_params;
    }

    # Frontend React (SPA)
    location / {
        try_files $uri $uri/ /index.html;
    }

    # Evitar loop - não redirecionar index.html novamente
    location = /index.html {
        internal;
    }
}
```

### **Opção 2: Comando Rápido para Testar**

```bash
# Testar configuração do Nginx
sudo nginx -t

# Se estiver OK, recarregar
sudo systemctl reload nginx
```

---

## 🔧 Correção Rápida

Execute no servidor:

```bash
# 1. Ver configuração atual
sudo cat /etc/nginx/sites-enabled/gestor.jf.eng.br

# 2. Fazer backup
sudo cp /etc/nginx/sites-enabled/gestor.jf.eng.br /etc/nginx/sites-enabled/gestor.jf.eng.br.backup

# 3. Editar configuração
sudo nano /etc/nginx/sites-enabled/gestor.jf.eng.br

# 4. Procurar por linhas como:
#    try_files $uri $uri/ /index.html;
#    E garantir que não há loop

# 5. Testar configuração
sudo nginx -t

# 6. Se OK, recarregar
sudo systemctl reload nginx
```

---

## 📝 Configuração Correta (Exemplo Completo)

```nginx
server {
    listen 443 ssl http2;
    server_name gestor.jf.eng.br;

    root /var/www/gestorstream/frontend/dist;
    index index.html;

    # SSL
    ssl_certificate /etc/letsencrypt/live/gestor.jf.eng.br/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/gestor.jf.eng.br/privkey.pem;

    # API Laravel
    location /api {
        alias /var/www/gestorstream/backend/public;
        try_files $uri $uri/ @laravel;
        
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/gestorstream/backend/public/index.php;
            include fastcgi_params;
        }
    }

    location @laravel {
        rewrite /api/(.*)$ /api/index.php?/$1 last;
    }

    # Frontend SPA
    location / {
        try_files $uri $uri/ /index.html;
    }

    # Evitar processar index.html como PHP
    location = /index.html {
        add_header Cache-Control "no-cache, no-store, must-revalidate";
    }
}
```

---

## 🚨 Erro Secundário (CheckSubscriptionExpiration)

Há também um erro no comando de verificação de expiração de assinaturas. A coluna `active` não existe na tabela `subscriptions`.

**Correção temporária:** Desabilitar o comando até corrigir:

```bash
# Ver crontab
sudo crontab -l

# Comentar a linha do CheckSubscriptionExpiration temporariamente
```

---

## ✅ Verificação Pós-Correção

```bash
# 1. Testar Nginx
sudo nginx -t

# 2. Recarregar Nginx
sudo systemctl reload nginx

# 3. Testar no navegador
curl -I https://gestor.jf.eng.br

# 4. Ver logs
sudo tail -f /var/log/nginx/error.log
```

---

## 📞 Se Ainda Não Funcionar

1. **Verificar se o frontend está buildado:**
   ```bash
   ls -la /var/www/gestorstream/frontend/dist/
   ```

2. **Verificar permissões:**
   ```bash
   sudo chown -R www-data:www-data /var/www/gestorstream/frontend/dist
   ```

3. **Verificar se o PHP-FPM está rodando:**
   ```bash
   sudo systemctl status php8.2-fpm
   ```

4. **Ver logs do PHP-FPM:**
   ```bash
   sudo tail -f /var/log/php8.2-fpm.log
   ```

