个人笔记nginx:HTTPS

一、HTTPS基础

1.为什么要使用https

http使用的是明文传输。 在传输敏感信息时不安全。( 交易信息、账户密码、银行卡号…) 监听获取。
所以我们需要使用https,https在数据传输过程中是加密的,能够有效的避免网站传输时信息的泄露。

2.什么是https

现在很多公司都是使用https来实现站点数据传输的安全的。 早期由 网景公司设计了 SSL 安全套接层协议。TLS协议。 传输层安全协议

3.TLS协议是如何实现明文消息被加密的

TLS与SSL在传输层与应用层之间对网络连接进行加密。

  1. 提供数据安全,保证数据尽可能不被泄露。
  2. 提供数据的完整性,保证数据在传输的过程中,不会被修改或者篡改数据。
  3. 对应用层交给传输层的数据进行加密与解密。
4.HTTPS加密模型

对称加密: 相同的秘钥。
非对称加密: 一对秘钥,公钥加密,私钥解密。
CA机构:可信任的组织架构,主要用来颁发证书的。 ( 警察局 --> 身份证 )

5.HTTPS实现加密与解密

https采用的是混合加密算法:使用了对称加密和非对称加密。
前提:CA机构申请证书,部署其web服务器上。

6.证书类型、购买方式、注意事项:

DV、OV、EV
证书是捆绑域名:
保护一个域名: www.oldxu.com
保护多个域名: www.oldxu.com test.oldxu.com images.oldxu.com cdn.oldxu.com m.oldxu.com
通配符域名: *.oldxu.com
a.oldxu.com b.oldxu.com c.oldxu.com test.oldxu.com …xx.oldxu.com

注意事项:
	绿色:	安全传输
	红色:	要么证书过期、要么证书不受信任
	黄色:	该 https中包含了http协议 (  会提示部分内容传输不安全  )
7.配置https证书
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.oldxu.com.conf
server {
	listen 443 ssl;
	server_name s.oldxu.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.oldxu.com.conf

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


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

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

	location / {
		index index.html;
	}
}
8.集群环境下的https证书
1.搭建http的负载均衡

	站点 a

[root@web01 ssl_key]# cat /etc/nginx/conf.d/s.oldxu.com.conf
server {
	listen 80;
	server_name s.oldxu.com;
	root /code;
	location / {
		index index.html;
	}
}


站点 b

[root@web02 ~]# cat  /etc/nginx/conf.d/s.oldxu.com.conf

server {
	listen 80;
	server_name s.oldxu.com;
	root /code;
	location / {
		index index.html;
	}
}



负载均衡

[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_s.oldxu.com.conf

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

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

	location / {
		proxy_pass http://http;
		include proxy_params;
	}
}
[root@lb01 ~]# systemctl reload nginx


2.改造http为https协议


[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_s.oldxu.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.oldxu.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.oldxu.com;
	return 302 https://$server_name$request_uri;
}

二、模拟银行的场景

  1. 主页http协议 http://yh.oldxu.com (提供网页浏览)
    1. 模拟登陆 http://yh.oldxu.com/login (相当于点击了登陆按钮)
    2. 新域名下,使用的是https协议。 https://star.oldxu.com (提供安全登陆)
1.配置  https://star.oldxu.com
[root@web01 ~]# cat /etc/nginx/conf.d/star.oldxu.com.conf
server {
	listen 443 ssl;
	server_name start.oldxu.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.oldxu.com
[root@web01 ~]# cat  /etc/nginx/conf.d/yh.oldxu.com.conf
server {
	listen 80;
	server_name yh.oldxu.com;
	root /code;

	location / {
		index index.html;
	}

	location /login {
		return 302 https://start.oldxu.com;
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值