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次

相关推荐
噫吁嚱!!!10 分钟前
Linux安装elasticsearch单机版
linux·运维·ubuntu
lshzdq18 分钟前
【CMake】基本概念和快速入门
linux·运维·服务器
江东大都督周总23 分钟前
nginx转发的问题
nginx
爱技术的小伙子26 分钟前
Linux 磁盘管理与分区:使用fdisk、parted、df、du等命令管理磁盘和分区
linux·运维·服务器
kinlon.liu32 分钟前
基于 Nginx + Spring Boot + Vue + JPA 的网站安全防护指南
网络·vue.js·spring boot·nginx·安全
wanhengwangluo43 分钟前
徐州服务器租用:论带宽的作用有哪些
运维·网络安全·数据安全
不喜欢就睡大觉1 小时前
Linux—网络设置
linux·运维·服务器
Roc-xb1 小时前
使用ifconfig命令获取当前服务器的内网IP地址
运维·服务器·tcp/ip
qq_1778036221 小时前
Windows系统服务器远程教程
运维·服务器·云计算·ddos·ip
我爱小白羊1 小时前
【Linux】进程优先级 + 环境变量
linux·运维·服务器