Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

291
Visualizações
www in domain return timeout, after I add redirect using regex in nginx

I have nginx and I want to redirect non-www to www:

So I did it using regex, but the redirect seems to works I get the redirect, but www return timeout.

How can I solve this?

upstream wwwapp {   
    least_conn;                                                                                  
    server www-app:3000 weight=10 max_fails=3 fail_timeout=30s;
}

server {
    server_name ~^(?!www\.)(?<domain>.+)$;
    return  301 $scheme://www.$domain$request_uri;
}

server {                                                                                                  
    listen       80;                                                                                      

    location / {                                                                                         
        proxy_pass http://wwwapp;                                                                     
        proxy_http_version 1.1;                                                                          
        proxy_set_header Upgrade $http_upgrade;                                                          
        proxy_set_header Connection 'upgrade';                                                           
        proxy_set_header Host $host;                                                                     
        proxy_cache_bypass $http_upgrade;                                                                
    }                                                                                                      
}  
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Easy.

server {                                                                                                  
    listen       80;                                                                                      

    if ($host ~ ^(?!www\.)(?<domain>.+)$) {
        return  301 $scheme://www.$domain$request_uri;
    }

    location / {                                                                                         
        proxy_pass http://wwwapp;                                                                     
        proxy_http_version 1.1;                                                                          
        proxy_set_header Upgrade $http_upgrade;                                                          
        proxy_set_header Connection 'upgrade';                                                           
        proxy_set_header Host $host;                                                                     
        proxy_cache_bypass $http_upgrade;                                                                
    }                                                                                                      
} 
over 4 years ago · Santiago Trujillo Relatório

0

Your second server block does not have a server_name so it is unlikely to get selected to process any requests.

The first server block is the default and will get selected even if the regex does not match.

You should change the second server block, and either add a server_name statement or make it the default_server.

See this document for details.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda