环境说明

阿里云 CentOS 7.6
Nginx 使用 yum install nginx -y 方式安装,版本为 1.12.2
使用 certbot 配置 www.domain.com 支持 https 访问,并选择 2: Redirect

目标是访问 https://domain.comhttp://domain.comhttp://www.domain.com 时全部跳转到 https://www.domain.com,统一网站对外入口。

配置步骤

配置 http 和不带 www 的访问统一重定向到 https://www.domain.com
在 certbot 生成配置的基础上添加 domain.com域名。

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

    listen 80;
    server_name  www.domain.com domain.com;
    return 404; # managed by Certbot
}

使用 certbot 为 domian.com 配置 https,注意询问 Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. 时选择 1: No redirect

certbot

检查配置并重启 nginx

nginx -t
service nginx restart

标签: CentOS, Nginx, HTTPS