
创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为临汾企业提供专业的网站设计、成都网站制作,临汾网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
rewrite进行匹配跳转if匹配全局变量后跳转location匹配再跳转if全局变量匹配proxy_ pass反向代理| 字符 | 说明 | 
|---|---|
| ^ | 匹配输入字符串的起始位置 | 
| $ | 匹配输入字符串的结束位置 | 
| * | 匹配前面的字符零次或多次 | 
| + | 匹配前面的字符一次或多次 | 
| ? | 匹配前面的字符零次或一次 | 
| . | 匹配除“\n”之外的任何单个字符。使用诸如"[.\n]"之 类的模式,可匹配包括“n”在内的任意字符 | 
| \ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用 | 
| \d | 匹配纯数字 | 
| {n} | 重复n次 | 
| {n,} | 重复n次或更多次 | 
| [c] | 匹配单个字符c | 
| [a-z] | 匹配a-z小写字母的任意一个 | 
| [a-zA-Z] | 匹配a-z小写字母或A-Z大写字母的任意一 个 | 
rewrite          [flag];
  //正则        //跳转后的内容    [flag]   //rewrite支持flag标记    | 标记 | 说明 | 
|---|---|
| last | 相当于 Apache的[L]标记,表示完成rewrite | 
| break | 本条规则匹配完成即终止,不再匹配后面的任何规则 | 
| redirect | 返回 302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url | 
| permanent | 返回 301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url | 
| last | break | |
|---|---|---|
| 使用场景 | 一般写在 server和if中 | 一般使用在 location中 | 
| URL匹配 | 不终止重写后的 url匹配 | 终止重写后的 url匹配 | 
location = patt {}       [精准匹配]
location patt {}         [一般匹配]
location ~ patt {}       [正则匹配]| 标记 | 说明 | 
|---|---|
| ~ | 执行一个正则匹配,区分大小写 | 
| ~* | 执行一个正则匹配,不区分大小写 | 
| !~ | 执行一个正则匹配,区分大小写不匹配 | 
| !~* | 执行一个正则匹配,不区分大小写不匹配 | 
| ^~ | 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他 location | 
| = | 普通字符精确匹配。也就是完全匹配 | 
| @ | 定义一个命名的 location,使用在内部定向时 | 
=类型^~类型表达式~和~*)类型rewrite是在同一域名内更改获取资源的路径location是对一-类路径做控制访问或反向代理,还可以proxy_pass到其他机器server块里面的rewrite指令location匹配location中的rewrite指令location =/ {
[ configuration A ]           //精确匹配/,主机名后面不能带任何字符串
}
location/ {
[ configuration B ]           //所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配
}
location /documents/ {
[ configuration C ]           //匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用
}
location ~ /documents/abc {
[ configuration D ]            //匹配任何以/documents/abc开头的地址当后面的正则表达式没有匹配到时,才会起作用
}
location ^~ /images/ {
[ configuration E ]             //以/images/开头的地址,匹配符合后,停止往下匹配
}
location ~* \.(gif|jipg|jpeg)$ {
[ configuration F ]             
}
//匹配所有以gif,jpg或jpeg结尾的请求,/images/下的图片会被[ configuration E ]处理,因为^~的优先级更高
location /images/abc {
[ configuration G ]               //最长字符匹配到/images/abc,优先级最低
}
location ~ /images/abc {
[ configuration H ]                //以/images/abc开头的,优先级次之
}
location /images/abc/1.html {
[ configuration I ]                 //如果和正则~ /images/abc/1.html相比,正则优先级更高
}location =完整路径) > (location ^~完整路径) > (location ~*完整路径) > (location ~完整路径) > (location 完整路径) > (location /)location= 目录) > (location ^~目录/) > (location ~ 目录) > (location ~*目录) > (location 目录) > (location /)[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm     //安装nginx官方源码包
获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.ymMjSa: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y     //安装nginx服务
已加载插件:fastestmirror, langpacks
已加载插件:fastestmirror, langpacks
base                                                                       | 3.6 kB  00:00:00     
extras                                                                     | 2.9 kB  00:00:00     
...//省略部分内容...
已安装:
  nginx.x86_64 1:1.16.1-1.el7.ngx                                                                   
完毕!
[root@localhost ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf                 //配置文件路径
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf     //编辑配置文件
server {
    listen       80;
    server_name  www.aacp.com;           //更改域名
    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;   //更改日志文件名称,并开启功能
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...//省略部分内容...
:wq
[root@localhost ~]# yum install bind -y          //安装DNS功能
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
...//省略部分内容...
已安装:
  bind.x86_64 32:9.11.4-9.P2.el7 
  ...//省略部分内容...
完毕!
[root@localhost ~]# vim /etc/named.conf
...//省略部分内容...
options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };
...//省略部分内容...
:wq
[root@localhost ~]# vim /etc/named.rfc1912.zones
...//省略部分内容...
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};
...//省略部分内容...
:wq
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133
:wq
[root@localhost named]# systemctl start named               //启动DNS服务
[root@localhost named]# systemctl start nginx               //启动nginx服务
[root@localhost named]# systemctl stop firewalld.service    //关闭防火墙
[root@localhost named]# setenforce 0                        //关闭增强性安全功能
[root@localhost named]# netstat -ntap | grep nginx       //查看nginx服务是否开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2769/nginx: master 

[root@localhost named]# vim /etc/nginx/conf.d/default.conf     //编辑Nginx配置文件
...//省略部分内容...
 #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location / {
        if ( $host = "www.accp.com" ){   
               rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;
           //在location模块中添加if判断语句,当输入"www.accp.com"访问网页时跳转到www.kgc.com中
         }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }   
    #error_page  404              /404.html;
...//省略部分内容...
:wq
[root@localhost named]# vim /etc/named.rfc1912.zones
...//省略部分内容...
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};      
...//省略部分内容...
:wq
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# systemctl restart nginx
[root@localhost named]# systemctl restart named





[root@localhost conf.d]#  vim default.conf        //编辑配置文件
...//省略部分内容...
#charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    set $rewrite true;              //设置是否合法的IP标记
    if ($remote_addr = "192.168.144.128"){    //判断是否为合法地址,如果为合法地址执行操作
            set $rewrite false;
    }
    if ($rewrite = true){         //判断是否为非法地址,如果为非法地址则打上标记,合法地址不做操作
            rewrite (.+) /main.html;
    }
    location = /main.html {         //匹配标记,执行跳转
            root /usr/share/nginx/html;
    }
    location / {     //注意此处删除上面设置的基于域名跳转的配置条目
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...//省略部分内容...
:wq
[root@localhost conf.d]# cd /usr/share/nginx/html/      //进nginx服务站点
[root@localhost html]# ls         //查看信息
50x.html  index.html
[root@localhost html]# vim main.html      //编辑跳转的网页内容
this is test web
:wq
[root@localhost html]# systemctl restart nginx    //重启网站

http://bbs.accp.com下面的发帖都跳转到http://www.accp.com/bbs,且域名跳转后保持参数不变[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  bbs.aacp.com;          //更改域名
    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location /post {               //设置匹配字段,字段匹配执行跳转操作
          rewrite (.+) http://www.accp.com/bbs$1 permanent;
    }
                             //注意删除上面设置的基于客户端IP访问的跳转的配置条目
    location / {     
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
...//省略部分内容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone        //编辑DNS服务区域数据文件
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
bbs IN  A       192.168.144.133          //更改主机头解析
:wq
[root@localhost html]# systemctl restart nginx      //重启nginx服务
[root@localhost html]# systemctl restart named       //重启DNS服务

http://www.accp.com/100-(100|200)-100.html跳转到http://www.accp.com页面[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.aacp.com;       //将服务器域名更改会www
    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$){    //删除此处上面设置的基于新、旧域名跳转并加目录跳转的配置条目
          rewrite (.*) http://www.accp.com permanent;     并设置基于参数访问时跳转回主网页
    }
    location / {     
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
...//省略部分内容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133      //更改主机头解析
:wq
[root@localhost html]# systemctl restart nginx        //重启nginx服务
[root@localhost html]# systemctl restart named        //重启DNS服务

http://www.accp.com/upload/1.php跳转到首页[root@localhost html]# vim /etc/nginx/conf.d/default.conf     //编辑配置文件
server {
    listen       80;
    server_name  www.aacp.com;
    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location ~* /upload/.*\.php$ {        //删除上面设置的匹配参数跳转访问,配置匹配所有php结尾访问跳转回主页
          rewrite (.+) http://www.accp.com permanent;
    } 
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
...//省略部分内容...
:wq
[root@localhost html]# systemctl restart nginx        //重启nginx服务

[root@localhost html]# vim /etc/nginx/conf.d/default.conf    //编辑配置文件
server {
    listen       80;
    server_name  www.aacp.com;
    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location ~* ^/abc/123.html {        //删除上面的配置,并重新编辑以具体的某个页面访问网页时跳转回主页
          rewrite (.+) http://www.accp.com permanent;
    }
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
...//省略部分内容...
:wq
[root@localhost html]# systemctl restart nginx        //重启nginx服务
