返回顶部
分享到

nginx中重定向的实现

nginx 来源:互联网 作者:佚名 发布时间:2025-03-02 18:03:29 人浏览
摘要

一、location 1、 location匹配 location匹配的就是后面的URI /wordpress 192.168.100.11/wordpress 2、 location匹配的分类 2.1 精确匹配 location = / 对字符串进行完全匹配,必须完全符合 2.2 正则匹配 ^~前缀匹配,

一、location

1、 location匹配

location匹配的就是后面的URI

/wordpress

192.168.100.11/wordpress

2、 location匹配的分类

2.1 精确匹配

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

2.2 正则匹配

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

~ 区分大小写的匹配

~* 不区分大小写

!~:区分大小写的取反

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

2.3 一般匹配(通用匹配)

location /字符串

3、location匹配优先级*

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

3.1 匹配优先级实验

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

systemctl stop firewalld

setenforce 0

systemctl restart nginx

cd /usr/local/nginx/html/

把照片拖进去

 

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

location = /1.jpg {

   root    /data/nginx/static1;

   index   index.html;

}

location /1.jpg {

   root   /data/nginx/static2;

   index  index.html;

}

location ~*\.(jpglgif)$ {

    root   /data/nginx/static3;

    index  index.html;

}

wq!

 

nginx -t

systemctl restart nginx

 

mkdir -p /data/nginx/static1

cd /data/nginx   #查看只有一个static1

mkdir static2 static3   #查看此时有static1 static2 static3

cd /usr/local/nginx/static1/

把1.jpg拖进去

cd /usr/local/nginx/static2/

把2.jpg拖进去

cd /usr/local/nginx/static3/

把3.jpg拖进去

[root@localhost static3]# mv 3.jpg 1.jpg

[root@localhost static3]# ls

1.jpg

 

页面访问

192.168.100.11/1.jpg   # 此时访问的是1.jpg的图片

 

页面访问

把精确匹配注释掉

nginx -t

systemctl restart nginx

192.168.100.11/1.jpg   # 此时访问的是3.jpg的图片

3.2 优先级总结

location = 完整路径 = 1.jpg即完整的一个字也不能少

location ^~、location ~ location ~* 、location /(字符串)部分起始位置、location /

4、实际网站中的使用规则

第一个必选规则

网站首页用精确等于杠(= /)

1.1 网站首页

1

2

3

4

5

location = / {

   root html;

   index index.html index.htm  index.php;

}

# = / 表示家目录的开头,访问= /相当于访问网站首页

第二个必选规则

处理静态请求的页面

1.2 用来匹配静态页面

1

2

3

4

location ^~ /static/ {

    root /web/static/;

    index index.html index.htm;

}

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

1

2

3

4

location ~* \.(jpg|gif|jpeg|png|css)$ {

    root /web/pictures/;

    index index.html index.htm;

}

第三个必须规则

一般是通用规则,用来转发.php .js为后缀的动态请求到后端服务器(数据库)

1.4 转发后端请求和负载均衡

1

2

3

location / {

  proxy_pass

}

二、rewrite重定向

1、概念

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

2、工作方式

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

3、nginx的变量

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

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

4、nginx真实ip地址访问设置

1

2

3

vim nginx.conf

proxy_set_header X Real-IP $remote_addr

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

操作

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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

在server模块修改,修改如下

 

location / {

   root   html;

   default_type text/plain;

   return 200 "ip:$remote_addr";

}

wq!

 

systemctl restart nginx

 

页面访问 192.168.100.11 

#此时显示的是本机地址 ip 192.168.100.1

 

# return 200 "ip:$remote_addr\ntest1:$host\nport:$remote_port\nxieyi:$scheme";

 

systemctl restart nginx

页面访问

# ip:192.168.100.1

# test1:192.168.100.11

# port:51360

# xieyi:http

5、标志位(flag)

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

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

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

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

6、rewrite的执行顺序

6.1 server模块的rewrite的优先级最高

6.2 匹配location的规则

6.3 执行选定的location规则

7、rewrite的语法

1

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

7.1 实验

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

cd /usr/local/nginx/html

mkdir test1

mkdir xy102

cd /usr/local/nginx/html/test1

echo 123 > index.html

cd /usr/local/nginx/html/xy102

echo 456 > index.html

# 查看一下test1和xy102目录下的index.html是否创建

 

(1)

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

在server模块修改,修改如下

 

location / {

   root   html;

   rewrite /test1/(.*) /xy102/$1 permanent;

#192.168.100.11/test1/index.html  192.168.100.11/xy102/index.html $1就是捕获组

   index index.html;

}

wq!

 

nginx -t

systemctl restart nginx

 

页面访问

192.168.100.11/test1

# 访问结果为456,网站ip跳转为192.168.100.11/xy102

 

(2)

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

在server模块修改,修改如下

 

location /test1 {

   root   html;

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

   index index.html;

}

location /test2 {

   root   html;

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

   index index.html;

}

wq!

 

nginx -t

systemctl restart nginx

cd /usr/local/nginx/html

mkdir test2

cd /usr/local/nginx/html/test2

echo 789 > index.html

 

页面访问

192.168.100.11/test1

# 访问结果为500

 

此时再打开一台192.168.100.11的shell终端

[root@localhost opt]# tail -f /usr/local/nginx/logs/error.log

页面继续访问 192.168.100.11/test1

xshell报错中出现以下一句话:rewrite or internal redirection cycle while processing

解释:

# 表示在重定向的过程中,使用了last方式进行重定向,但是,没有结束语,陷入了死循环,nginx会自动循环10次,last匹配最多只能执行10次,超过10次没有结束,就会停止,然后报错500。

7.2 基于域名进行跳转,老的不用了的域名,但是依然可以访问,统统跳转到新的域名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

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

在server模块修改,修改如下

 

    # 修改 server_name如下

    server_name   www.xy102.com;

     

    # 取消charset koi8-r;的注释,修改如下

    charset ulf-8;

 

location / {

   root   html;

   if ( $host = 'www.xy102.com' ) {

      rewrite ^/(.*)$ http://www.xiaodai.com/$1 permanent;

   }

   index index.html;

}

wq!

 

nginx -t

systemctl restart nginx

 

做域名映射

vim /etc/hosts

192.168.100.11 www.xy102.com www.xiaodai.com

wq!

# 访问www.xy102.com/index.html就是访问www.xiaodai.com/index.html

 

cd /usr/local/nginx/html

echo "this is new page" > index.html

 

页面访问(在虚拟机)

www.xy102.com

# 打印结果为 this is new page  网页地址跳转www.xiaodai.com

7.3 基于客户端的ip进行跳转,公司有新业务上线,测试阶段,其他ip只能显示维护中,只有192.1168.100.11能正常访问

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

vim /usr/local/nginx/cong/ngin.conf

在server模块的access_log下添加

 

set $rewrite true;

# 设置一个变量名,rewrite,值是ture

# 来进行判断ip是否是合法ip

if ( $remote_addr = "192.168.100.11" ) {

      set $rewrite false;

}

if ( $rewrite = true ) {

      rewrite (.+) /error.html;

      # 重定向,192.168.100.13/error.html

}

location = /error.html {

   root html;

    

}

wq!

 

cd /usr/local/nginx/html

echo "网页维护中" > error.html

 

nginx -t

systemctl restart nginx

 

页面访问

192.168.100.11    #打印结果为 页面维护中

总结·:

location匹配的优先级


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • nginx中重定向的实现
    一、location 1、 location匹配 location匹配的就是后面的URI /wordpress 192.168.100.11/wordpress 2、 location匹配的分类 2.1 精确匹配 location = / 对字符串进行
  • nginx配置多域名共用服务器80端口
    多个域名,比如两个域名,这两个域名其实共用一台服务器(意味着域名解析到同一个IP),一个域名为abc.com(可以是http://abc.com或者www.a
  • nginx upstream六种方式分配介绍
    1 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 2 weight 指定轮询几率,weight和访问
  • Nginx之upstream被动式重试机制的实现
    我们使用Nginx通过反向代理做负载均衡时,如果被代理的其中一个服务发生错误或者超时的时候,通常希望Nginx自动重试其他的服务,从而实
  • Nginx搭建自己的CDN服务器的方法
    nginx安装 1 2 sudo apt update sudo apt install nginx nginx配置文件 /etc/nginx/nginx.conf或/etc/nginx/sites-available/default 设置缓存 在Nginx配置中启用缓存,这样
  • Nginx实现动态封禁IP的步骤
    在日常的生产环境中,网站可能会遭遇恶意请求、DDoS 攻击或其他有害的访问行为。为了应对这些情况,动态封禁 IP 是一项十分重要的安全
  • Nginx HTTP Status 400 –错误的请求问题解决

    Nginx HTTP Status 400 –错误的请求问题解决
    第一:实验故障 今天小编在搭建完Nginx反向代理两个Tomcat站点,实现负载均衡的实验,访问网页内容时,提示如下报错: HTTP Status 400 错误的
  • nginx反向代理java项目方式
    nginx反向代理java项目 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server { listen 80; server_name ***.***.com; location / { proxy_pass http://127.0.0.1:8686; } #一键申请SSL证
  • 使用Nginx解决前端跨域问题
    1. 理解 CORS 和同源策略 1.1 同源策略 同源策略是一种浏览器安全机制,用于阻止不同源(不同域名、协议或端口)的 Web 应用相互访问数据。
  • Nginx如何集成到Windows服务

    Nginx如何集成到Windows服务
    Nginx集成到Windows服务 nginx版本:nginx-1.20.2; windows版本:win11,将nginx解压,要保证nginx的目录不含空格 下载winsw 下载地址为: 官方地址:
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计