如何使用 HTTPS 和 nginx 的 certbot 将 www 重定向到非 www

How to redirect www to non-www using certbot for HTTPS & nginx

我正在尝试将 www 重定向到非 www,但它不起作用。我已经尝试过类似问题的各种答案,但这些都没有用。

我有 SSL 证书,使用 certbot 为 3 个域 example.comwww.example.comadmin.example.com

这是我当前的配置,适用于非 www 和管理员,但是 www.example.com 不起作用。

# HTTP - redirect all requests to HTTPS
server {
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}

# Redirect to non-www
server {
    server_name www.example.com;
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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
    return 301 https://example.com$request_uri;
}

# non-www
server {

    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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

}
# CMS
server {

    server_name admin.example.com;

    location / {
        proxy_pass http://localhost:1337;
        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;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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
}

我使用 DigitalOcean,其中 admin 和非 www 都指向我的 droplet,并且 www.example.com 有一条指向 example.com(非 www)的 CNAME 记录。

首先,www.example.com 和 example.com 应该在一个服务器块中。

其次,您需要在 #non-www 服务器配置博客中添加此内容

if ($host = 'www.example.com') {
        return 301 https://example.com$request_uri;
}

第三,要将所有请求重定向到 HTTPS,必须在 # HTTP - redirect all requests to HTTPS 块中添加 server_name

最后,您的 NGINX 配置文件将如下所示

# HTTP - redirect all requests to HTTPS
server {
    server_name example.com www.example.com admin.example.com;
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}

# non-www
server {

    server_name example.com www.example.com;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }

    if ($host = 'www.example.com') {
        return 301 https://example.com$request_uri;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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

}
# CMS
server {

    server_name admin.example.com;

    location / {
        proxy_pass http://localhost:1337;
        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;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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
}

更新 NGINX 配置文件后,重新启动 NGINX:

$ sudo systemctl restart nginx