Correct way to install SSL certificate in nginx

王林
Release: 2020-11-18 16:33:32
forward
5035 people have browsed it

Correct way to install SSL certificate in nginx

前提条件:

已经生成了以下文件:

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;

    #其他的配置信息···
}
Copy after login

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

安装后重启nginx使其生效

如果重启nginx

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

nginx -V
Copy after login

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

./configure --with-http_stub_status_module --with-http_ssl_module
make && make install
Copy after login

强制跳转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;
    }
}
Copy after login

The above is the detailed content of Correct way to install SSL certificate in nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!