Linux-nginx动静分离/rewrite

image

动静分离

1、创建挂载点 在NFS那台机器上 
	1.1 [root@nfs ~]# mkdir /static 
	1.2 [root@nfs /static/s]# vim /etc/exports 

image

	1.3 s[root@nfs /static/s]# ystemctl restart nfs-server 
	1.4 [root@nfs /static/s]# chown -R www.www /static/ 
2、在服务器上创建文件夹,然后将静态资源放进文件夹内 
[root@web01 static]# mkdir /opt/static/s 
[root@web01 static]# cp -r /opt/bbs/static/* /opt/static/s/ 
3、挂载到lb01上 
[root@lb01 conf.d]# yum install nfs-utils -y 
[root@lb01 conf.d]# mkdir /opt/static 
[root@lb01 conf.d]# mount -t nfs 172.16.1.31:/static /opt/static/ 
4、[root@lb01 conf.d]# vi bbs.conf 把下面那一段加上!!!! 
location ~ \.(jpg|css|js|png)$ { root /opt/static; } } 
<⚠️注意:自己电脑上域名解析的部分!!!!!!!!不然就是死活找不到!!!!!!!!btw,什么时候脑袋才能清醒??????> 

rewrite

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

1、地址跳转,用户访问www.linux.com这个url,通过rewrite可以将其重新定向到www.baidu.com 
2、协议跳转,用户通过http协议请求网站时,将其重新跳转到https协议方式 
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态的url地址对外暴露过多的参数,提升更高的安全性。 
4、搜索引擎,SEO优化依赖于url路径,好记的url便于搜索引擎录入。

rewrite语法

Syntax: rewrite regex replacement [flag]; 
Default: — 
Context: server, location, if 
rewrite # 模块命令 
regex # 请求的链接(支持正则表达式) 
replacement # 跳转的链接
[flag]; # 标签 

location /download/ {
		rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; 
		rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; 
		return 403; 
} 

rewrite标记Flag

rewrite指令根据表达式来重定向url,或者修改字符串,可以应用于server,local,if环境下,美航rewrite指令下最后跟一个flag标记,支持的flag标记有如下的表哥所示:

flag 作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址

以下例子:

server {
	server_name _;
	listen 80;
	location ~ ^/break {
		rewrite (.*) /test break;
	}

	location ~ ^/last {
		rewrite (.*) /test last;
	}

	location /test {
		default_type text/html;
		return 200 "test";
	}
}

break和last的区别

🌹break请求:

1、请求linux.rewrite.com/break 
2、匹配location ~^/break 会跳转到linux.rewrite.com/test 
3、请求挑战后,回去查找本地站点目录下的 /test 
4、如果找到了,则返回/code/test/index.html的内容 
5、如果没找到该目录下则报错404,如果找到该目录没找到对应的文件则返回403

🌹last请求:

1、请求linux.rewrite.com/break 
2、匹配location ~^/break 会跳转到linux.rewrite.com/test 3、如果找到了,则返回/code/test/index.html的内容 
4、如果没有找到,会重新对当前server发起请求,这个时候访问的地址就变成linux.rewrite.com/test 
5、重新请求server会匹配到location /test/ 直接返回该location的内容 
6、如果也没有location匹配

redirect和permanent的区别

重定向

location /redirect {
	rewrite (.*) http://www.baidu.com redirect;
}
location /permanent {
	rewrite (.*) http://www.baidu.com permanent;
}

redirect:每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败
permanent:第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

HTTPS

为什么需要https呢?因为http不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么在传输过程中时加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时的细腻下泄漏。我们在实现https时,需要了解ssl协议,但是我们现在使用更多的是TLS加密协议。
在OSI七层模型中,应用层是http协议,那么在应用层协议之下,表示层是ssl协议发挥作用的一层,他通过(握手,交换密钥,告警,加密)等方式,是应用层http协议没有感知的情况下i啊做到了数据的安全加密。

🌼🌼🌼模拟网站劫持

[root@web03 ~]# vim /opt/judy.html

粘贴以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生信息注册页面</title>
</head>
<body>
<h3 align="center">学生信息注册</h3>
<form  name="stu"action="">
<table>
  <tr><td>姓名:</td><td><input type="text"name="stuName"/></td></tr>
  <tr><td>性别:</td>
	  <td><input type="radio"name="stuSex"checked="checked">男
		  <input type="radio"name="stuSex">女
		  </td>
		  </tr>
   <tr><td>出生日期</td>
	   <td><input type="text"name="stuBirthday"></td>
	   <td>按格式yyyy-mm-dd</td>
	   </tr>
	   <tr><td>学校:</td><td><input type="text"name="stuSchool"></td></tr>
	   <tr><td>专业:</td>
		   <td><select name="stuSelect2">
			   <option selected>计算机科学与技术</option>
			   <option>网络工程</option>
			   <option>物联网工程</option>
			   <option>应用数学</option>
			   </select>
			   </td>
			   </tr>
			   <tr><td>体育特长:</td>
				   <td colspan="2">
					  <input type="checkbox"name="stuCheck" >篮球
					  <input type="checkbox"name="stuCheck" >足球
					  <input type="checkbox"name="stuCheck" >排球
					  <input type="checkbox"name="stuCheck" >游泳
				   </td> 
			   </tr>
			   <tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>
			   <tr><td>密码:</td><td><input type="password"name="stuPwd" ></td></tr>
			   <tr><td>个人介绍:</td>
				   <td colspan="2"><textarea name="Letter"rows="4"cols="40"></textarea></td>
			   </tr>
			   <tr>
				 <td><input type="submit"value="提交" ><input type="reset"value="取消" ></td>
				 </tr>
				 </table>
				 </form>
</body>
</html>

[root@web01 ~]# chown -R www.www /opt/judy.html

⚠️⚠️⚠️ 中间网站劫持

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.jc.com.conf 
server {
	listen 80;
	server_name linux.jc.com;

	location / {
		proxy_pass http://192.168.15.9:80;
		include proxy_params;

		sub_filter '<title>学生信息注册页面</title>' '<title>澳门首家线上赌场</title>';
		sub_filter '<h3 align="center">学生信息注册</h3>' '<h3 align="center">VIP用户信息注册</h3>';
		sub_filter '<tr><td>性别:</td>' '<tr><td>爱好:</td>';
		sub_filter '<option selected>计算机科学与技术</option>' '<option selected>按摩</option>';
		sub_filter '<option>网络工程</option>' '<option>抽烟</option>';
		sub_filter '<option>物联网工程</option>' '<option>喝酒</option>';
		sub_filter '<option>应用数学</option>' '<option>烫头</option>';
		sub_filter '<tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>' '<img src="https://blog.driverzeng.com/zenglaoshi/xingganheguan.gif">';
	}
}

[root@lb01 ~]# systemctl restart nginx

<中间需要重新编译:重新编译的时候必须重新进入nginx目录里面:>

cd /root/nginx-1.20.2
[root@lb01 conf.d]# ./configure  --with-http_gzip_static_module --with-stream --with-http_ssl_module --with-http_sub_module 
[root@lb01 conf.d]# make
[root@lb01 conf.d]# make install
再替换nginx(如果需要的话)

加密🔐

一、流程

1、浏览器发起往服务器的443端口发起请求,请求携带了浏览器支持的加密算法和哈希算法。 
2、服务器收到请求,选择浏览器支持的加密算法和哈希算法。 
3、服务器下将数字证书返回给浏览器,这里的数字证书可以是向某个可靠机构申请的,也可以是自制的。 
4、浏览器进入数字证书认证环节,这一部分是浏览器内置的TLS完成的:
	4.1 首先浏览器会从内置的证书列表中索引,找到服务器下发证书对应的机构,如果没有找到,此时就会提示用户该证书是不是由权威机构颁发,是不可信任的。如果查到了对应的机构,则取出该机构颁发的公钥。 
	4.2 用机构的证书公钥解密得到证书的内容和证书签名,内容包括网站的网址、网站的公钥、证书的有效期等。浏览器会先验证证书签名的合法性(验证过程类似上面Bob和Susan的通信)。签名通过后,浏览器验证证书记录的网址是否和当前网址是一致的,不一致会提示用户。如果网址一致会检查证书有效期,证书过期了也会提示用户。这些都通过认证时,浏览器就可以安全使用证书中的网站公钥了。 
	4.3 浏览器生成一个随机数R,并使用网站公钥对R进行加密。
5、浏览器将加密的R传送给服务器。 
6、服务器用自己的私钥解密得到R。 
7、服务器以R为密钥使用了对称加密算法加密网页内容并传输给浏览器。 
8、浏览器以R为密钥使用之前约定好的解密算法获取网页内容。

二、证书对比

image

三、自签证书

使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)

1、[root@web03 /opt]# openssl genrsa -idea -out server.key 2048

2、#生成自签证书(公钥),同时去掉私钥的密码
[root@web03 /opt]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

# req  --> 用于创建新的证书
# new  --> 表示创建的是新证书    
# x509 --> 表示定义证书的格式为标准格式
# key  --> 表示调用的私钥文件信息
# out  --> 表示输出证书文件信息
# days --> 表示证书的有效期
# sha256 --> 加密方式

#1.开启证书
Syntax: ssl on | off;
Default:    ssl off;
Context:    http, server

#2.指定证书文件
Syntax: ssl_certificate file;
Default:    —
Context:    http, server

#3.指定私钥文件
Syntax: ssl_certificate_key file;
Default:    —
Context:    http, server

3、[root@web03 /opt]# vim test.conf 

	server {
   listen 443 ssl;
   server_name _;
   ssl_certificate /opt/server.crt;
   ssl_certificate_key /opt/server.key;

   location / {
	 root /opt;
	 index judy.html;
   }
}	 
	server {
		listen 80;
		server_name _;
		rewrite (.*) https://192.168.15.5 permanent;
	}

4、测试 输入https://+ip
posted @ 2022-01-07 20:24  JudyJU  阅读(90)  评论(0)    收藏  举报