HTTPS配置实战

原因

现在网站使用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

相关文章

nginx 代理转发 http https 基本用法

nginx 配置http最简单的用法;server{ # 监听的端口 listen 80; # 绑定的域名,多个用空格分开即可。 server_name prvt.c...

nginx 服务器配置https时如何重定向http

为了网络访问的安全性,https被更多的网站所使用,现在开发App,微信小程序都固定为使用https的访问,让访问更安全,但同时也得处理http请求,使它重定向,方便官网的访问。nginx 服务器配置...

nginx配置https详解

要在Nginx上配置HTTPS,需要进行以下步骤:获取证书和密钥文件 在使用HTTPS之前,需要先获取SSL证书和密钥文件。可以通过购买证书或者使用自签名证书来获取,证书和密钥文件可以通过各种方式获取...

Nginx: 最常见的 2 种 http to https 跳转场景

Nginx: 最常见的 2 种 http to https 跳转场景建议点击 查看原文 查看最新内容。原文链接: https://typonotes.com/posts/2023/08/28/ngin...

nginx配置https的详细流程

1、下载SSL证书2、两个证书放在cert目录上然后放到nginx 与nginx.conf同目录下3、去nginx解压目录下执行./configure --with-http_ssl_module如果...

如果你搞个网站要http访问又要https访问咋个搞?

实际工作中不知道你遇没遇到这种问题:客户要求微信公众号菜单的配置链接采用https访问微信的接口对接自己服务器采用的是http访问然后只给了一个端口8088这种能不能实现http能够访问到服务内容,h...