Nginx redirect al het verkeer naar HTTPS en non-www

stemmen
0

wat ik dacht was een eenvoudige kwestie, maar kan niet het uit. Mijn doel is om HTTPS hebben non-www.

NON-HTTPS-verkeer correct wordt doorgestuurd, maar de laatste die ik kan niet achterhalen is om redirect HTTPS www verkeer naar HTTPS non-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
}
De vraag is gesteld op 13/01/2020 om 23:52
bron van user
In andere talen...                            


1 antwoorden

stemmen
0

Dus ik miste de if-statement in de SSL server block

Toegevoegd:

if ($host = www.example.com) {
        return 301 https://example.com$request_uri;
    } # managed by Certbot
antwoordde op 14/01/2020 om 00:51
bron van user

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