Nginx重定向[Rewrite]配置及示例

PS:Nginx重定向[Rewrite]配置及示例

  1. Rewrite的Flags

    last – 完成重写指令后,搜索相应的URI和位置。相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则。
    break – 中止Rewirte,不在继续匹配。
    redirect – 返回临时重定向的HTTP状态302。
    permanent – 返回永久重定向的HTTP状态301。

  2. 文件及目录匹配

    -f和!-f用来判断是否存在文件
    -d和!-d用来判断是否存在目录
    -e和!-e用来判断是否存在文件或目录
    -x和!-x用来判断文件是否可执行

  3. 正则表达式全部符号解释

    ~ 为区分大小写匹配
    ~* 为不区分大小写匹配
    !~和!~* 分别为区分大小写不匹配及不区分大小写不匹配
    (pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘/(’ 或 ‘/)’。
    ^ 匹配输入字符串的开始位置
    $ 匹配输入字符串的结束位置

  4. ZEND Framework的重定向规则

    案例一:全部重定向到 /index.php

            rewrite ^/(.*)  /index.php?$1&;
            

    案例二:如果文件或目录不存在则重定向到index.php

            if (!-e $request_filename) {
                rewrite ^/(.*)  /index.php?$1&;
            }
            
  5. WordPress的重定向规则

    案例一:http://www.**.com/12 重定向到 http://www.**.com/index.php?p=12

            if (!-e $request_filename) {
                rewrite ^/(.+)$  /index.php?p=$1 last;
            }
            

    案例二:与zendframework配置很像

            if (!-e $request_filename) {
                rewrite ^/(.*)  /index.php?$1&;
            }
            
  6. 以下为Discuz完整的Rewrite for Nginx规则

            if (!-f $request_filename) {
                rewrite ^/archiver/((fid|tid)-[/w/-]+/.html)$  /archiver/index.php?$1 last;
                rewrite ^/forum-([0-9]+)-([0-9]+)/.html$  /forumdisplay.php?fid=$1&page=$2 last;
                rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)/.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
                rewrite ^/space-(username|uid)-(.+)/.html$  /space.php?$1=$2 last;
                rewrite ^/tag-(.+)/.html$  /tag.php?name=$1 last;
            }
            
  7. 重定向规则案例

    案例一:多目录转成参数
    abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

            if ($host ~* (.*)\.domain\.com) {
                set $sub_name $1;
                rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
            }
            

    案例:目录对换
    /123456/xxxx -> /xxxx?id=123456

            rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
            

    案例:设定nginx在用户使用ie的使用重定向到/nginx-ie目录下

            if ($http_user_agent ~ MSIE) {
                rewrite ^(.*)$  /nginx-ie/$1 break;
            }
            

    案例:目录自动加”/”

            if (-d $request_filename){
                rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
            }
            

    案例:禁止htaccess

            location ~/\.ht {
                deny all;
            }
            

    案例:禁止多个目录

            location ~ ^/(cron|templates)/ {
                deny all;
                break;
            }
            

    案例:禁止以/data开头的文件
    可以禁止/data/下多级目录下.log.txt等请求;

            location ~ ^/data {
                deny all;
            }
            

    案例:禁止单个目录
    不能禁止.log.txt能请求

            location /searchword/cron/ {
                deny all;
            }
            

    案例:禁止单个文件

            location ~ /data/sql/data.sql {
                deny all;
            }
            

    案例:给favicon.ico和robots.txt设置过期时间;
    这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

            location ~(favicon.ico) {
                log_not_found off;
                expires 99d;
                break;
            }
            location ~(robots.txt) {
                log_not_found off;
                expires 7d;
                break;
            }
            

    案例:设定某个文件的过期时间;这里为600秒,并不记录访问日志

            location ^~ /html/scripts/loadhead_1.js {
                access_log   off;
                root /opt/lampp/htdocs/web;
                expires 600;
                break;
            }
            

    案例:文件反盗链并设置过期时间
    这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
    “rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片
    “access_log off;”不记录访问日志,减轻压力
    “expires 3d”所有文件3天的浏览器缓存

            location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
                valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
                if ($invalid_referer) {
                    rewrite ^/ http://leech.c1gstudio.com/leech.gif;
                    return 412;
                    break;
                }
                access_log   off;
                root /opt/lampp/htdocs/web;
                expires 3d;
                break;
            }
            

    案例:只充许固定ip访问网站,并加上密码

            root  /opt/htdocs/www;
            allow   208.97.167.194;
            allow   222.33.1.2;
            allow   231.152.49.4;
            deny    all;
            auth_basic "C1G_ADMIN";
            auth_basic_user_file htpasswd;
            

    案例:将多级目录下的文件转成一个文件,增强seo效果
    /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 last;
            

    案例:将根目录下某个文件夹指向2级目录
    如/shanghaijob/ 指向 /area/shanghai/
    如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

                rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
            #上面例子有个问题是访问/shanghai 时将不会匹配
            

    案例:域名跳转

                server{
                    listen       80;
                    server_name  jump.c1gstudio.com;
                    index index.html index.htm index.php;
                    root  /opt/lampp/htdocs/www;
                    rewrite ^/ http://www.c1gstudio.com/;
                    access_log  off;
                }
            

    案例:多域名转向

            server_name  www.c1gstudio.com www.c1gstudio.net;
                         index index.html index.htm index.php;
                         root  /opt/lampp/htdocs;
            if ($host ~ "c1gstudio\.net") {
            rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
            }
            

    案例:三级域名跳转

            if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
                rewrite ^(.*) http://top.c1gstudio.com$1;
                break;
            }
            

    案例:域名镜向

            server{
                listen       80;
                server_name  mirror.c1gstudio.com;
                index index.html index.htm index.php;
                root  /opt/lampp/htdocs/www;
                rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
                access_log  off;
            }
            

总结:


参考:
1.nginx官网Rewrite

相关文章
  1. 对Web静态资源缓存自动更新的思考与实践的总结
  2. gearman应用-分布式图库系统设计
  3. HTTP/1.1 405 Method not allowed
  4. Nginx使用的php-fpm的两种进程管理方式及优化
  5. Nginx配置参数中文说明
  6. Nginx(PHP/fastcgi)的PATH_INFO配置
本站版权
1、本站所有主题由该文章作者发表,该文章作者与尘埃享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和尘埃的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:
二维码
Posted in Nginx, web服务器

发表评论