文章目录

  • 前言:
  • 一、常用的 Nginx 正则表达式
  • 二、location
  • (一)、location 大致可以分为三类:
  • (二)、location 常用的匹配规则:
  • (三)、location 优先级:
  • (四)、location 示例说明
  • (五)、实际网站使用中,至少有三个匹配规则定义:
  • 三、Rewrite简介
  • (一)、Rewrite跳转场景
  • 四、Rewrite 跳转实现
  • 五、rewrite
  • (一)、rewrite功能
  • (二)、rewrite 执行顺序如下:
  • (三)、语法 rewrite [flag];
  • (四)、rewrite示例
  • 1、基于域名的跳转
  • 2、基于客户端 IP 访问跳转
  • 3、基于旧域名跳转到新域名后面加目录
  • 4、基于参数匹配的跳转
  • 5、基于目录下所有 php 结尾的文件跳转
  • 6、基于最普通一条 url 请求的跳转


前言:

nginx通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else(所以if判断语句为单分支)。

另外该模块需要PCRE支持,应在编译nginx时指定PCRE支持。根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后nginx将返回500错误。

同时,重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。

一、常用的 Nginx 正则表达式

NGINX redirect 没有端口 nginx redirect配置_正则表达式


从功能看 rewrite 和 location 似乎有点像,都能实现跳转,主要区别在于 rewrite 是在同一域,location 是对一类路径做控制访问或反向代理,还可以 proxy_pass 到其他机器。

二、location

(一)、location 大致可以分为三类:

精准匹配:location = / {…}
一般匹配:location / {…}
正则匹配:location ~ / {…}

(二)、location 常用的匹配规则:

=:进行普通字符精确匹配,也就是完全匹配
^~:表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他 location
~:区分大小写的匹配
~*:不区分大小写的匹配
!~:区分大小写的匹配取非
!~*:不区分大小写的匹配取非

(三)、location 优先级:

首先精确匹配 =
其次前缀匹配 ^~
其次是按文件中顺序的正则匹配 ~或~*
然后匹配不带任何修饰的前缀匹配
最后是交给 / 通用匹配

(四)、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相比,正则优先级更高

优先级总结:
(location = 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (location /)

(五)、实际网站使用中,至少有三个匹配规则定义:

#第一个必选规则
直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
可以是一“个静态首页,也可以直接转发给后端应用服务器
location = / {
   root  html;
   index index.html index. htm;
}

#第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
   root /webroot/static/ ;
}

location ~* \. (html Igif ljpg ljpeglpnglcssljslico)$ {
   root /webroot/res/ ;
}

#第三个规则就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
非静态文件请求就默认是动态请求
location / {
   proxy_pass http://tomcat_server;
}

三、Rewrite简介

(一)、Rewrite跳转场景

1、调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求。
2、为了让搜索引擎搜寻网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
3、网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的 360buy.com 会跳转到 jd.com。
4、服务端某些业务调整,比如根据特殊变量、目录、客户端的信息进行URL调整等。

四、Rewrite 跳转实现

Nginx:通过ngx_http_rewrite_module 模块支持URL重写、支持if条件判断,但不支持else

跳转:从一个location跳转到另一个location, 循环最多可以执行10次,超过后nginx将返回500错误

PCRE支持:perl兼容正则表达式的语法规则匹配

重写模块 set 指令:创建新的变量并设其值

NGINX redirect 没有端口 nginx redirect配置_运维_02

五、rewrite

(一)、rewrite功能

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

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

(二)、rewrite 执行顺序如下:

(1) 执行 server 块里面的rewrite 指令。
(2) 执行 location 匹配。
(3) 执行选定的 location 中的rewrite 指令。

(三)、语法 rewrite [flag];

regex:表示正则匹配规则。
replacement:表示跳转后的内容。
flag:表示 rewrite 支持的 flag 标记。

--------------flag标记说明---------------
last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和 if 中。
break:本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在location中。
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址。
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。

(四)、rewrite示例

1、基于域名的跳转

现在公司旧域名www.gcc.com有业务需求变更,需要使用新域名www.benet.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。

mkdir -p /var/log/nginx/

vim  /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.gcc.com;     #域名修改

        #charset koi8-r;

        access_log  /var/log/nginx/www.gcc.com-access.log;     #日志保存路径修改

        location / {
             if ($host = 'www.gcc.com'){       #$host为rewrite全局变量,代表请求主机头字段或主机名
                rewrite ^/(.*)$ http://www.benet.com/$1 permanent;   #$1为正则匹配得内容,即域名后边得字符串
            }
            root   html;
            index  index.html index.htm;
        }

echo "192.168.200.50 www.gcc.com" >> /etc/hosts
echo "192.168.200.50 www.benet.com" >> /etc/hosts
systemctl restart nginx.service

此时访问 http://www.gcc.com 时会自动跳转到 www.benet.com 上进行访问。

NGINX redirect 没有端口 nginx redirect配置_nginx_03

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

今天公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司 IP 192.168.200.50访问正常。

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

        #charset koi8-r;

        access_log  /var/log/nginx/www.gcc.com-access.log;
#设置是否是合法的IP标记
        set $rewrite true;     #设置变量$rewrite,变量值为boole值true
#判断是否为合法IP
        if ($remote_addr = "192.168.200.50"){     #当客户端IP为192.168.200.50时,将变量值设为flase,不进行重写
            set $rewrite false;
        }
#除了合法IP,其它都是非法IP,进行重写跳转到维护页面
        if ($rewrite = true){      #当变量值为true时,进行重写
              rewrite (.+) /weihu.html;    #重写在访问IP后边插入/weihu.html,例如192.168.200.40/weihu.html
        }
        location = /weihu.html {
            root /var/www/html;      #页面返回/var/www/html/weihu.html的内容
        }
        location / {
            root   html;
            index  index.html index.htm;
        }


mkdir -p /var/www/html
echo '<h1>this is weihu web!</h1>' > /var/www/html/weihu.html
systemctl restart nginx.service

此时用另外一台机子访问192.168.200.50时会跳转到weihu.html界面,而只有IP为192.168.200.50的机子才可以正常进行访问。

NGINX redirect 没有端口 nginx redirect配置_运维_04

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

当访问的是 http://bbs.gcc.com,现在需要将这个域名下面的访问都跳转到 http://www.gcc.com/bbs下

vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  bbs.gcc.com;   #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.gcc.com-access.log;
        #添加
        location /post {
          rewrite (.+) http://www.gcc.com/bbs$1 permanent;    #这里$1为位置变量,代表/post
        }
        location / {
            root   html;
            index  index.html index.htm;
        }


[root@localhost ~]#mkdir -p /usr/local/nginx/html/bbs/post
[root@localhost ~]#echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
[root@localhost ~]#echo "192.168.200.50 bbs.gcc.com" >> /etc/hosts
[root@localhost ~]#systemctl restart nginx.service 
[root@localhost ~]#systemctl restart nginx.service

此时用浏览器访问 http://bbs.gcc.com/post/1.html 会自动跳转到 http://www.gcc.com/bbs/post/1.html

NGINX redirect 没有端口 nginx redirect配置_运维_05

4、基于参数匹配的跳转

现在访问 http://www.gcc.com/100-(100|200)-100.html 会跳转到 http://www.gcc.com的页面

vim /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.gcc.com;    #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.gcc.com-access.log;

        if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$){   #设置正则匹配
            rewrite (.*) http://www.gcc.com permanent;     #设置重写
        }
        location / {
            root   html;
            index  index.html index.htm;
        }


systemctl restart nginx.service

使用浏览器访问 http://www.gcc.com/100-100-100.html 或 http://www.gcc.com/100-200-100.html会自动跳转到 http://www.gcc.com页面

NGINX redirect 没有端口 nginx redirect配置_运维_06

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

要求访问 http://www.gcc.com/upload/123.php 跳转到首页

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

        #charset koi8-r;

        access_log  /var/log/nginx/www.gcc.com-access.log;

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

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


systemctl restart nginx.service

浏览器访问 http://www.gcc.com/upload/123.php 跳转到 http://www.gcc.com 首页

NGINX redirect 没有端口 nginx redirect配置_nginx_07

6、基于最普通一条 url 请求的跳转

要求访问一个具体的页面,如: http://www.gcc.com/abc/123.html,跳转到首页

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

        #charset koi8-r;

        access_log  /var/log/nginx/www.gcc.com-access.log;

        location ~* /abc/123.html {
           rewrite (.+) http://www.gcc.com permanent;
        }

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


systemctl restart nginx.service

浏览器访问 http://www.gcc.com/abc/123.html 跳转到 http://www.gcc.com

NGINX redirect 没有端口 nginx redirect配置_正则表达式_08