Nginx配置SSL证书

申请证书

FreeSSL.cn 一个提供免费HTTPS证书申请的网站

按照提示操作申请并验证域名后,

在证书下载弹出界面中,首先选择部署SSL证书的服务器,然后再选择“下载”,或者将证书文件发送到邮箱。

以部署到nginx的证书为例,下载得到一个压缩包,包含2个文件:

“.pem文件”:SSL证书文件
“.key文件”:SSL证书私钥文件

部署SSL证书

注意:在部署SSL证书之前,先要确保域名已经在添加了解析,并且可以通过http协议使用域名访问到目标网站。

以在nginx中部署SSL证书为例,将通过上述方式签发得到的SSL证书上传到nginx服务器,在nginx.conf配置文件中添加配置:

80端口负责由http强制跳转到https,443端口负责https的配置。

server {   
    listen       80;   
    server_name  jianz.xyz;   
    # 访问http时强制跳转到https   
    rewrite ^(.*)$  https://$host$1 permanent;
}

server
    {
        listen 443 ssl ;
        server_name www.jianz.xyz jianz.xyz;

    include enable-php-pathinfo.conf;

        # 根目录 博客
        index index.html index.htm index.php;
        root  /home/wwwroot/typecho_blog;

        charset utf-8;
        # SSL
        ssl_certificate  /usr/local/nginx/ssl/jianz.xyz/jianz.xyz_cert_chain.pem; 
        ssl_certificate_key  /usr/local/nginx/ssl/jianz.xyz/jianz.xyz_key.key; 
        ssl_session_timeout 5m;   
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   
        ssl_prefer_server_ciphers on;
        
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        # api
        location /api{
            proxy_pass http://127.0.0.1:8081;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /usr/local/nginx/logs/access.log;
    }

修改nginx.conf后记得重载

lnmp nginx reload

如果是阿里服务器记得在安全组规则中开启443端口

访问测试

  • 浏览器中输入 https://jianz.xyz 测试是否正常
  • 浏览器中输入 http://jianz.xyz 测试是否跳转到https

参考

https://baijiahao.baidu.com/s?id=1738462497295739137&wfr=spider&for=pc
https://blog.csdn.net/m0_52457734/article/details/123139998

本文链接: https://jianz.xyz/index.php/archives/320/

1 + 9 =
快来做第一个评论的人吧~