#Web网站的登录页面都是使用https加密传输的,加密数据以保障数据的安全,HTTPS能够加密信息,以免敏感信息被第三方获取,所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议,HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据.
#(1)https 配置参数
#nginx 的https 功能基于模块ngx_http_ssl_module实现,因此如果是编译安装的nginx要使用参数ngx_http_ssl_module开启ssl功能,但是作为nginx的核心功能,yum安装的nginx默认就是开启的,编译安装的nginx需要指定编译参数--with-http_ssl_module开启
配置参数如下:
ssl on | off; #老版本使用,新版本淘汰
listen 443 ssl;#新版本直接在listen后面加ssl
#为指定的虚拟主机配置是否启用ssl功能,此功能在1.15.0废弃,使用listen [ssl]替代
ssl_certificate /path/to/file;
#指向包含当前虚拟主机和CA的两个证书信息的文件,一般是crt文件
ssl_certificate_key /path/to/file;
#当前虚拟主机使用的私钥文件,一般是key文件
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
#支持ssl协议版本,早期为ssl现在是TLS,默认为后三个
ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
#配置ssl缓存
off: #关闭缓存
none: #通知客户端支持ssl session cache,但实际不支持
builtin[:size]:#使用OpenSSL内建缓存,为每worker进程私有
[shared:name:size]:#在各worker之间使用一个共享的缓存,需要定义一个缓存名称和缓存空间
大小,一兆可以存储4000个会话信息,多个虚拟主机可以使用相同的缓存名称
ssl_session_timeout time;
#客户端连接可以复用ssl session cache中缓存的有效时长,默认5m
#(2)自签名证书
#自签名CA证书
[root@centos8 ~]# cd /apps/nginx/
[root@centos8 nginx]# mkdir certs
[root@centos8 nginx]# cd certs/
[root@centos8 nginx]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout
ca.key -x509 -days 3650 -out ca.crt #自签名CA证书
Generating a 4096 bit RSA private key
.................++
.....
Country Name (2 letter code) [XX]:CN #国家代码
State or Province Name (full name) []:HuBei #省份
Locality Name (eg, city) [Default City]:HuBei #城市名称
Organization Name (eg, company) [Default Company Ltd]:tan #公司名称
Organizational Unit Name (eg, section) []:tan #部门
Common Name (eg, your name or your server's hostname) []:ca.tan.org #通用名称
Email Address []: qqq@qq.com#邮箱
[root@centos8 certs]# ll ca.crt
-rw-r--r-- 1 root root 2118 Feb 22 12:10 ca.crt
#自制key和csr文件
[root@centos8 certs]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout
www.tan.org.key -out www.tan.org.csr
Generating a 4096 bit RSA private key
........................................................................++
......
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HuBei
Locality Name (eg, city) [Default City]:HuBei
Organization Name (eg, company) [Default Company Ltd]:tan.org
Organizational Unit Name (eg, section) []:tan.org
Common Name (eg, your name or your server's hostname) []:www.tan.org
Email Address []:qqqq@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@centos8 certs]# ll
total 16
-rw-r--r-- 1 root root 2118 Feb 22 12:10 ca.crt
-rw-r--r-- 1 root root 3272 Feb 22 12:10 ca.key
-rw-r--r-- 1 root root 1760 Feb 22 12:18 www.tan.org.csr
-rw-r--r-- 1 root root 3272 Feb 22 12:18 www.tan.org.key
#签发证书
[root@centos8 certs]# openssl x509 -req -days 3650 -in www.tan.org.csr -CA
ca.crt -CAkey ca.key -CAcreateserial -out www.tan.org.crt
#验证证书内容
[root@centos8 certs]# openssl x509 -in www.tan.org.crt -noout -text
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
bb:76:ea:fe:f4:04:ac:06
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=CN, ST=HuBei, L=HuBei, O=tan, OU=tan,
CN=tan.ca/emailAddress=qqqq@qq.com
Validity
Not Before: Feb 22 06:14:03 2019 GMT
Not After : Feb 22 06:14:03 2020 GMT
Subject: C=CN, ST=HuBei, L=HuBei, O=tan.org, OU=tan.org,
CN=www.tan.org/emailAddress=qqqq@qq.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
#合并CA和服务器证书成一个文件,注意服务器证书在前
[root@centos8 certs]#cat www.tan.org.crt ca.crt > www.tan.org.pem
#(3) https 配置
server {
listen 80;
listen 443 ssl;
ssl_certificate /apps/nginx/certs/www.tan.org.pem;
ssl_certificate_key /apps/nginx/certs/www.tan.org.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
root /data/nginx/html;
}
#重启Nginx并访问验证
#(4) 自动跳转 https
基于通信安全考虑公司网站要求全站 https,因此要求将在不影响用户请求的情况下将http请求全部自动跳转至 https,另外也可以实现部分 location 跳转
[root@centos8 ~]#vim /apps/nginx/conf.d/pc.conf
server {
listen 443 ssl;
listen 80;
ssl_certificate /apps/nginx/certs/www.tan.org.crt;
ssl_certificate_key /apps/nginx/certs/www.tan.org.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
server_name www.tan.org;
location / { #针对全站跳转
root /data/nginx/html/pc;
index index.html;
if ($scheme = http ){ #如果没有加条件判断,会导致死循环
rewrite / https://$host redirect;
}
}
location /login { #针对特定的URL进行跳转https
if ($scheme = http ){ #如果没有加条件判断,会导致死循环
rewrite / https://$host/login redirect;
}
}
}
#重启Nginx并访问测试
[root@centos7 ~]#curl -ikL www.tan.org
配置虚拟主机,实现强制https跳转访问www.x.com
发布时间 2023-10-08 09:45:56作者: 小糊涂90