nginx的重定向

nginx的重定向

nginx的重定向(rewrite)

location 匹配

location匹配的就是后面的URI

/wordpress

192.168.39.50./wordpress

location匹配的分类和优先级*

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

2、正则匹配

^~ 前缀匹配,以什么为开头
 区分大小写的匹配

~* 不区分大小的匹配

!~ 区分大小写的取反

!~* 不区分大小的取反

3、一般匹配

location /字符串

精确匹配的优先级最高,正则,一般

优先级总结

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

实际网站中的使用规则:

1、

	location =/ {
		root html;
		index index.php index.html index.htm;
	}
	#一般为网站首页

2、必选的规则:处理静态请求的页面

	location ^~ /static/ {
		root /web/static/;
		index	index.html index.htm;
	}
	#用来匹配静态页面
	location ~* \.(jpg|gif|png|jpeg|css)$ {
	root /web/picturs/;
	index	index.html index.htm;
	}
	#访问图片或者指定后缀名

3、一般是通用规则,用来转发.php .js

为后缀的动态请求到后端服务器(数据库)

	location / {
		proxy_pass
	}
	#转发后端请求和负载均衡

rewrite重定向:

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

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

nginx的变量

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

$host 请求的主机名

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

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

$remote_addr 客户端的ip地址

$remote_posrt 客户端的端口号

$server_addr 服务端的IP地址

$server_port 服务端的端口号

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

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

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

x_Real-IP 客户端真实的IP地址

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 正则表达式 跳转后的内容 标志位;

rewrite /test1/(.*)/xy102/$1 permanent;
# 192.168.233.61/test1/index.html
192.168.233.61/xy102/index.html $1就是捕获组

rewrite or internal redirection cycle while processing?
在重定向的过程中,使用了last当时进行重定向,但是没有结束语,陷入了死循环,nginx会自动循环10次,last匹配最多只能执行10次,超过10次没有结束,就会停止,然后报错

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

location匹配的优先
location =
location ~
~*
location /
重定向
permanent
redriect
break
last
500报错 10次

相关推荐

  1. nginx定向

    2024-07-11 13:50:03       19 阅读
  2. Nginx定向

    2024-07-11 13:50:03       19 阅读
  3. Nginx定向

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

    2024-07-11 13:50:03       16 阅读
  5. 使用 Nginx 将 HTTP 定向到 HTTPS

    2024-07-11 13:50:03       42 阅读
  6. Nginx 中将 HTTP 定向到 HTTPS

    2024-07-11 13:50:03       34 阅读

最近更新

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

    2024-07-11 13:50:03       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

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

    2024-07-11 13:50:03       42 阅读
  4. Python语言-面向对象

    2024-07-11 13:50:03       53 阅读

热门阅读

  1. SpringBoot整合Easy-Es最佳实践

    2024-07-11 13:50:03       19 阅读
  2. SpringBoot防止重复提交 AOP+自定义注解+redis

    2024-07-11 13:50:03       16 阅读
  3. 在Spring Boot中实现多租户架构的数据隔离

    2024-07-11 13:50:03       18 阅读
  4. LeetCode --- 2119. A Number After a Double Reversal 解题报告

    2024-07-11 13:50:03       16 阅读
  5. sublime使用

    2024-07-11 13:50:03       17 阅读
  6. Linux Conda简介

    2024-07-11 13:50:03       17 阅读