HTTPS配置实战

HTTPS配置实战

精选文章moguli202025-01-14 10:54:3513A+A-

原因

现在网站使用HTTPS是规范操作之一,前些日子买了腾讯云服务,同时申请了域名http://www.asap2me.top/,目前该域名只支持HTTP,想升级为HTTPS。关于HTTPS的链接过程大家可以看我的这篇文章HTTPS连接过程。

使用http访问是正常的:

使用https访问报错:

环境

目前环境是:Centos7.6

Web服务:GoLang

Web服务端口号:8080

操作

因为Web服务是GoLang写的,所以思路是搭建Nginx反向代理,Nginx上配置证书,将请求代理到Go服务上。

申请证书

申请证书的方案很多,可以从Let's Encrypt申请,因为我的域名是从腾讯云上买的,所以直接在腾讯云上申请免费证书

位置:https://console.cloud.tencent.com/ssl

查看域名验证状态,一般需要5分钟左右才能通过,如果报错,可以过会重新验证,验证状态成功后

申请完成后,可以下载证书

安装Nginx

安装Nginx的过程中,要记得把ssl模块一起安装上,否则会报错nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install

安装完毕后,在/usr/local/nginx/目录下会有nginx的相关文件

上传证书

  1. 在云服务器上创建ssl目录,位置为/usr/local/nginx/ssl
  2. 将申请到的证书上传到该位置,共两个文件,一个crt,一个key

Nginx配置

编辑Nginx配置,nginx.conf

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    include     /usr/local/nginx/conf/vhost/*.conf;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
               listen 443 ssl;
        server_name  www.asap2me.top;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
              ssl_certificate         /usr/local/nginx/ssl/asap2me.crt;
              ssl_certificate_key     /usr/local/nginx/ssl/asap2me.key;
            ssl_session_cache  shared:SSL:10m;
              ssl_session_timeout  5m;
            ssl_protocols  TLSv1.2 TLSv1.1 TLSv1;
       ssl_ciphers  HIGH:!aNULL:!MD5;
           ssl_prefer_server_ciphers   on;
       # location / {
       #     root   html;
       #     index  index.html index.htm;
       # }
                location / {
                   proxy_set_header Host  $host;
               proxy_set_header   X-Real-IP        $remote_addr;
             proxy_set_header X-Forwarded-For  $http_x_forwarded_for;
                proxy_pass http://go_backend;
               }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
}

对于这个配置,有几个地方需要说明一下:

  1. listen 443 ssl; 监听443端口
  2. include /usr/local/nginx/conf/vhost/*.conf; 创建vhost目录,里面存放其他配置文件
  3. ssl配置 ssl_certificate /usr/local/nginx/ssl/asap2me.crt;
    ssl_certificate_key /usr/local/nginx/ssl/asap2me.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
  4. 请求转发 location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $http_x_forwarded_for;
    proxy_pass http://go_backend;
    }

go_backend的配置如下,go_back_upstream.conf

upstream go_backend{
    server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=10s;
}

验证

现在让我们验证一下效果:

http正常

https正常

总结

将http升级为https,分如下几步

  1. 申请证书
  2. 将证书存放到代理机器上
  3. 不同代理,如Nginx、Apache、Tomcat,做对应的配置
  4. 验证

资料

  1. 快速签发 Let's Encrypt 证书指南https://www.cnblogs.com/esofar/p/9291685.html
  2. HTTPS 免费证书申请和应用https://cloud.tencent.com/document/product/627/41141?from=information.detail.%E8%85%BE%E8%AE%AF%E4%BA%91%E4%B8%8B%E8%BD%BDhttps%E8%AF%81%E4%B9%A6
  3. 域名型(DV)免费证书申请流程https://cloud.tencent.com/document/product/400/6814
  4. 后端服务轻松切换HTTPShttps://www.songma.com/news/txtlist_i25496v.html
  5. CentOS7安装Nginxhttps://www.cnblogs.com/boonya/p/7907999.html
  6. https://www.cnblogs.com/ghjbk/p/6744131.html ngx_http_ssl_module

最后

大家如果喜欢我的文章,可以关注我的公众号(程序员麻辣烫)

我的个人博客为:https://shidawuhen.github.io/

往期文章回顾:

技术

  1. Go通道实现原理
  2. Go定时器实现原理
  3. HTTPS连接过程
  4. 限流实现2
  5. 秒杀系统
  6. 分布式系统与一致性协议
  7. 微服务之服务框架和注册中心
  8. Beego框架使用
  9. 浅谈微服务
  10. TCP性能优化
  11. 限流实现1
  12. Redis实现分布式锁
  13. Golang源码BUG追查
  14. 事务原子性、一致性、持久性的实现原理
  15. CDN请求过程详解
  16. 常用缓存技巧
  17. 如何高效对接第三方支付
  18. Gin框架简洁版
  19. InnoDB锁与事务简析
  20. 算法总结

读书笔记

  1. 资治通鉴
  2. 敏捷革命
  3. 如何锻炼自己的记忆力
  4. 简单的逻辑学-读后感
  5. 热风-读后感
  6. 论语-读后感
  7. 孙子兵法-读后感

思考

  1. 项目流程管理
  2. 对项目管理的一些看法
  3. 对产品经理的一些思考
  4. 关于程序员职业发展的思考
  5. 关于代码review的思考
  6. Markdown编辑器推荐-typora
点击这里复制本文地址 以上内容由莫古技术网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

莫古技术网 © All Rights Reserved.  滇ICP备2024046894号-2