HTTPS实践

一:单节点实现HTTPS

1.配置https证书:
1…1申请证书 ( 黑户 )
[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# cd /etc/nginx/ssl_key
[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048
	
	#设置密码:1234

[root@web01 ssl_key]# openssl req -days 36500 -x509 \
-sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

.....设置证书信息自己随意填写(反正假的)
2.在Nginx中配置证书
[root@web01 ssl_key]# cat  /etc/nginx/conf.d/s.wyk.com.conf
server {
	listen 443 ssl;
	server_name s.wyk.com;
	root  /code;

	ssl_certificate  ssl_key/server.crt;    	#公钥
	ssl_certificate_key ssl_key/server.key;		#私钥
	ssl_protocols TLSv1.2;						#版本号
	location / {
		index index.html;
	}
}
3.访问,请带上https,( http访问失败 )
4.如果希望http访问,强制转到https
[root@web01 ssl_key]# cat /etc/nginx/conf.d/s.wyk.com.conf

#当有用户请求http://s.wyk.com 则强制跳转为https协议。
server {
	listen 80;
	server_name s.wyk.com;
	return 302 https://$server_name$request_uri;
}


server {
	listen 443 ssl;
	server_name s.wyk.com;
	root  /code;

	ssl_certificate  ssl_key/server.crt;
	ssl_certificate_key ssl_key/server.key;
	ssl_protocols TLSv1.2;

	location / {
		index index.html;
	}
}

二:集群环境下HTTPS证书

1.搭建http负载均衡
1.1.站点a
[root@web01 ssl_key]# cat /etc/nginx/conf.d/s.wyk.com.conf
server {
	listen 80;
	server_name s.wyk.com;
	root /code;
	location / {
		index index.html;
	}
}

[root@web02 ssl_key]# mkdir /code
[root@web02 ssl_key]# echo "a-https" > /code/index.html 

1.2.站点b
[root@web02 ssl_key]# cat /etc/nginx/conf.d/s.wyk.com.conf
server {
	listen 80;
	server_name s.wyk.com;
	root /code;
	location / {
		index index.html;
	}
}

[root@web01 ssl_key]# mkdir /code
[root@web01 ssl_key]# echo "b-https" > /code/index.html 
1.3.负载均衡
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_s.wyk.com.conf

upstream http {
	server 172.16.1.7:80;
	server 172.16.1.8:80;
}

server {
	listen 80;
	server_name s.wyk.com;

	location / {
		proxy_pass http://http;
		include proxy_params;
	}
}
[root@lb01 ~]# systemctl reload nginx
2.改造http为https协议
  • 把上面/etc/nginx/ssl_key目录下的证书推一份过来
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_s.wyk.com.conf

upstream http {
	server 172.16.1.7:80;
	server 172.16.1.8:80;
}

	#2.用户请求https协议,贼通过负载均衡方式请求资源池,使用的是http协议
server {
	listen 443 ssl;
	server_name s.wyk.com;
	ssl_certificate ssl_key/server.crt;
	ssl_certificate_key ssl_key/server.key;

	location / {
		proxy_pass http://http;
		include proxy_params;
	}
}

	#1.用户请求http协议,强制跳转至https协议
server {
	listen 80;
	server_name s.wyk.com;
	return 302 https://$server_name$request_uri;
}

三:模拟银行场景

1.主页http协议 http://yh.wyk.com (提供网页浏览)
2.模拟登陆 http://yh.wyk.com/login (相当于点击了登陆按钮)
3.新域名下,使用的是https协议。 https://star.wyk.com (提供安全登陆)

1.配置  https://star.wyk.com
[root@web01 ~]# cat /etc/nginx/conf.d/star.wyk.com.conf
server {
	listen 443 ssl;
	server_name start.wyk.com;
	ssl_certificate ssl_key/server.crt;
	ssl_certificate_key ssl_key/server.key;

	root  /code/login;
	location / {
		index index.html;
	}
}
[root@web01 ~]# mkdir /code/login -p
[root@web01 ~]# echo "login...https" > /code/login/index.html

2.配置 http://yh.wyk.com
[root@web01 ~]# cat  /etc/nginx/conf.d/yh.wyk.com.conf
server {
	listen 80;
	server_name yh.wyk.com;
	root /code;

	location / {
		index index.html;
	}

	location /login {
		return 302 https://start.wyk.com;
	}
}

四.HTTPS优化

https优化:
减少 CPU 运算量
SSL的运行计算需要消耗额外的 CPU 资源,SSL通讯过程中『握手』阶段的运算最占用 CPU 资源,有两个方法可以减少每台客户端的运算量:

	1.设置worker进程数设置为等于CPU处理器的核心数。 worker_processes auto;
	1.启用 keepalive 长连接,一个连接发送更多个请求。
	2.启用 SSL 会话缓存参数,避免进行多次 SSL『握手』。


例子如下所示:

    server {
        listen              443 ssl;
        server_name         www.example.com;
        
	keepalive_timeout   70;				#设置长连接
        ssl_certificate     www.example.com.crt;
        ssl_certificate_key www.example.com.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;

	#在建立完ssl握手后如果断开连接,在session_timeout时间内再次连接,是不需要在次建立握手,可以复用之前的连接。
	ssl_session_cache   shared:SSL:10m;		 #1M缓存空间能存储 4000 个会话数量
	ssl_session_timeout 1024m;			 #配置会话超时时间  默认5分钟 
    }
	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值