• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

nginx安装SSL证书的正确方法

武飞扬头像
PHP中文网
帮助63

前提条件:

已经生成了以下文件:

domain.com.crt (域名证书) 有的证书可能是cer 或 pem 或其他后缀名,都可以

domain.com.key (私钥文件)

(相关教程:nginx教程

配置Nginx

找到站点的配置文件,在server中添加443端口监听和证书文件引用

server {
    listen 80;
    #监听443端口(必须)
    listen 443 ssl;
    
    server_name domain.com www.domain.com;
    index index.html index.php index.htm;
    root /www/wwwroot/domain.com;

    #引用证书(必须,放在conf/ssl目录下可以用相对路径,其他位置用绝对路径)
    ssl_certificate     ssl/domain.com.crt;
    ssl_certificate_key ssl/domain.com.key;

    #协议优化(可选,优化https协议,增加安全性)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    #其他的配置信息···
}

以上为最基本的配置,其他参数请根据生产环境需要添加

安装后重启nginx使其生效

如果重启nginx

失败使用如下命令检测nginx是否支持SSL

nginx -V

如果有显示 –with-http_ssl_module 表示已编译openssl,支持安装ssl,仔细检查配置文件
如果没有安装请下载nginx源码重新编译

./configure --with-http_stub_status_module --with-http_ssl_module
make && make install

强制跳转HTTPS

在配置文件中添加跳转代码

server {
    #站点和SSL的配置信息···

    #自动跳转到HTTPS(可选,和下面的部分域名跳转不能同时使用)
    if ($server_port = 80){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }

    #绑定域名较多,只让部分域名跳转(根据情况选用,和上面的全部跳转不能同时使用)
    set $redirect_https 1;
    if ($server_port = 80) {
        set $redirect_https "${redirect_https}2";
    }
    if ($http_host = 'abc.com') {
        set $redirect_https "${redirect_https}3";
    }
    if ($http_host = 'cde.com') {
        set $redirect_https "${redirect_https}3";
    }
    if ($redirect_https = "123") {
        #当前域名跳转
        rewrite ^(.*)$ https://$host$1 permanent;
        #可以跳转到指定的域名
        #rewrite ^(.*)$ https://www.abcde.com$1 permanent;
    }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanffkai
系列文章
更多 icon
同类精品
更多 icon
继续加载