Nginx 重定向未按预期工作

Nginx 重定向未按预期工作

我想将所有 www 指向非 www,并将所有 http 指向 https。

我想出了下面的服务器块,但它不会重定向到非 www,并且所有内部页面都出现“ERR_CONNECTIN_CLOSED”。

server{
    listen 80;
    listen 443 ssl;
    server_name www.digitalreach.online;
    return 301 https://digitalreach.online$request_uri;
}

server {
    listen 80;
    server_name digitalreach.online;
    return 301 https://digitalreach.online$request_uri;
}

server{

    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/digitalreach.online/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/digitalreach.online/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    root /var/www/digitalreach.online/html/;
    index index.html index.htm;
    error_log /var/www/digitalreach.online/error.log;
    # Make site accessible from http://localhost/
    server_name digitalreach.online;

        location ~ /.well-known {
        allow all;
    }

    location / {
        try_files $uri $uri/ =404;

    }
}

我哪里做错了?

答案1

这在 https 上不起作用,要监听 443,您必须复制此块内的证书信息:

server{
    listen 80;
    listen 443 ssl;
    server_name www.digitalreach.online;
    return 301 https://digitalreach.online$request_uri;
}

然后对于第三块,您可能会遇到其他错误,请检查 nginx 默认错误日志(默认路径为 /var/log/nginx/error.log)或尝试$ nginx -t

相关内容