nginx中rewrite转发记录

nginx转发的几个典型应用:

1.访问*.abc.com会重定向到www.def.com/*
        if ($http_host ~ "(.*)\.abc\.com$")
        {
              set $sub_name $1;
              rewrite ^(.*)   http://www.def.com/$sub_name permanent;
         }

2.访问m.abc.com/area/***会隐式转发到m.abc.com/admin/union/index.do?area=***
         if ($http_host ~* "m\.abc\.com$")
         {
               rewrite ^/area/(.*)   /admin/union/index.do?area=$1 last;
          }

3.禁止访问以.sh   .flv   .mp4为文件名后缀的URL地址:
          location ~ .*\.(sh|flv|mp4)?$
          {
               return 403;
          }

4.当文件或者目录不存在时,重定向到某个php文件上:
          if (! -e $request_filename)
          {
               rewrite ^/(.*)$ /index.php last;
          }

5.如果客户端使用IE浏览器,则重定向到/nginx-ie目录下面:
          if ($http_user_agent ~ MSIE)
          {
               rewrite ^(.*)$ /nginx-ie/$1 break;
          }

6.禁止访问以data开头的文件:
           location ~ ^/data
           {
                deny all;
           }
 

7.将多级目录下的文件转换成一个文件/job-123-456-789.html 指向/job/123/456/789.html:
           rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$  /job/$1/$2/jobshow_$3.html

8.目录兑换/123456/xxxx 换位为/xxxx?id=123456:
           rewrite ^/(\d+)/(.+)/   /$2?id=$1  last;

你可能感兴趣的:(rewrite,nginx转发)