• nginx rewrite实战实例


      本部分内容为nginx生产环境中使用的场景示例。

    域名跳转(域名重定向)

    示例1(不带条件的):
    server{
        listen 80;
        server_name www.aminglinux.com;
        rewrite /(.*) http://www.aming.com/$1 permanent;
        .......
        
    }
    示例2(带条件的):
    server{
        listen 80;
        server_name www.aminglinux.com aminglinux.com;
        if ($host != 'www.aminglinux.com')
        {
            rewrite /(.*) http://www.aminglinux.com/$1 permanent;
        }
        .......   
    }
    示例3(http跳转到https):
    server{
        listen 80;
        server_name www.aminglinux.com;
        rewrite /(.*) https://www.aminglinux.com/$1 permanent;
        .......    
    }
    示例4(域名访问二级目录)
    server{
        listen 80;
        server_name bbs.aminglinux.com;
        rewrite /(.*) http://www.aminglinux.com/bbs/$1 last;
        .......
    }
    示例5(静态请求分离)
    server{
        listen 80;
        server_name www.aminglinux.com;
        location ~* ^.+.(jpg|jpeg|gif|css|png|js)$
        {
            rewrite /(.*) http://img.aminglinux.com/$1 permanent;
        }
        .......  
    }
    或者:
    server{ listen
    80; server_name www.aminglinux.com; if ( $uri ~* 'jpg|jpeg|gif|css|png|js$') { rewrite /(.*) http://img.aminglinux.com/$1 permanent; } ....... }

    防盗链:

    示例6
    server{
        listen 80;
        server_name www.aminglinux.com;
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
        {
            valid_referers none blocked server_names *.aminglinux.com aminglinux.com *.aming.com aming.com;
            if ($invalid_referer)
            {
                rewrite /(.*) http://img.aminglinux.com/images/forbidden.png;
            }
        }
        .......
    }
    说明:
    *这里是通配,跟正则里面的*不是一个意思,none指的是referer不存在的情况(curl -e 测试), blocked指的是referer头部的值被防火墙或者代理服务器删除或者伪装的情况, 该情况下,referer头部的值不以http://或者https://开头(curl -e 后面跟的referer不以http://或者https://开头)。 或者:
    location
    ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.aminglinux.com *.aming.com aminglinux.com aming.com; if ($invalid_referer) { return 403; } }

    伪静态:

    示例7(discuz伪静态):
    location /  {
        rewrite ^([^.]*)/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;
        rewrite ^([^.]*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
        rewrite ^([^.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^([^.]*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
        rewrite ^([^.]*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;
        rewrite ^([^.]*)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3 last;
    }

    rewrite多个条件的并且:

    示例8:
    location /{
        set $rule 0;
        if ($document_uri !~ '^/abc')
        {
            set $rule "${rule}1";
        }
        if ($http_user_agent ~* 'ie6|firefox')
        {
           set $rule "${rule}2";
        }
        if ($rule = "012")
        {
            rewrite /(.*) /abc/$1 redirect;
        }
    }
  • 相关阅读:
    iOS 字符串的UTF8 编码 以及归档反归档
    iOS 关于关键字高亮
    iOS 适配 ,关于prefix Header 文件的配置
    关于collectionView 的头视图
    关于页面的下拉刷新,和上拉加载 --- > collectionView ,tableView
    Nums数独计算器的感谢版,有3种解题方式,过去注册的也是可以用
    Nums数独计算器,良心软件,给不给钱都是可以的
    人生不过是给自己找一些痕迹,证明自己存在过
    做些有益的事情,人生要懂得自重
    振作才是自己可以走的路,人生不要放弃自己的争取
  • 原文地址:https://www.cnblogs.com/yyxianren/p/10815107.html
Copyright © 2020-2023  润新知