阅读目录:
1. 简介
2. 证书和私钥的生成
3. 配置文件
4. 开启nginx的ssl模块
5. 同时启用HTTP和HTTPS
6.说明
请先参照上一篇:Nginx 1.12.x 安装和配置--Linux篇
https://my.oschina.net/u/3209432/blog/1581391
1. 简介
- https简介
HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据。
- https协议原理
首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来。
2. 证书和私钥的生成
注意:一般生成的目录,最好应该放在nginx/conf/ssl目录
2.1 进入nginx安装目录
[root@localhost ~]# cd /usr/local/nginx/conf [root@localhost conf]# mkdir ssl [root@localhost ssl]# cd ssl
2.2 创建服务器证书密钥文件 server.key 和 证书的申请文件 server.csr
[root@localhost ssl]# openssl req -newkey rsa:2048 -keyout server.key -out server.csr s Generating a 2048 bit RSA private key ............................+++ ..............................+++ writing new private key to 'server.key' Enter PEM pass phrase:
输入密码,确认密码,自己随便定义,但是要记住,后面会用到。
之后要输入验证信息的内容为:
-----
Enter pass phrase for root.key: ← 输入前面创建的密码
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) []:ZheJiang← 省的全名,拼音
Locality Name (eg, city) [Default City]:HangZhou← 市的全名,拼音
Organization Name (eg, company) [Default Company Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, your name or your server's hostname) []: xxx.com← ip
Email Address []:[email protected] ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
2.3 备份一份服务器密钥文件
[root@localhost ssl]# cp server.key server.key.org
以下是使用服务器上的证书,如果要用三方的证书请省略下面两步
2.4 去除文件口令
[root@localhost ssl]# openssl rsa -in server.key.org -out server.key
2.5 生成证书文件server.crt
[root@localhost ssl]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
3. 配置文件
[root@localhost ssl]# vim xxx.conf
server {
listen 443 ssl;ssl_certificate_key /usr/local/nginx/conf/ssl/server.key;
keepalive_timeout 70;
server_name www.loubobooo.com;
#禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
server_tokens off;
#如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的>浏览器本网站全站加密,并且强制用 HTTPS 访问
#add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
# ......
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;autoindex on;
access_log /usr/local/nginx/logs/access.log combined;
error_log /usr/local/nginx/logs/error.log;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;location / {
proxy_pass http://127.0.0.1:8080/;
add_header Access-Control-Allow-Origin '*';
}}
4. 开启nginx的ssl模块
4.1 重启nginx过程中的报错
[root@localhost ssl]# service nginx restart the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
这里是因为nginx缺少http_ssl_module模块,编译安装时带上--with-http_ssl_module配置就可以了
4.2 查看ngixn原有的模块
[root@localhost ssl]# /usr/local/nginx/sbin/nginx -V
大概显示2个模块,总之没有http_ssl_module模块
4.3 切换到nginx源码包
[root@localhost ssl]# cd /developer/nginx-1.10.2
4.4 重新配置
[root@localhost nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
4.5 重新编译,不需要make install安装。否则会覆盖
[root@localhost nginx-1.10.2]# make
4.6 备份原有已经安装好的nginx
[root@localhost nginx-1.10.2]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
4.7 将刚刚编译好的nginx覆盖掉原来的nginx(ngixn必须停止)
[root@localhost nginx-1.10.2]# cp ./objs/nginx /usr/local/nginx/sbin/
这时,会提示是否覆盖,请输入yes,直接回车默认不覆盖
4.8 启动nginx,查看nginx模块,发现已经添加
[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.10.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
5. 同时启用HTTP和HTTPS
[root@localhost nginx-1.10.2]# cd /usr/local/nginx/conf [root@localhost conf]# vim xxx.conf
继续在下面添加如下代码:
server {
listen 80;
server_name loubobooo.com;
rewrite ^ https://$http_host$request_uri? permanent;
}
6. 说明
说明:本次使用
操作系统:CentOS 6.8 64位
Nginx版本:1.12.2
最后关于配置Tomcat的Https和Nginx的Https的选择,
只要在nginx层配置证书就可以了。在nginx层配置把请求转发到指定端口就可以了,证书强烈不建议在tomcat上配置,会导致证书各处传播非常不安全,同时呢,如果证书过期需要换个新证书,这个工作量也是巨大。现在一个大点公司都有很多项目,后面有更多的tomcat项目,因此只需要在nginx入口那里配置好证书就可以了,安全又方便。<!-- 引用jimi的一句话 -->
下一篇:HTTPS免费证书StartSSL申请详解
https://my.oschina.net/u/3209432/blog/1595700