nginx的重定向(rewrite)

nginx的重定向(rewrite)

location 匹配

location匹配的就是后面的URI

/wordpress

192.168.60.20/wordpress

location匹配的分类和优先级

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

2、正则匹配

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

~ 区分大小写的匹配

~* 不区分大小写

!~ 区分大小写的取反

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

3、一般匹配

location /字符串

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

优先级总结:
复制代码
location = 完整路径   >   location  ^~  >   location ~      >     location /部分起始位置    >   location /
                                           location ~*

第一个规则:实际万战中的使用规则:

复制代码
location  =   /  {
   root   html;
   index index.html index.htm index.php;
}
#网站首页

第二个必选的规则:处理静态请求的页面

复制代码
location ^~ /static/ {
  root /web/static;
  index index.html  index.htm;
}
#用来匹配静态页面

图片:

复制代码
loction ~* \.(jpg|gif|png|jpeg|css)$ {
   root /web/picture/;
   index index.html index.htm;
}
#访问图片或者是指定的后缀名

第三个规则:一般是通用规则,用来转发.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_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地址

标志位

flag

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

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

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

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

rewrite的执行顺序:

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

2、匹配location的规则

3、执行选定的location规则

rewrite的语法:

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

rewrite or internal refirection cycle while processing?

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

实验1:

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

操作:

1、配置域名跳转

复制代码
[root@test3 ~]# cd /usr/local/nginx/conf/
[root@test3 conf]# vim nginx.conf
   server {
        listen       80;
        server_name  www.xy102.com;
        charset utf-8;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            if ( $host = 'www.xy102.com' ) {
                rewrite ^/(.*)$ http://www.zxc.com/$1 permanent;
            }
            index  index.html index.htm;
        }

2、将ip地址进行域名映射

复制代码
[root@test3 ~]# vim /etc/hosts
192.168.60.30 www.xy102.com www.zxc.com

3、结果

实验2:

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

操作:

1、定义可以正常访问的ip地址

复制代码
 35     server {
 36         listen       80;
 37         server_name  www.xy102.com;
 38     
 39         charset utf-8;
 40     
 41         #access_log  logs/host.access.log  main;
 42         set $rewrite true;
 43         #设置一个变量名,rewrite,值是true
 44         #来进行判断ip是否是合法ip
 45         if ( $remote_addr = "192.168.60.30" ) {
 46           set $rewrite false;
 47         } 
 48         if ( $rewrite = true ) {
 49           rewrite (.+) /error.html;
 50           #重定向,192.168.60.30/error.html       
 51         }
 52         location = /error.html {
 53          root html;
 54         }

2、在html目录下写一个网页维护中

复制代码
[root@test3 html]# echo "网页维护中!" > error.html

3、test3结果

4、test4结果

location匹配的优先级

location = 精确

location ~ 正则

location / 一般

location /

重定向

permanent

refriect

break

last

500报错 10次

相关推荐
EMTime19 小时前
Docker运行OpenWRT
运维·docker·容器
lolo大魔王20 小时前
Linux 文件系统超全面详解(原理、结构、挂载、分区、inode、日志、管理命令)
linux·运维·服务器
zyl837211 天前
Docker 使用手册
运维·docker·容器
古月方枘Fry1 天前
MGRE实验
运维·服务器
stolentime1 天前
FreeDomain 本地开发环境快速搭建指南
运维·服务器·网络
bush41 天前
嵌入式linux学习记录四
linux·运维·学习
lihao lihao1 天前
软硬链接
linux·运维·服务器
TOWE technology1 天前
智能安防监控系统如何做好防雷?——视频信号SPD综合应用方案解析
运维·服务器·防雷产品·信号保护·信号防雷·spd
楼田莉子1 天前
Docker学习:Docker介绍及其架构介绍
运维·后端·学习·docker·容器·架构
大明者省1 天前
IIS 端口绑定正常访问的原理说明与常见误区澄清
运维·服务器·笔记