nginx动静分离和rewrite重写和https和keepalived

bash 复制代码
动静分离,通过中间件将动态请求和静态请求分离,可以减少不必要的消耗,同时减少请求延迟
动静分离只有好处:动静分离后,即使动态资源不可用,但静态资源不受影响

单台实现动静分离

bash 复制代码
1.部署java
yum install java-11-openjdk -y
2.部署tomcat
  官网:https://tomcat.apache.org/
  [root@test2 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.30/bin/apache-tomcat-10.1.30.tar.gz
[root@test2 ~]# ./apache-tomcat-10.1.30/bin/startup.sh 
Using CATALINA_BASE:   /root/apache-tomcat-10.1.30
Using CATALINA_HOME:   /root/apache-tomcat-10.1.30
Using CATALINA_TMPDIR: /root/apache-tomcat-10.1.30/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/apache-tomcat-10.1.30/bin/bootstrap.jar:/root/apache-tomcat-10.1.30/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.


tomcat的代码目录
[root@test2 examples]# ll /var/lib/tomcat/webapps/ROOT/
total 164
-rw-r----- 1 root root 27235 Oct  6 10:35 asf-logo-wide.svg
-rw-r----- 1 root root   713 Oct  6 10:35 bg-button.png
-rw-r----- 1 root root  1918 Oct  6 10:35 bg-middle.png
-rw-r----- 1 root root  1401 Oct  6 10:35 bg-nav.png
-rw-r----- 1 root root  3103 Oct  6 10:35 bg-upper.png
-rw-r----- 1 root root 21630 Oct  6 10:35 favicon.ico
-rw-r----- 1 root root 12241 Oct  6 10:35 index.jsp
-rw-r----- 1 root root  6776 Oct  6 10:35 RELEASE-NOTES.txt
-rw-r----- 1 root root  5584 Oct  6 10:35 tomcat.css
-rw-r----- 1 root root 67795 Oct  6 10:35 tomcat.svg
drwxr-x--- 2 root root    21 Oct  6 10:35 WEB-INF


3.配置nginx代理tomcat
[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
server {
	listen 80;
	server_name 192.168.23.102;
        location / {
	proxy_pass http://192.168.23.102:8080;
	}
	location ~ \.(svg|png)$ {
	root /code;
	}
}



[root@test2 ~]# mv apache-tomcat-10.1.30/webapps/ROOT/tomcat.svg /code/
[root@test2 ~]# mv apache-tomcat-10.1.30/webapps/ROOT/asf-logo-wide.svg /code/
[root@test2 ~]# mv apache-tomcat-10.1.30/webapps/ROOT/*.png /code/
[root@test2 ~]# chown -R nginx /code

因为图片是静态文件,即使这个页面出现问题了,这个小猫也能访问

此时就算把tomcat停止了,图片也能继续访问

bash 复制代码
[root@test2 ~]# apache-tomcat-10.1.30/bin/shutdown.sh 
Using CATALINA_BASE:   /root/apache-tomcat-10.1.30
Using CATALINA_HOME:   /root/apache-tomcat-10.1.30
Using CATALINA_TMPDIR: /root/apache-tomcat-10.1.30/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/apache-tomcat-10.1.30/bin/bootstrap.jar:/root/apache-tomcat-10.1.30/bin/tomcat-juli.jar
Using CATALINA_OPTS:   

根据user-agent跳转不同的业务

bash 复制代码
来判断用户是iphone 还是window来跳转到不同的业务
在负载均衡上来实现

[root@test2 ~]# cat /etc/nginx/conf.d/proxy.conf 
upstream android {
	server 192.168.23.102:9090;
}
upstream iphone {
	server 192.168.23.102:9091;
}
upstream pc {
	server 192.168.23.102:9092;
}
server {
	listen 80;
	server_name 192.168.23.102;
	charset 'utf-8';
	
	location / {
	#如果客户端的来源是android则跳转到androiad
	if ($http_user_agent ~* "Android") {
		proxy_pass http://android;
	}
	
	#如果客户端的来源是iphone则跳转到iphone
	if ($http_user_agent ~* "Iphone") {
		proxy_pass http://iphone;
	}
	
	#如果客户端的来源是IE 浏览器则返回403
	if ($http_user_agent ~* "MSIE") {
		return 403;
	}
	
	#默认跳转到pc资源
	proxy_pass http://pc;
	}
}

rewrite重写

bash 复制代码
http://www.oldboy.com:80/index.html
www  主机
oldboy 权威域名
com 顶级域名
index。html  资源


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

rewrite使用场景
1.地址跳转
2.协议跳转 http跳转为https
3.伪静态:将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态url地址对外暴露过多参数,提升更高的安全性
4.搜索引擎,SEO优化依赖与url路径,好纪的url便于支持搜索引擎录入

语法
Syntax:	rewrite regex replacement [flag];
Default:	---
Context:	server, location, if
flag 作用
last 本条规则匹配完成后,停止匹配,不在匹配后面的规则
break 本条规则匹配完成后,停止匹配,不在匹配后面的规则
redirect 返回302临时重定向,地址栏会显示调准后的地址
permanent 返回301临时重定向,地址栏会显示跳转后的地址

测试

bash 复制代码
[root@test3 ~]# cat /etc/nginx/conf.d/test.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code/test/;
	location / {
		rewrite /1.html /2.html;
		rewrite /2.html /3.html;
	}
	location /2.html {
		rewrite /2.html /a.html;
	}
	location /3.html {
		rewrite /3.html /b.html;
	}
}
[root@test3 ~]# mkdir /code/test/ -p
[root@test3 ~]# echo 2.html > /code/test/2.html
[root@test3 ~]# echo 3.html > /code/test/3.html
[root@test3 ~]# echo a.html > /code/test/a.html
[root@test3 ~]# echo b.html > /code/test/b.html
[root@test3 ~]# echo 1.html > /code/test/1.html
[root@test3 ~]# systemctl restart nginx
[root@test3 ~]# curl 192.168.23.103/1.html
b.html
[root@test3 ~]# curl 192.168.23.103/2.html
a.html


break的用法,停止向后匹配
[root@test3 ~]# cat /etc/nginx/conf.d/test.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code/test/;
	location / {
		rewrite /1.html /2.html break;
		rewrite /2.html /3.html;
	}
	location /2.html {
		rewrite /2.html /a.html;
	}
	location /3.html {
		rewrite /3.html /b.html;
	}
}

[root@test3 ~]# systemctl restart nginx
[root@test3 ~]# curl 192.168.23.103/1.html
2.html


last的用法
[root@test3 ~]# cat /etc/nginx/conf.d/test.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code/test/;
	location / {
		rewrite /1.html /2.html last;
		rewrite /2.html /3.html;
	}
	location /2.html {
		rewrite /2.html /a.html;
	}
	location /3.html {
		rewrite /3.html /b.html;
	}
}
[root@test3 ~]# systemctl restart nginx
[root@test3 ~]# curl 192.168.23.103/1.html
a.html
rewrite /1.html /2.html last;到2.thml就不会向后匹配了,但是他会重新对2.html发起请求,所以返回的就是a。html

break:停止向下匹配。直接返回结果
last:停止向下匹配,重新发起请求到服务器

redirect和permanent的用法

bash 复制代码
[root@test3 conf.d]# cat rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code;
	location /test {
		rewrite ^(.*)$ http://192.168.23.102/1.html redirect;
	#	rewrite ^(.*)$ http://192.168.23.102/1.html permanent;
	#	return 301 http://192.168.23.102/1.html;
	#	return 302 http://192.168.23.102/1.html;
	}
}
redirect 302临时跳转,nginx停了他就不能跳转了,第二次以后所有的访问,都会先请求原站,192.168.23.103/test 如果原站挂了则不行,
permanent 301永久跳转,nginx停了他依然能跳转,第二次以后再也不访问原站了,他会直接访问http://192.168.23.102/1.html,301被缓存到了浏览器
rewrite和return任选其一

http状态码

bash 复制代码
200 正常的
301 永久跳转
302 临时跳转
304 缓存
307 内部跳转
401 验证没有通过
403 有目录没资源,或者权限不对
404 没有代码资源

正常500以后都是和数据库相关的
500 (服务器内部错误)服务器遇到错误,无法完成请求
501 (尚未实施)     服务器不具备完整的请求功能,例如:服务器无法识别请求方法时可能会返回此代码
502 (错误网关)     服务器作为网关或代理,从上游服务器收到i的无效响应
503 (服务不可用)   服务器目前无法使用(由于超载或者停机维护),通常这是暂停状态
504 (网关超时)     服务器作为网关或者代理,但是没有及时从上游服务器收到请求
505 (http版本不受支持) 服务器不支持请求中所用的http协议版本    

在写rewrite之前需要开启rewrite日志

bash 复制代码
[root@test3 ~]# cat /etc/nginx/nginx.conf|grep -E "^(http|    rewrite)"
http {
    rewrite_log on;


测试
[root@test3 ~]# cat /etc/nginx/conf.d/rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code;
	location /test {
		rewrite ^(.*)$ /ccc/bbb/2.html;
	}
}

[root@test3 ~]# mkdir /code/ccc/bbb/ -p
[root@test3 ~]# echo tigs  >  /code/ccc/bbb/2.html
[root@test3 ~]# systemctl restart nginx
[root@test3 ~]# curl 192.168.23.103/test
tigs


[root@test3 test]# cat /etc/nginx/conf.d/rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	location / {
		root /code;
		index index.html;
	}
	location /2018 {
		rewrite ^/2018/(.*)$ /2014/$1 redirect;
	}
}
[root@test3 test]# mkdir /code/2014/test/ -p
[root@test3 test]# echo 111 > /code/2014/test/1.html
此时就已经跳转了
[root@test3 test]# curl 127.0.0.1/2018/test/1.html

案例4:访问course-11-22-33.html 实际上访问的是 /course/11/22/course_33.html
[root@test3 conf.d]# cat /etc/nginx/conf.d/rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code;
	index index.html;
	location / {
		#灵活写法
		rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
		#固定写法
		#rewrite ^/course-(.*) /course/11/22/course_33.html redirect;
	}
}


错误页面跳转
[root@test3 ~]# cat /etc/nginx/conf.d/rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code;
	location /test {
		rewrite (.*) https://192.168.23.103/111 redirect;
	}
	error_page 403 404 500 501 502 @error_test;
	location @error_test {
		rewrite ^(.*)$ /404.html break;
	}
}


变量的使用
案列1:需要在跳转后的请求行加上想要的参数&showoffline=1,公司内部人员加上
[root@test3 conf.d]# cat rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	#$args为nginx内置变量请求行的参数
	set $args "&showoffline=1";   #设置一个变量$args 但是设置的时候变量为空,于是就把后面的参数赋值给args
	location / {
		root /code;
		index index.html;
	}
	if ($remote_addr = 192.168.23.1 )
	{
		rewrite (.*) http://192.168.23.103$1;
	}
}

实际访问的ip
http://192.168.23.103/?&showoffline=1


案列2.网站维护,指定的ip正常访问,其他ip跳转维护页面
[root@test3 code]# cat /etc/nginx/conf.d/rewrite.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	root /code/;
	charset utf-8,gbk;
	location / {
		index index.html;
		set $ip 0;    #设置变量为0
		if ($remote_addr = "192.168.23.2"){
			set $ip 1;  #如何来源ip为0 ,1则设置成1
		}
		if ($ip = 0){
			rewrite ^(.*)$ /404error.html break;
		}
	}
}


[root@test3 code]# echo 1111 > index.html
[root@test3 code]# echo '网站正在维护' > 404error.html

网站被篡改 http劫持

bash 复制代码
test1作为正常的网站
test2作为劫持的网站

keepalived高可用

bash 复制代码
vrrp虚拟路由冗余协议

负载均衡
LB01   test
LB02   test3

安装ab压力测试工具

bash 复制代码
1.安装ab压力测试工具
yum install httpd-tools -y
ab -n 200 -c 2 http://127.0.0.1/
-n 要执行的请求数
-c 请求的并发数
-k 是否开启长连接

配置缓存

bash 复制代码
比如说一个图片,用户一旦访问,就让他缓存在浏览器
server {
	listen 80;
	server_name _;
	location ~.*\.(jpg|gif|png) {
			expires 7d;                #浏览器缓存七天
		}
}

如果开发代码没有正式上线,希望静态文件不被缓存

bash 复制代码
取消css.js.html等静态文件的缓存
location ~.*\.(css|js|html)$ {
add_header Cache-Control no-store;
add_head Pragma no-cache;
}

静态资源压缩

bash 复制代码
sendfile on;
tcp_nopush on; #大文件业务使用,比如说视频文件等
tcp_nodelay on; #小文件使用  使用这两个的前提是sendfile要开启

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    rewrite_log on;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;

nginx将响应报文发送至客户端之前启用压缩功能,然后进行传输,这能够有效的节约带宽,并提高响应至客户端的速度
gzip在传输前进行压缩,传输后解压

防盗链

bash 复制代码
test3承担服务器
test2承担盗链服务器
就是流量我承担了,钱你赚了


test3 有一张tomcat图片
[root@test3 ROOT]# cat /etc/nginx/conf.d/tomcat.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	location / {
		proxy_pass http://192.168.23.103:8080/;
	}
	location ~ \.(svg) {
		root /code/;
	}
}




test2
[root@test2 conf.d]# cat daolian.conf 
server {
	listen 80;
	server_name 192.168.23.102;
	root /code;
	index index.html;
}
[root@test2 code]# cat index.html 
<html>
	<head>
		<meta charset="utf-8">
		    <title>welcome to xx</title>
	</head>
	<body style="background-color:pink;">
		<center><img src="http://192.168.23.103/tomcat.svg"/></center>
	</body>
</html>

访问测试,这个所承载的流量全部是test3承担,他会记录是http://192.168.23.102/的
[root@test3 bin]# tail -n1 /var/log/nginx/access.log
192.168.23.1 - - [24/Nov/2024:10:26:46 +0800] "GET /tomcat.svg HTTP/1.1" 200 67795 "http://192.168.23.102/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36" "-"

配置防盗链

bash 复制代码
[root@test3 bin]# cat /etc/nginx/conf.d/tomcat.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	location / {
		proxy_pass http://192.168.23.103:8080/;
	}
	location ~ \.(svg) {
		root /code/;
 		valid_referers none blocked *.baidu.com *.goole.com;   #除了baidu和goole其他的不让访问,访问报403
		if ( $invalid_referer ) {
			return 403;
                 }

	}
}
bash 复制代码
如果用rewrite
[root@test3 bin]# cat /etc/nginx/conf.d/tomcat.conf 
server {
	listen 80;
	server_name 192.168.23.103;
	location / {
		proxy_pass http://192.168.23.103:8080/;
	}
	location ~ \.(svg) {
		root /code/;
 		valid_referers none blocked *.baidu.com *.goole.com;
		if ( $invalid_referer ) {
			rewrite ^(.*)$ http://www.zhuo.com/2.jfif break;
			#rewrite ^(.*)$ /code/1.png break;
                 }

	}
}

允许跨域访问

bash 复制代码
什么是跨域访问,当我们通过浏览器访问a网站时,同时会利用ajax或者是其他方式,同时也请求b网站,这样的话,就出现了请求一个页面,使用两个域名,这样的方式对浏览器来说是默认禁止的

cpu亲和力

bash 复制代码
让nginx自动的绑定在对应的cpu上
[root@test2 conf.d]# grep worker ../nginx.conf
worker_processes auto;
worker_cpu_affinity auto;
    worker_connections 1024;
[root@test2 conf.d]# ps -eo pid,args,psr|grep [n]ginx
  5040 nginx: master process /usr/   0
  5041 nginx: worker process         0
  5042 nginx: worker process 
相关推荐
ping某13 小时前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树882 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠2 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质2 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工2 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智2 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
程序员mine3 天前
HTTPS-TLS加密与证书完全指南(中)
网络协议·https·ssl
施努卡机器视觉3 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦3 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw