美文网首页
Nginx 配置HTTPS

Nginx 配置HTTPS

作者: touch_The_Sky | 来源:发表于2020-04-06 20:44 被阅读0次

安装Nginx

Nginx配置SSL首先要保证安装的时候安装的有SSL模块
如果没有的话打开ssl会报如下错误

 [nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.co]

安装nginx

#如果之前有安装过的,想把之前的配置复制过来,可以在nginx目录下使用此命令查看之前的配置
/usr/local/nginx/sbin/nginx -V
#下载-->解压nginx 然后进入源码目录执行如下命令进行安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install

make install完毕会在我们指定的 /usr/local/nginx目录出现编译后的nginx

尝试启动nginx

/usr/local/nginx/sbin/nginx 

发现报错:

nginx: [emerg] BIO_new_file("/usr/local/nginx/conf/cert.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/usr/local/nginx/conf/cert.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)

这说明ssl模块已经有了, 只不过我们还没有给nginx配置证书而已

配置证书

阿里云为例:
控制台==>域名==>选中你的域名==>管理==>开启SSL==>购买证书页面(没钱不买,点左上角"回到证书列表")==>点击"购买证书"==>选择免费版(个人) 免费版只可以给单域名使用一年,对个人来说足够了

  • PS: (购买后一般几分钟就能签发下来)

==>回到证书列表
找到刚才购买的证书点击下载, 选择你使用证书的方式(推荐使用Nginx,省得换个Tomcat就得重新部署一遍证书)


image.png

==>将证书放在你的Nginx所在服务器上, 比如我 放在/root/cert目录下
==>配置nginx.conf 告诉Nginx证书所在位置

   server {
        listen       443 ssl; #https默认端口
        server_name  www.sourcecoder.cn; #证书绑定的域名

        ssl_certificate      /root/cert/3710808_www.sourcecoder.cn.pem; #证书位置
        ssl_certificate_key  /root/cert/3710808_www.sourcecoder.cn.key;#证书key位置

        ssl_session_cache    shared:SSL:1m; 
        ssl_session_timeout  5m;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  #加密协议配置

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;    #加密套件配置
        ssl_prefer_server_ciphers  on;#打开

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

#将80端口的请求转发到443
 server {
    listen       80;
    server_name  www.sourcecoder.cn;#填写绑定证书的域名
    rewrite ^ https://$http_host$request_uri? permanent;    # 将http转到https
 }

加密协议的配置有特殊兼容需求的可以参考:http://f2ex.cn/properly-enable-http2-support/

==>重启Nginx

nginx -t #测试配置是否有误
service nginx restart

==>没有问题的话访问你的域名,发现已经是Https协议了! 成功!


image.png

相关文章

  • # nginx https证书配置

    nginx https证书配置 一 前言 此文档针对于nginx配置反向代理使用https证书方法 nginx作为...

  • nginx https 配置

    nginx https 配置

  • nginx配置https

    nginx配置https https需要的证书我们已经申请到了,下面分享下nginx配置https的一些配置参数!...

  • Nginx配置https请求,以及Nginx+keepalive

    一、Nginx配置https请求 要实现Nginx配置https请求,安装的时候需要加上 --with-http_...

  • nginx配置https

    只配置443会导致http和https共存,只要再80里配置个重定向即可return 301 https://$s...

  • nginx https配置

    本地文件上传到服务器

  • Nginx https 配置

    准备环境阿里云 腾讯云部署好站点,且使用DNS解析到云服务器的IP地址 接下来就是配置HTTPS的关键步...

  • Nginx配置HTTPS

  • Nginx 配置 https

    从云服务提供商处申请证书 申请 https 证书教程-百度经验 申请下来的证书目录结构 下文中只拿 Nginx 来...

  • NGINX 配置 HTTPS

    首先是下载证书,我的是再阿里云里面,如果购买成功了,直接下载证书。里面会有两后缀分别为.key和.pem的个文件,...

网友评论

      本文标题:Nginx 配置HTTPS

      本文链接:https://www.haomeiwen.com/subject/zutlphtx.html