Nginx redirigir todo el tráfico de HTTPS y sin www

votos
0

tengo lo que pensaba que era una cuestión simple pero no puedo entenderlo. Mi objetivo es tener HTTPS sin www.

No HTTPS tráfico está siendo redirigido correctamente, pero el último no puedo averiguar es redirigir el tráfico HTTPS a HTTPS www sin www.

Working:
http://example.com -> https://example.com  
http://www.example.com -> https://example.com 
https://example.com (no redirect needed)

Not Working:
https://www.example.com -> https://example.com (not working)

server {

        root /var/www/example.com/;
        index index.php index.html index.htm;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {

    if ($host = www.example.com) {
        return 301 https://example.com$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://example.com$request_uri;
    } # managed by Certbot
        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;
    return 404; # managed by Certbot
}
Publicado el 13/01/2020 a las 23:52
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
0

Así que me estaba perdiendo la sentencia if en el bloque de servidor SSL

Adicional:

if ($host = www.example.com) {
        return 301 https://example.com$request_uri;
    } # managed by Certbot
Respondida el 14/01/2020 a las 00:51
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more