AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / server / Perguntas / 1083283
Accepted
AtomX
AtomX
Asked: 2021-11-12 10:42:35 +0800 CST2021-11-12 10:42:35 +0800 CST 2021-11-12 10:42:35 +0800 CST

Como resolver o redirecionamento não www para www Nginx?

  • 772

Esta é a configuração do meu servidor Web NGinx

    server {
        if ($host ~ ^[^.]+\.betafox\.net$) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = www.betafox.net) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = betafox.net) {
            return 301 https://$host$request_uri; 
        } # managed by Certbot
    
        listen 80;
        listen [::]:80;
    
        #server_name _;
        root /var/www/html;
    
    server_name betafox.net *.betafox.net;
        #return 301 https://$host$request_uri;
        index index.php index.html index.htm;
        location / {
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            proxy_pass  https://betafox.net/;
            proxy_redirect  https://betafox.net/ $host;
            proxy_set_header Accept-Encoding "";
            proxy_ssl_server_name on;
        }
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            #fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/run/php/php8.0-fpm.sock;
            fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
     }
    
    
    }
    
    server {
    
    listen 443 ssl default_server;
            listen [::]:443 ssl default_server;
    
            root /var/www/html;
            index index.php index.html index.htm;
    
          # server_name _;
            server_name betafox.net *.betafox.net;
            # Maximum file upload size is 4MB - change accordingly if needed
            client_max_body_size 512M;
            client_body_buffer_size 128k;
            include snippets/letsencrypt-nginx-certs.conf;
            include snippets/letsencrypt-nginx-route.conf;
    
            location / {
                    # try_files $uri $uri/ =404;
                    try_files $uri $uri/ /index.php?q=$uri&$args;
            }
    error_page 404 /404.html;
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    #fastcgi_pass 127.0.0.1:9000;
                    #fastcgi_pass unix:/var/run/php8.0-fpm.sock;
                    fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
            }
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    
    }

A maior parte foi modificada automaticamente pelo Certbot quando instalei certificados SSL para meu FQDN e subdomínios. O problema que estou enfrentando é sobre o redirecionamento de URL. A URL original é www.betafox.net , quando o usuário digita betafox.net é redirecionado para https://betafoxnet.www.betafox.net/ e há uma mensagem que diz: O site que você estava procurando, não existe.

Eu só quero que todos os usuários que digitem betafox.net sejam encaminhados para www.betafox.net . Eu acredito que o Nginx poderia fazer isso. Como posso conseguir tal coisa?

linux redirect nginx
  • 1 1 respostas
  • 56 Views

1 respostas

  • Voted
  1. Best Answer
    Tero Kilkanen
    2021-11-15T06:59:59+08:002021-11-15T06:59:59+08:00

    Infelizmente, o Certbot cria os redirecionamentos nginx usando ifa $hostvariável que é problemática.

    É melhor ter o redirecionamento em uma serverseção separada como segue.

    # Redirect all requests to betafox.net URLs to corresponding www.betafox.net URLs
    server {
        listen 80;
        listen 443 ssl http2;
    
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    
        server_name betafox.net;
    
        return 301 https://www.betafox.net$request_uri;
    }
    
    # Redirect all other subdomain HTTP requests to HTTPS.
    server {
        listen 80;
    
        server_name *.betafox.net;
    
        return 301 https://$http_host$request_uri;:
    }
    
    # Removed the server block for port 80, it looked meaningless
    
    server {
        # Removed the default_server, default_server should not be the actual website
        listen 443 ssl;
        listen [::]:443 ssl;
    
        root /var/www/html;
        index index.php index.html index.htm;
    
        server_name betafox.net *.betafox.net;
        # Maximum file upload size is 4MB - change accordingly if needed
        client_max_body_size 512M;
        client_body_buffer_size 128k;
        include snippets/letsencrypt-nginx-certs.conf;
        include snippets/letsencrypt-nginx-route.conf;
    
        location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }
        error_page 404 /404.html;
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
        }
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    }
    
    • 1

relate perguntas

  • Protegendo um novo servidor Ubuntu [fechado]

  • (Soft) RAID 6 no Ubuntu 7.10, devo migrar para 8.10?

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Você pode passar usuário/passar para autenticação básica HTTP em parâmetros de URL?

    • 5 respostas
  • Marko Smith

    Ping uma porta específica

    • 18 respostas
  • Marko Smith

    Verifique se a porta está aberta ou fechada em um servidor Linux?

    • 7 respostas
  • Marko Smith

    Como automatizar o login SSH com senha?

    • 10 respostas
  • Marko Smith

    Como posso dizer ao Git para Windows onde encontrar minha chave RSA privada?

    • 30 respostas
  • Marko Smith

    Qual é o nome de usuário/senha de superusuário padrão para postgres após uma nova instalação?

    • 5 respostas
  • Marko Smith

    Qual porta o SFTP usa?

    • 6 respostas
  • Marko Smith

    Linha de comando para listar usuários em um grupo do Windows Active Directory?

    • 9 respostas
  • Marko Smith

    O que é um arquivo Pem e como ele difere de outros formatos de arquivo de chave gerada pelo OpenSSL?

    • 3 respostas
  • Marko Smith

    Como determinar se uma variável bash está vazia?

    • 15 respostas
  • Martin Hope
    Davie Ping uma porta específica 2009-10-09 01:57:50 +0800 CST
  • Martin Hope
    kernel O scp pode copiar diretórios recursivamente? 2011-04-29 20:24:45 +0800 CST
  • Martin Hope
    Robert ssh retorna "Proprietário incorreto ou permissões em ~/.ssh/config" 2011-03-30 10:15:48 +0800 CST
  • Martin Hope
    Eonil Como automatizar o login SSH com senha? 2011-03-02 03:07:12 +0800 CST
  • Martin Hope
    gunwin Como lidar com um servidor comprometido? 2011-01-03 13:31:27 +0800 CST
  • Martin Hope
    Tom Feiner Como posso classificar a saída du -h por tamanho 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich O que é um arquivo Pem e como ele difere de outros formatos de arquivo de chave gerada pelo OpenSSL? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent Como determinar se uma variável bash está vazia? 2009-05-13 09:54:48 +0800 CST

Hot tag

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve