Nginx-Location匹配与Rewrite重写跳转

本文详细介绍了Nginx中的Location匹配规则及其优先级,包括精准匹配、一般匹配和正则匹配,并提供了实际配置示例,如处理静态文件和动态请求。此外,还深入讲解了Rewrite重写规则,展示了基于域名、客户端IP、旧域名跳转、参数匹配和目录文件跳转的实例,帮助读者理解Nginx的URL重定向和流量控制。
摘要由CSDN通过智能技术生成

一、Location

1. location分类

  • 精准匹配:location = / {}
  • 一般匹配:location / {}
  • 正则匹配:location ~ / {}

2. 常用匹配规则

= :进行普通字符精确匹配,也就是完全匹配。
^~ :表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它location。
~ :区分大小写的匹配。
~* :不区分大小写的匹配。
!~ :区分大小写的匹配取非。
!~* :不区分大小写的匹配取非。

3. 优先级

首先精确匹配 =
其次前缀匹配 ^~
其次是按文件中顺序的正则匹配 ~或~*
然后匹配不带任何修饰的前缀匹配
最后交给"/"通用匹配

4. 示例说明

在这里插入图片描述

5. 三个匹配规则定义

规则一

直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。

location = / {
    proxy_pass http://tomcat_server/;
}

规则二

处理静态文件请求,这是nginx作为http服务器的强项
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用

location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

规则三

通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
非静态文件请求就默认是动态请求

location / {
    proxy_pass http://tomcat_server;
}

二、Rewrite示例

1. 基于域名的跳转

要求:现在公司旧域名www.ng.com有业务需求变更,需要使用新域名www.kk.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.ng.com;		                             #域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.ng.com.access.log;		     #日志修改
	location / {
	#添加域名重定向
        if ($host = 'www.ng.com'){						         #$host为rewrite全局变量,代表请求主机头字段或主机名
			rewrite ^/(.*)$ http://www.kk.com/$1 permanent;		 #$1为正则匹配的内容,即域名后边的字符串
        }
        root   html;
        index  index.html index.htm;
    }
}

在这里插入图片描述

echo "192.168.117.30 www.ng.com www.kk.com" >> /etc/hosts
mkdir -p /var/log/nginx
systemctl restart nginx

在这里插入图片描述
在这里插入图片描述

mkdir /usr/local/nginx/html/test
echo 'Hello Baby' > /usr/local/nginx/html/test/1.html
systemctl restart nginx

在这里插入图片描述

2. 基于客户端IP访问跳转

要求:所有IP访问任何内容都显示一个固定维护页面,只有IP:192.168.117.30访问正常

vim /usr/local/nginx/conf/nginx.conf

server {
	listen       80;
	server_name  www.ng.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.ng.com-access.log;		#日志修改

	#设置是否合法的IP标记
    set $rewrite true;							#设置变量$rewrite,变量值为boole值true
    #判断是否为合法IP
	if ($remote_addr = "192.168.117.30"){		#当客户端IP为192.168.117.30时,将变量值设为false,不进行重写
        set $rewrite false;
    }
	#除了合法IP,其它都是非法IP,进行重写跳转维护页面
    if ($rewrite = true){						#当变量值为true时,进行重写
        rewrite (.+) /weihu.html;				#重写在访问IP后边插入/weihu.html,例如192.168.117.31/weihu.html
    }
    location = /weihu.html {
        root /var/www/html;						#网页返回/var/www/html/weihu.html的内容
    }
	
	location / {
        root   html;
        index  index.html index.htm;
    }
    
mkdir -p /var/www/html/
echo 'weihu!' > /var/www/html/weihu.html
systemctl restart nginx

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 基于旧域名跳转到新域名后面加目录

现在访问的是 http://kk.ng.com,现在需要将这个域名下面的访问都跳转到http://www.ng.com/kk

vim /usr/local/nginx/conf/nginx.conf
	listen       80;
	server_name  kk.ng.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.ng.com-access.log;
	#添加
	location /post {
        rewrite (.+) http://www.ng.com/kk$1 permanent;		#这里的$1为位置变量,代表/post
    }
	location / {
        root   html;
        index  index.html index.htm;
    }

在这里插入图片描述

mkdir -p /usr/local/nginx/html/def/post
echo 'this is 1.html' > /usr/local/nginx/html/kk/post/1.html
echo "192.168.117.30 kk.ng.com www.ng.com" > /etc/hosts
mkdir -p /usr/local/nginx/html/kk/post
systemctl restart nginx.service 

访问http://kk.ng.com/post/1.html跳转到http://www.ng.com/kk/post/1.html

在这里插入图片描述

4. 基于参数匹配的跳转

访问http://www.ng.com/100-(100|200)-100.html 跳转到http://www.ng.com页面

vim /usr/local/nginx/conf/nginx.conf

server {
	listen       80;
	server_name  www.ng.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.ng.com.access.log;
	
	if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.+) http://www.ng.com permanent;
    }

	location / {
        root   html;
        index  index.html index.htm;
    }
}




在这里插入图片描述

echo "192.168.117.30 www.ng.com" >> /etc/hosts
systemctl restart nginx
浏览器访问
访问http://www.ng.com/100-100-100.html跳转到http://www.ng.com页面

在这里插入图片描述

5. 基于目录下所有php结尾的文件跳转

要求:访问 http://www.ng.com/upload/ng.php 跳转到首页

vim /usr/local/nginx/conf/nginx.conf

server {
	listen       80;
	server_name  www.ng.com;
	charset utf-8;
	access_log  /var/log/nginx/www.ng.com.access.log;
	
location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.ng.com permanent;
}

location / {
    root   html;
    index  index.html index.htm;
}
}

在这里插入图片描述

echo "192.168.117.30 www.ng.com" >> /etc/hosts
systemctl restart nginx
访问http://www.ng.com/upload/abc.php跳转到http://www.ng.com页面

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值