微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx 安装 ssl 证书,配置 https ,使用 Certbot 客户端免费配置 ssl 证书centos7 下可以,其他版本自行测试

* Nginx 配置 ssl 证书前需要先安装 SSL 模块,本次操作是在已经安装 Nginx 的基础上配置,先安装 SSL 模块,来到 Nginx 源码目录,我的是这里,执行进入到自己的安装目录:

cd /usr/local/Nginx/Nginx-1.8.0

执行语句,重新安装 ssl 模块

./configure --prefix=/usr/local/Nginx --with-http_stub_status_module --with-http_ssl_module

如果报 error,一般是没有安装 openssl openssl-devel 导致的,先安装 openssl openssl-devel(没有报错直接跳过)

openssl openssl-devel

然后再:(不可 install )

make

等待执行完成后,我们需要把新编译的Nginx模块替换原来的Nginx,先备份(记得看好自己的安装路径

cp /usr/local/Nginx/sbin/Nginx /usr/local/Nginx/sbin/Nginx.bak

关闭Nginx(因为要把新的模块覆盖旧的Nginx

可以直接:ps aux | grep Nginx
查看端口后:kill -9 xxxx

关闭Nginx进程后就可以开始替换了(注意:我当前的位置是在我Nginx的源码包中,目录不要搞错了)

cp ./objs/Nginx /usr/local/Nginx/sbin/

先切换到sbin目录,检测Nginx配置文件是否有错误

cd /usr/local/Nginx/sbin/
./niginx -t

 

 

 看到这个就是表示 ssl 模块安装成功。

========================

开始使用 Let’s Encrypt 安装证书:

安装 Certbot 客户端:

yum install -y epel-release
yum install -y certbot

因为使用 Certbot 获取证书时,Let's Encrypt 服务器会访问 http://sub.domain.com/.well-kNown 来验证你的域名服务器,因此需要修改 Nginx 配置文件,配置 .well-kNown 指向本地一个目录:

server {
    
    ......

    location /.well-kNown {
        alias /usr/local/Nginx/html/.well-kNown;
    }

    ......
}

然后就可以使用 certbot 命令来获取证书了,获取证书时需要输入你的Email并接受用户条款。需要注意:-w 指定的 web 目录需要和前边 Nginx 配置的 .well-kNown 的本地目录一致(/usr/local/Nginx/html):

#记得把 sub.domain.com 替换成自己的域名;同时把  [email protected] 替换成自己的邮箱
certbot certonly --webroot -w /usr/local/Nginx/html/ -d sub.domain.com -m [email protected] --agree-tos

-w 指定 webroot 目录
-d domain 想要获取的证书域名,支持多个域名
但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另外一种模式 --standalone,这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如Nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。

certbot certonly --standalone -d sub.domain.com -m [email protected] --agree-tos  //记得替换域名和邮箱

如果成功获取证书,你的密钥和证书存放在 /etc/letsencrypt/live/sub.domain.com/ 目录(sub.domain.com 目录是自己的域名),可以看到如下:

ll /etc/letsencrypt/live/sub.domain.com/

cert.pem -> ../../archive/sub.domain.com/cert1.pem
chain.pem -> ../../archive/sub.domain.com/chain1.pem
fullchain.pem -> ../../archive/sub.domain.com/fullchain1.pem
privkey.pem -> ../../archive/sub.domain.com/privkey1.pem

有时需要删除生成的证书,重新生成。可使用如下命令进行删除

certbot delete --cert-name sub.domain.com  //记得替换自己的域名

生成 dhparam(这一步花费时间比较多,而且有时候会失败,报找不到 sites-enabled 目录,建议提前在 /etx/Nginx/ 目录下新建好 sites-enable 目录)

openssl dhparam -out /etc/Nginx/sites-enabled/dh4096.pem 4096

做完以上步骤后,在我们:/usr/local/Nginx/conf   和  /etc/Nginx  目录下,都会有 conf 目录下的相同文件,如下:

 

 

通常我们配置 /usr/local/Nginx/conf 下的即可,/etc/Nginx 下的也会同步更新

接下来开始配置我们的 Nginx.conf 文件

server {
    listen 80;
    server_name sub.domain.com;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen 443 ssl;

    server_name sub.domain.com;

    include /etc/Nginx/sites-enabled/sub.domain.com.ssl;

    location / { try_files $uri @proxy_to_app; }
    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:8080;
    }
}

sub.domain.com.ssl 文件配置内容

ssl on;
ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/Nginx/sites-enabled/dhparam.pem;  //这里的 dhparam.pem 去目录下自己查看,替换一下
ssl_ciphers HIGH:!ADH:!MD5:!aNULL:!eNULL:!MEDIUM:!LOW:!EXP:!kEDH;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
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;

之后重启 Nginx ,到此 Nginx 配置 https 完成。

启动:/usr/local/Nginx/sbin/Nginx
停止:/usr/local/Nginx/sbin/Nginx -s stop
重启:/usr/local/Nginx/sbin/Nginx -s reload

如果配置了服务:(可以看我上一篇文章怎么配置服务)

启动:systemctl start Nginx.service
停止:systemctl stop Nginx.service
重启:systemctl restart Nginx.service

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐