目录

一、nginx的location匹配

1、nginx的正则表达式

2、location的常用匹配规则

3、location匹配的优先级

4、location的示例

二、rwrite

1、rwrite跳转

2、rwrite执行顺序

3、rwrite语法格式

4、flag标记说明

三、rewrite实例

1、基于域名的跳转

2、基于客户端ip访问跳转

3、基于旧域名转到新域名后面加目录

4、基于参数匹配的跳转


一、nginx的location匹配

1、nginx的正则表达式

(1)^

匹配字符串起始的位置

(2)$

匹配字符串结束的位置

(3)*

匹配前面的字符零次或多次 比如"cd*" 能匹配“c”及“cd”、“cdd”

(4)+

匹配前面的字符一次或多次。如“cd+”能匹配“cd”及“cdd”、“cddd”,但不能匹配“c”

(5)?

匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”

(6).

匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式

(7)\

将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“$”则匹配“$”

(8)\d

匹配纯数字[0-9] \s :空白符 \w :任意单词字符包括下划线[A-Za-z0-9_]

(9){n}

重复 n 次

(10){n,}

重复 n 次或更多次

(11){n,m}

重复n次到m次(其中m大于等于n)

(12)[ ]

定义匹配的字符范围

(13)[c]

匹配当个字符c

(14)[a-z]

匹配 a-z 小写字母的任意一个

(15)[a-zA-Z0-9]

匹配所有大小写字母或数字

(16)()

表达式的开始和结束位置

(17)|

或运算

2、location的常用匹配规则

(1)=

进行普通字符精确匹配,也就是完全匹配。

(2)^~

表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它 location。

(3)~ 

区分大小写的匹配。

(4)~*

不区分大小写的匹配。

(5)!~

区分大小写的匹配取非。

(6)!~*

不区分大小写的匹配取非。

3、location匹配的优先级

(1)首先精确匹配 =

(2)其次前缀匹配 ^~

(3)其次是按文件中顺序的正则匹配 ~或~*

(4)然后匹配不带任何修饰的前缀匹配 最后是交给 / 通用匹配

4、location的实例

(1)location = / {}

=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配 再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location  /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。

(2)location / {}

因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配, 但若后面是正则表达式会和最长字符串优先匹配(最长匹配)

(3)location /documents/ {}

匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location 只有其它 location后面的正则表达式没有匹配到时,才会采用这一条

(4)location /documents/abc {}

匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location 只有其它 location后面的正则表达式没有匹配到时,才会采用这一条

(5)location ^~ /images/ {}

匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条

(6)location ~* .(gif|jpg|jpeg)$ {}

匹配所有以 gif、jpg或jpeg 结尾的请求 然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则

(7)location /images/abc {}

最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在

(8)location ~ /images/abc {}

匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条

(9)location /images/abc/1.html {}

匹配/images/abc/1.html 文件,如果和正则location ~ /images/abc/1.html 相比,正则优先级更高

二、rwrite

rwrite的定义

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。 比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。

rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用, 例如 http://www.abc.com/abc/bbs/index.php?a=1&b=2 只对/abc/bbs/index.php重写。

1、rewrite跳转

Nginx:通过ngx_http_rewrite_module 模块支持URL重写、支持if条件判断,但不支持else 跳转:从一个 location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误 PCRE支持:perl兼容正则表达式的语法规则匹配 重写模块 set 指令:创建新的变量并设其值

2、rewrite执行顺序

(1) 执行 server 块里面的 rewrite 指令。

(2) 执行 location 匹配。

(3) 执行选定的 location 中的 rewrite 指令。

3、rewrite的语法格式

rewrite <regex> <replacement> [flag];

(1)regex

表示正则匹配规则。

(2)replacement

表示跳转后的内容。

(3)flag

表示 rewrite 支持的 flag 标记。

4、flag标记说明

(1)last

本条规则匹配完成后,不终止重写后的url匹配,一般用在 server 和 if 中。

(2)break

本条规则匹配完成即终止,终止重写后的url匹配,一般使用在 location 中。

(3)redirect

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

(4)permanent 

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

三、rwrite实例

1、基于域名的跳转

vim /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.abc.com;

        #charset koi8-r;

        access_log  /var/log/nginx/def.com.access.log;

        location / {
        if ($host = 'www.def.com'){
        rewrite ^/(.+)$ http://www.abc.com/$1 permanent;
}

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx 移动端跳转到指定页面

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx 移动端跳转到指定页面_02

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_bc_03

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_bc_04

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_05

  

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_06

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_07

 2、基于客户端ip访问跳转

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

 server {
        listen       80;
        server_name  www.abc.com;

        charset utf-8;

        access_log  /var/log/nginx/abc.com.access.log;
        set $rewrite true;
           if ($remote_addr = '192.168.222.10'){
           set $rewrite false;
  }
       if ($rewrite = true){
          rewrite (.+) /weihu.html;
 }
      location = /weihu.html{
      root   /var/www/html;
 }

        location / {
            root   html;
            index  index.html index.htm;
        }

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_08

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_09

  

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx 移动端跳转到指定页面_10

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_bc_11

 3、基于旧域名跳转到新域名后面追加目录

vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.def.abc.com;

        charset utf-8;

        access_log  /var/log/nginx/abc.com.access.log;

        location /post{
         rewrite (.+) http://www.abc.com/def$1 permanent;
       }

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx 移动端跳转到指定页面_12

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_13

  

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_html_14

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx_15

 

4、基于参数匹配的跳转

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

 server {
        listen       80;
        server_name  www.abc.com;

        charset utf-8;

        access_log  /var/log/nginx/abc.com.access.log;
        if ($request_uri ~ ^/100-(100|200)\-(\d)+\.html$) {
            rewrite (.+) http://www.abc.com permanent;
       }

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_运维_16

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_运维_17

5、基于所有的php结尾的文件跳转

vim /usr/local/nginx/conf/nginx.conf
  server {
        listen       80;
        server_name  www.abc.com;

        charset utf-8;

        access_log  /var/log/nginx/abc.com.access.log;

        location ~* ^/upload.*\.php$ {
        rewrite (.+) http://www.abc.com permanent;
      }

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_bc_18

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx 移动端跳转到指定页面_19

6、基于最普通的url进行跳转 

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


    server {
        listen       80;
        server_name  www.abc.com;

        charset utf-8;

        access_log  /var/log/nginx/abc.com.access.log;

        location ~* ^/def/ghi.html {
        rewrite (.+) http://www.abc.com permanent;
      }

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_html_20

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_运维_21

 

nginx 移动端跳转到指定页面 nginx跳转到另一个ip_nginx 移动端跳转到指定页面_22