location匹配的优先级和重定向

nginx的重定向(rewrite)

location 匹配

location匹配的就是后面的uri

/wordpress 192.168.233.10/wordpress

location匹配的分类和优先级

1.精确匹配

location = / 对字符串进行完全匹配,必须完全符合

2.正则匹配

^-前缀级别,以什么为开头

~区分大小写的匹配

~*不区分大小写

!~:区分大小写的取反

!~*:不区分大小写的取反

3.一般匹配(通用匹配)

location /字符串

精确匹配的优先级最高,其次是正则,最后是一般。

优先级总结:

location = 完整路径 > location ^~ >location ~ location~* >location /部分起始位置 > location /

实际网站中的所有规则:

第一种:网站首页

location = / {
	root html;
	index index.heml index.htm index.php;
}

第二章:处理静态请求的页面

location ^~ /static/ {
	root /web/static/;
	index index.heml index.htm;
}

访问图片或者是指定的后缀名

location ~* \\.(jpg|gif|png|css) {
	root /web/pictures/;
	index index.heml index.htm;
}

第三章:一般是通用规则,原来转发.php .js为后缀的动态请求到后台服务器(数据库)。

转发后端请求和负载均衡

location / {

​ proxy_pass

}

rewrite重定向:

rewrite就是把当前访问的页面跳转到其他页面。

rewrite的工作方式:通过nginx的全局变量或者自定义变量,结合正则表达式和标志位实现url的重定向

nginx的变量

$uri 客户端请求的uri的地址

$host 请求的主机名

$http_user_angent 客户端请求的浏览器和操作系统

$http_referer 请求头的referer信息,表示当前页面来源的url

$remote_addr 客户端的IP地址

$remote_port 客户端的端口号

$server_addr 服务端的IP地址

$server_port 服务端的端口号

$request_method 获取客户端请求的方法

$scheme 显示请求的协议,要么是http要么是https

x_forwarded_for 用来获取请求头当中客户端的真实IP地址。代理服务器添加,在代理服务器当中指示客户端的IP地址

X-Real-IP:客户端真实的IP地址

vim nginx.conf

proxy_set_header X-Real-IP $remote_addr

加上这一字段,客户端的真实IP地址就会传递给后端服务器

标志位

flag

permanent:永久重定向,返回码是301,浏览器地址栏会显示跳转后的URL地址

redirect:临时重定向,返回码是302,浏览器地址栏会显示跳转后的URL地址

break:永久重定向,返回码也是301,但是它匹配到规则之后,不会在向下匹配其他规则,URL也不会发生变化

last:重定向,但是会继续向下匹配其他的location规则

rewrite的执行顺序:

1.server模块的rewrite优先级最高

2.匹配location的规则

3.执行选定的location规则

rewrite的语法:

rewrite 正则表达式 跳转后的内容 标志位;

在重定向的过程中,使用了last方式进行重定向,但是,没有结束语,陷入了死循环,nginx会自动循环10次,last最多只能循环10次,超过10次nginx就会自动结束,返回码500。

基于域名进行跳转,老的不用,但是依然能够访问,统统跳转到新的域名

在这里插入图片描述

vim /usr/local/nginx/conf/nginx.conf
server {
	listen 80;
	server_name www.xy102.com;
	charset utf-8;
	location / {
	root html;
	if ( $host = 'www.xy102.com'){
	rewrite ^/(.*)$ http://www.cj.com/$1 permanent;
	}
	index index.html;
	}
}
nginx -t 
systemctl restart nginx
#进入/etc/hosts做映射
vim /etc/hosts
192.168.233.10 www.xy102.com www.cj.com
#在index.html中添加内容
echo "" > /usr/local/nginx/html/index.html

基于客户端的ip进行跳转,公司新业务上线,测试阶段,

vim nginx.conf
set $rewrite true;
#设置应该变量名,rewrite,值是true
#来判断ip是否为合法ip
if ( $remote_addr = "192.168.233.10") {
	set $rewrite false;
}
if ( $rewrite = true ) {
	rewrite (.+) /error.html;
	#重定向,192.168.233.22/error.html
}
location = /error.html {
	root html;
}

在这里插入图片描述

location匹配的优先级

8.233.10") {
set $rewrite false;
}
if ( $rewrite = true ) {
rewrite (.+) /error.html;
#重定向,192.168.233.22/error.html
}
location = /error.html {
root html;
}

相关推荐

  1. Nginx配置文件中Location指令匹配优先级

    2024-07-12 14:04:04       30 阅读
  2. location.href window.location区别有这些!

    2024-07-12 14:04:04       29 阅读
  3. nginx定向

    2024-07-12 14:04:04       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 14:04:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 14:04:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 14:04:04       58 阅读
  4. Python语言-面向对象

    2024-07-12 14:04:04       69 阅读

热门阅读

  1. SQL注入:时间盲注

    2024-07-12 14:04:04       24 阅读
  2. Mybatis插件:IDEA中MyBatisCodeHelperPro插件下载安装

    2024-07-12 14:04:04       17 阅读
  3. spark中的floor函数

    2024-07-12 14:04:04       21 阅读
  4. 数据结构第21节 归并排序以及优化方案

    2024-07-12 14:04:04       21 阅读
  5. 手撕红黑树

    2024-07-12 14:04:04       20 阅读
  6. python程序打包.exe文件

    2024-07-12 14:04:04       21 阅读