openssl自签证书(https)
什么是https?
HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。
HTTPS:全称:Hyper Text Transfer Protocol over Secure Socket Layer,则是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。
HTTPS协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。
openssl自签证书
自签证书测试
1. 安装nginx
[[email protected] c7-47 ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel wget pcre pcre-devel
[[email protected] c7-47 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[[email protected] c7-47 ~]# tar zxf nginx-1.14.2.tar.gz
[[email protected] c7-47 ~]# cd nginx-1.14.2/
./configure --with-http_stub_status_module --with-http_ssl_module
[[email protected] c7-47 nginx-1.14.2]# make && make install
2. 检查Nginx的SSL模块
[[email protected] c7-47 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module
3. 准备私钥和证书
创建私钥
[[email protected] c7-47 ~]# cd /usr/local/nginx/
[[email protected] c7-47 nginx]# mkdir -p ssl
[[email protected] c7-47 nginx]# cd ssl/
[[email protected] c7-47 ssl]# openssl genrsa -des3 -out server.key 1024
Generating RSA private key, 1024 bit long modulus
.......++++++
....++++++
e is 65537 (0x10001)
Enter pass phrase for server.key: #123456
Verifying - Enter pass phrase for server.key: #123456
[[email protected] c7-47 ssl]# ll
total 4
-rw-r--r-- 1 root root 963 Apr 27 16:35 server.key
签发证书
[[email protected] c7-47 ssl]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key: #123456
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----# 一路回车就行
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
删除私钥口令
[[email protected] c7-47 ~]# cd /usr/local/nginx/ssl/
[[email protected] c7-47 ssl]# cp server.key server.key.ori
[[email protected] c7-47 ssl]# openssl rsa -in server.key.ori -out server.key
Enter pass phrase for server.key.ori: #123456
writing RSA key
生成使用签名请求证书和私钥生成自签证书
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
4.开启Nginx SSL
# 创建虚拟子目录
[[email protected] c7-47 ~]# mkdir -p /usr/local/nginx/conf/conf.d
# 精简主配置文件
cat >/usr/local/nginx/conf/nginx.conf<<EOF
> user nobody;
> worker_processes 1;
> events {
> worker_connections 1024;
> }
> http {
> include mime.types;
> default_type application/octet-stream;
> sendfile on;
> keepalive_timeout 65;
> include conf.d/*.conf;
> }
> EOF# 启动nginx 并查看进程
[[email protected] c7-47 ~]# /usr/local/nginx/sbin/nginx
# 创建虚拟主机子配置文件
cat >/usr/local/nginx/conf/conf.d/hack.conf<<EOF
> server {
> listen 443 ssl;
> server_name www.hack.com;
> ssl on;
> ssl_certificate /usr/local/nginx/ssl/server.crt;
> ssl_certificate_key /usr/local/nginx/ssl/server.key;
>
> location / {
> #定义站点目录
> root /usr/local/nginx/html;
> index index.php index.html index.htm;
> }
> }
> EOF# 重新加载nginx配置文件
[[email protected] c7-47 ~]# /usr/local/nginx/sbin/nginx -t
[[email protected] c7-47 ~]# /usr/local/nginx/sbin/nginx -s reload
绑定windows的hosts:
10.0.0.47 www.hack.com
最后一行添加
上传 hack.html 到/usr/local/nginx/html目录。
然后谷歌浏览器访问:https://www.hack.com/hack.html
此时,你会发现,http://www.hack.com/hack.html,浏览器访问不了了,需要进行rewrite跳转。
5.rewrite跳转
以上配置有个不好的地方,如果用户忘了使用https或者443端口,那么网站将无法访问,因此需要将80端口的访问转到443端口并使用ssl加密访问。只需要增加一个server段,使用301永久重定向。
cat >/usr/local/nginx/conf/conf.d/hack.conf<<\EOF
> server {
> listen 80;
> server_name www.hack.com;
> rewrite ^(.*) https://$server_name$1 permanent;
> }
>
> server {
> listen 443 ssl;
> server_name www.hack.com;
> ssl on;
> ssl_certificate /usr/local/nginx/ssl/server.crt;
> ssl_certificate_key /usr/local/nginx/ssl/server.key;
>
>
> location / {
> #定义站点目录
> root /usr/local/nginx/html;
> index index.php index.html index.htm;
> }
> }
> EOF# 重新加载nginx配置文件
[[email protected] c7-47 ~]# /usr/local/nginx/sbin/nginx -t
[[email protected] c7-47 ~]# /usr/local/nginx/sbin/nginx -s reload
这时,浏览器访问 http://www.hack.com/hack.html,nginx会将请求跳转到 https://www.hack.com/hack.html,详细可以查看nginx日志。