Nginx rewrite配置

  • Rewrite简介
  • Rewrite跳转场景
  • Rewrite实际场景
  • Rewrite命令
  • Nginx正则表达式
  • last和break的区别
  • location分类
  • location优先级
  • Location优先级的示例
  • rewrite和location区别
  • location优先级规则
  • 应用实例
  • 基于域名的跳转
  • 基于客户端IP访问跳转
  • 基于旧、新域名跳转并加目录
  • 基于参数匹配的跳转
  • 基于目录下所有php文件跳转
  • 基于最普通url请求的跳转


Rewrite简介

Rewrite跳转场景

1、URL看起来更规范、合理
2、企业会将动态URL地址伪装成静态地址提供服务
3、网址换新域名后,让旧的访问跳转到新的域名上
4、服务端某些业务调整

Rewrite实际场景

Nginx跳转需求的实现方式

使用rewrit进行匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转

rewrite放在server{},if{},location{}段中
location只对域名后边的除去传递参数外的字符串起作用

对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理

Rewrite命令

rewrite       <regex>     <replacement>       [flag];
 <regex>:正则表达式        
 <replacement>:跳转后的内容     
 [flag]rewrite支持的flag标记

flag标记说明

标记

说明

last

相当于Apache 的[L]标记,表示完成rewrite

break

本条规则匹配完成即终止,不再匹配后面的任何规则break

redirect

返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url

permanent

返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新 url

Nginx正则表达式

常用的正则表达式元字符

字符

说明

^:

匹配输入字符串的起始位置

$:

匹配输入字符串的结束位置

*****:

匹配前面的字符零次或多次

+:

匹配前面的字符一次或多次

?:

匹配前面的字符零次或一次

.:

匹配除\n之外的任何单个字符 使用[.\n]可以匹配包括\n在内的任意字符

****:

转义符

\d:

匹配纯数字

{n}

重复n次

{n,}:

重复n次或更多次

[c]:

匹配单个字符c

[a-z]:

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

[a-zA-Z]:

匹配a-z小写字母或A-Z大写字母的任意一个

last和break的区别

last:url重写后,马上发起一个新请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变。
break:url重写后,直接使用当前资源,不再使用
location余下的语句,完成本次请求,地址栏不变。

总结:last和break再重定向后,地址栏都不会发生变化,这是他们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配

last

break

使用场景

一般写在server和if中

一般使用在location中

URL匹配

不终止重写后的 url 匹配

终止重写后的 url 匹配

location分类

location = patt{} [精准匹配]
location patt {} [一般匹配]
location ~ patt {} [正则匹配]

正则匹配的常用表达式

nginx的rewrite配置 nginx rewrite_linux

location优先级

相同类型的表达式,字符串长的会优先匹配
按优先级排列

=类型
^~类型表达式
正则表达式(*)类型
·常规字符串匹配类型,按前缀匹配
通用匹配(),如果没有其它匹配,任何请求都会匹配到

Location优先级的示例

location = / {	'//精确匹配 /,主机名后面不能带任何字符串'
    [configuraion A ]	
}

location / {	'//所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配'
    [configuraion B ]
}

location /documents/ {		'//匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用'
    [configuraion C ]
}

location ~ /documents/abc {		'//匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用'
    [configuraion D ]
}

location ^~ /images/ {	'//以/images/开头的地址,匹配符合后,停止往下匹配'
    [configuraion E ]
}

location ~*\.(gif|jpg|gpeg)$ {	'//匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高'
    [configuraion F ]
}

location /images/abc {	'//最长字符匹配到 /images/abc,优先级最低'
    [configuraion G ]
}

location ~ /images/abc {	'//以/ Images/abc开头的,优先级次之'
    [configuraion H ]
}

location /images/abc/1.html {	'//如果和正则 ~ images/abc/1.htm相比,正则优先级更高'
    [configuraion I ]
}

rewrite和location区别

相同点
都能实现跳转
不同点
rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
rewrite会写在location里,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令

location优先级规则

匹配某个具体文件:

(location = 完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)=(location ~
完整路径)>(location /)

用目录做匹配访问某个文件:

(location = 目录)>(location ^~ 目录)>(location ~* 目录)=(location ~
目录)>(location /)

应用实例

基于域名的跳转

公司旧域名www.domain.com,因业务需求有变更,需要
使用新域名www.newdomain.com代替
不能废除|旧域名
从旧域名跳转到新域名,且保持其参数不变
修改默认站点配置文件

vi /usr/local/nginx/conf/nginx.conf
       access_log  logs/host.access.log  main;
            if ($host = 'www.domain.com')
            {
                 rewrite ^/(.*)$ http://www.newdomain.com/$1  permanent;
            }
             location / {

修改主页内容

[root@localhost ~]# mkdir -p /usr/share/nginx/html
[root@localhost ~]# vi /usr/share/nginx/html/index.html
<html><body><h1>this is new</h1></body></html>

重启nginx

[root@server1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 ~]# systemctl restart nginx

修改主页内容
客户机域名映射

[root@server1 ~]# vi /etc/hosts
20.0.0.10      www.domain.com  www.newdomain.com

验证:输入www.domain.com的域名发现跳转到www.newdomain的网站

nginx的rewrite配置 nginx rewrite_html_02

基于客户端IP访问跳转

要求:
公司内部可以用来访问服务器的地址为192.168.200.40,其他IP的客户端访问时跳转到error网页
主配置修改

[root@server1 ~]# vi /usr/local/nginx/conf/nginx.conf
      server {
             listen       80;
             server_name  www.domain.com;
      
             charset utf-8;
      
             access_log  logs/host.access.log  main;
            set $rewrite true;  //#允许公司内部访问,更改标志位false(这样不会执行下面if判断的rewrite了)
             if ($remote_addr = "192.168.200.40") {
                 set $rewrite false;
                }
                 if ($rewrite = true) {    //#如果不是公司IP,加上后缀地址作为标识
                     rewrite (.+) /error.html;
                   }
              location = /error.html {  //#精确匹配有/error.html后缀的网页
                  root   /usr/share/nginx/html;
              }

修改重定向的主页

[root@localhost ~]# vi /usr/share/nginx/html/error.html
<html><body><h1>ERROR!</h1></body></html>

客户机添加映射

[root@server1 ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.40   www.domain.com www.newdomain.com

查看配置文件是否报错,且重启服务

[root@server1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 ~]# systemctl nginx restart

其他IP客户端访问效果图

nginx的rewrite配置 nginx rewrite_nginx_03

基于旧、新域名跳转并加目录

要求:
将域名http://www.aaa.com下面的发帖都跳转到http://www.old.com/bbs,且域名跳转后保持参数不变
修改主配置文件

[root@server1 ~]# vi /usr/local/nginx/conf/nginx.conf
      server {
              listen       80;
              server_name  www.aaa.com;
      
              charset utf-8;
      
             access_log  logs/host.access.log  main;
      
             location  /post {
                  rewrite (.+) http://www.old.com/bbs$1 permanent;
             }
              location / {
                 root   /usr/share/nginx/html;
                  index  index.html index.htm;
             }

更改主页

[root@server1 ~]# mkdir -p /usr/share/nginx/html/bbs/post
[root@server1 ~]# vi /usr/share/nginx/html/bbs/post/1.html
<html><body><h1>this is bbs</h1></body></html>

重启服务,并检查配置文件是否报错

[root@server1 ~]# systemctl restart nginx

验证

客户端浏览器访问www.aaa.com/post/1.html

nginx的rewrite配置 nginx rewrite_html_04

基于参数匹配的跳转

要求:
现在访问http://www.test.com/100-(100|200)-100.html
跳转到http://www.test.com页面。

修改配置文件

[root@server1 ~]# vi /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
            rewrite (.*) http://www.test.com permanent;
        }
        location  / {
            root   html;
            index  index.html index.htm;
        }

重启服务,检查配置文件是否报错

[root@server1 ~]# systemctl restart nginx
nginx -t

验证

访问www.test.com/100-100-10.html

nginx的rewrite配置 nginx rewrite_nginx的rewrite配置_05

基于目录下所有php文件跳转

要求:
访问http://www.test.com/upload/1.php跳转到首页
修改配置文件

[root@server ~]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~* /upload/.*\.php$ {
            rewrite (.+) http://www.test.com permanent;
        }
        location  / {
            root   html;
            index  index.html index.htm;
        }

检查语法,并重启服务

[root@server ~]# systemctl restart nginx
[root@server ~]# nginx -t

验证

客户端访问http://www.test.com/upload/1.php

nginx的rewrite配置 nginx rewrite_nginx_06

基于最普通url请求的跳转

要求:
访问一个具体的页面跳转到首页,如浏览器访问http://www.test.com/1/test.html跳转到首页。
修改配置文件

[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~* ^/1/test.html {
          rewrite (.+) http://www.test.com permanent;
        }
        location  / {
            root   html;
            index  index.html index.htm;
        }

检查语法,并重启服务

[root@server ~]# systemctl restart nginx
[root@server ~]# nginx -t

验证

浏览器访问http://www.test.com/1/test.html

nginx的rewrite配置 nginx rewrite_html_07