1、前提工作
1.1 准备一台具有公网IP的Linux服务器
1.2 申请一个域名,并将此域名和服务器的公网IP绑定
1.3 在阿里开发者平台上申请并获取SSL证书
(1)在开启SSL后,来到下图所示页面,选择一个证书类型后,不用管他上面的报价,点击申请。

(2)在接下来的这个页面中,选择你想要的证书服务类型

(3)购买成功之后,跟随系统的引导完成证书的签发之后点击下载SSL证书。这里我下载了Nginx。证书有两个,一个是证书文件 *.pem
,另一个是证书密钥文件 *.key
。

1.4 在服务器上安装Nginx服务器
2、配置SSL证书
登录Nginx服务器,在Nginx安装目录(默认Nginx安装目录为/usr/local/nginx/conf)下创建cert目录,并将下载的证书文件和密钥文件拷贝到cert目录中。

上传好证书和秘钥文件后,我们打开改Nginx安装目录/conf/nginx.conf文件。找到以下配置信息:

按照下文中注释内容修改nginx.conf文件:
http{
#其他配置省略
upstream tomcatServer{
server 121.36.xx.xx:8080;
server 121.36.xx.xx:8081;
}
server {
listen 80;
server_name easyblog.top;
rewrite ^(.*)$ https://$server_name$1 permanent; #http请求转换到https
}
# 以下属性中以ssl开头的属性代表与证书配置有关,其他属性请根据自己的需要进行配置。
server{
listen 443 ssl; #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动
server_name www.easyblog.top; #配置你的域名
root html;
index index.html index.htm;
ssl_certificate /usr/local/nginx/conf/cert/3622141_www.easyblog.top.pem; #SSL证书
ssl_certificate_key /usr/local/nginx/conf/cert/3622141_www.easyblog.top.key; #SSL证书秘钥
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
#禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
server_tokens off;
#如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问
#fastcgi_param HTTPS on;
#fastcgi_param HTTP_SCHEME https;
#access_log /usr/local/nginx/logs/httpsaccess.log;
#把以前在80端口的配置迁移过来
location / {
proxy_pass http://tomcatServer;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
修改后保存之后别急着重启,先检查一下配置是否正确:
[root@ecs-sn3-medium-2-linux-20191128162047 conf]# ../sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
没问题后再重启Nginx,就可以使用https了。
[root@ecs-sn3-medium-2-linux-20191128162047 conf]# ../sbin/nginx -s reload
注意!
如果检查时提示nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx
,那就说明我们在安装Nginx的时候没有安装http_ssl_module
模块,这一点我们可以使用nginx -V
命令来查看。 解决办法:重新配置Nginx安装http_ssl_module
模块即可。 (1)进入到nginx的源码包
[root@ecs-sn3-medium-2-linux-20191128162047 nginx-1.17.8]# pwd
/root/nginx-1.17.8
(2)在源码包下执行如下命令:
[root@ecs-sn3-medium-2-linux-20191128162047 nginx-1.17.8]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
(3)使用make命令编译(不要执行make install,执行后就会覆盖安装已经存在Nginx)
[root@ecs-sn3-medium-2-linux-20191128162047 nginx-1.17.8]# make
最终效果:
