Nginx的重定向

一、location匹配的分类和优先级

location匹配的就是后面的URI

1.1 匹配的类型

  1. 精确匹配

location =/ {}

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

  1. 正则匹配

location ~ 匹配条件 {}

  • ^~ :前缀匹配,以什么为开头

  • ~ :区分大小写的匹配

  • ~* :不区分大小写

  • !~ :区分大小写的取反

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

  1. 一般匹配

location /字符串 {}

整体上的优先级是精确匹配 > 正则匹配 > 一般匹配

1.2 匹配的优先级

​ 优先级总结:从高到低

  • 精确匹配:location = 完整路径 {}
  • 正则匹配:location ^~ 匹配内容 {}
  • 正则匹配:location ~ 匹配内容 {} ;location ~* 匹配内容 {}
  • 一般匹配:location /部分起始位置
  • 通用匹配:location /

1.3 实际网站中的使用规则

  1. 网站首页:location = / {}

    精确匹配网站首页

    location = / {
    	root html;
    	index index.html index.htm index.php;
    }
    
  2. 必选规则:处理静态请求的页面

    用来匹配静态页面

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

    用来匹配图片

    location ~* \.(jpg|gif|png|jpeg|css)$ {
    	root /web/pictures/;
    	index index.html index.htm;
    }
    
  3. 一般是通用规则,用来转发.php .js 为后缀的动态请求到后端服务器(数据库)

    location / {
    	proxy_pass ...
    }
    

    转发后端转发和负载均衡

二、rewrite重定向

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

rewrite的工作方式

通过Nginx的全局变量或者自定义变量,结合正则表达式和标志位实现URL的重定向。

2.1 Nginx的常用变量

  • &uri :客户端请求的uri的地址

  • $host :客户端请求的主机名

  • $http_user_agent :客户端请求的浏览器和操作系统

  • $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地址

proxy_set_header X-Real_IP $remote_addr加上这个字段,客户端的真实IP地址就会传递给后端服务器。

2.2 标志位(flag)

  • permanent :永久重定向,返回码是301,浏览器地址栏会显示跳转后的URL地址
  • redirect :临时重定向,返回码是302,浏览器地址栏会显示跳转后的URL地址
  • break :永久重定向,返回码是301,但是break匹配到规则之后不会再向下匹配其他规则,URL也不会发生变化
  • last :重定向,但是会继续向下匹配其他location的规则

2.3 rewrite的执行顺序

  1. server模块的rewrite优先级最高
  2. 匹配location的规则
  3. 执行选定的location规则

2.4 rewrite的语法

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

特别的:使用last重定向时,如果没有设置结束语,陷入死循环,Nginx会自动循环10次,超过10次没有结束,就会返回码500

rewrite /test2/(.*) /test1/$1 break;

例:基于域名进行跳转

老的域名不用了,但是依然能够访问,统统跳转到新的域名,最简单也最常用,只更换域名,不必更换页面内容

server{
	listen 80;
	server_name www.test1.com;
	
	charset utf-8;
	
	location / {
		root html;
		if ( $host = "www.test1.com" ) {
			rewrite ^(.*)$ http://www.ykw.com/$1 permanent;
		}
		index index.html index.htm;
	}
}

例:基于客户端的IP进行跳转

场景:公司有新业务上限,测试阶段,其他的IP访问全都显示维护中,只有20.0.0.10(本机)可以正常访问。

server{
	listen 80;
	server_name www.test1.com;
	charset utf-8;
	set $rewrite true;
	#设置一个变量名,rewrite,值为true
	#来进行判断IP是否是合法IP
	if ( $remote_addr = "20.0.0.10" ) {
		set $rewrite false;
	}
	if ( $rewrite = true ) {
		rewrite (.+) /error.html redir;
	}
	location / {
		root html;
		index index.html index.htm;
	}
}
echo "网页维护中!" > error.html

例:基于目录下所有 php 结尾的文件跳转

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;		#域名修改	
	charset utf-8;
	access_log /var/log/nginx/www.test.com-access.log;
	location ~* /upload/.*\.php$ {
		rewrite (.+) http://www.test.com permanent;
	}
 	location / {
		root   html;
    	index  index.html index.htm;
	}
}

systemctl restart nginx

访问http://www.test.com/upload/123.php,直接跳转到www.test.com

相关推荐

  1. nginx定向

    2024-07-15 18:18:03       18 阅读
  2. Nginx定向

    2024-07-15 18:18:03       19 阅读
  3. Nginx定向

    2024-07-15 18:18:03       20 阅读
  4. nginx+lua 实现URL定向(根据传入参数条件)

    2024-07-15 18:18:03       16 阅读
  5. 使用 Nginx 将 HTTP 定向到 HTTPS

    2024-07-15 18:18:03       42 阅读
  6. Nginx 中将 HTTP 定向到 HTTPS

    2024-07-15 18:18:03       34 阅读

最近更新

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

    2024-07-15 18:18:03       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 18:18:03       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 18:18:03       42 阅读
  4. Python语言-面向对象

    2024-07-15 18:18:03       53 阅读

热门阅读

  1. websocket中的STOMP 协议:sockjs-client 和 stompjs

    2024-07-15 18:18:03       22 阅读
  2. 等保测评的智能化与自动化展望

    2024-07-15 18:18:03       22 阅读
  3. 程序员养生指南:守护健康,高效编码

    2024-07-15 18:18:03       11 阅读
  4. YoloV8改进策略:卷积篇Kan行天下之小波Kan

    2024-07-15 18:18:03       14 阅读
  5. FastJson详解

    2024-07-15 18:18:03       15 阅读
  6. HTML-VUE页面调用android 客户端网络请求并返回数据

    2024-07-15 18:18:03       15 阅读
  7. C++ 左值与右值

    2024-07-15 18:18:03       15 阅读
  8. 网络协同新纪元:Eureka引领分布式网络管理革命

    2024-07-15 18:18:03       15 阅读
  9. deepstream tracker NvDCF未实现跟踪

    2024-07-15 18:18:03       16 阅读
  10. Mybatis

    Mybatis

    2024-07-15 18:18:03      12 阅读
  11. Kafka 入门指南

    2024-07-15 18:18:03       11 阅读
  12. 【redis】redis发布/订阅模型

    2024-07-15 18:18:03       19 阅读