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的相关文件
上传证书
- 在云服务器上创建ssl目录,位置为/usr/local/nginx/ssl
- 将申请到的证书上传到该位置,共两个文件,一个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;
}
}
}
对于这个配置,有几个地方需要说明一下:
- listen 443 ssl; 监听443端口
- include /usr/local/nginx/conf/vhost/*.conf; 创建vhost目录,里面存放其他配置文件
- 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; - 请求转发 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,分如下几步
- 申请证书
- 将证书存放到代理机器上
- 不同代理,如Nginx、Apache、Tomcat,做对应的配置
- 验证
资料
- 快速签发 Let's Encrypt 证书指南https://www.cnblogs.com/esofar/p/9291685.html
- 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
- 域名型(DV)免费证书申请流程https://cloud.tencent.com/document/product/400/6814
- 后端服务轻松切换HTTPShttps://www.songma.com/news/txtlist_i25496v.html
- CentOS7安装Nginxhttps://www.cnblogs.com/boonya/p/7907999.html
- https://www.cnblogs.com/ghjbk/p/6744131.html ngx_http_ssl_module
最后
大家如果喜欢我的文章,可以关注我的公众号(程序员麻辣烫)
我的个人博客为:https://shidawuhen.github.io/
往期文章回顾:
技术
- Go通道实现原理
- Go定时器实现原理
- HTTPS连接过程
- 限流实现2
- 秒杀系统
- 分布式系统与一致性协议
- 微服务之服务框架和注册中心
- Beego框架使用
- 浅谈微服务
- TCP性能优化
- 限流实现1
- Redis实现分布式锁
- Golang源码BUG追查
- 事务原子性、一致性、持久性的实现原理
- CDN请求过程详解
- 常用缓存技巧
- 如何高效对接第三方支付
- Gin框架简洁版
- InnoDB锁与事务简析
- 算法总结
读书笔记
- 资治通鉴
- 敏捷革命
- 如何锻炼自己的记忆力
- 简单的逻辑学-读后感
- 热风-读后感
- 论语-读后感
- 孙子兵法-读后感
思考
- 项目流程管理
- 对项目管理的一些看法
- 对产品经理的一些思考
- 关于程序员职业发展的思考
- 关于代码review的思考
- Markdown编辑器推荐-typora