zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Ansible facts
    K8S基础概念
    Ansible CMDB
    Docker 核心技术与实现原理
    如何在 Centos7 中使用阿里云的yum源
    一篇文全面了解DevOps:从概念、关键问题、兴起到实现需求
    关于操作stream流
    service not available now, maybe disk full, CL: 0.95 CQ: 0.95 INDEX: 0.95, maybe your broker mach
    AOP的底层实现,动态代理和cglib代理
    模拟浏览器加载静态资源
  • 原文地址:https://www.cnblogs.com/yyxianren/p/10815107.html
Copyright © 2011-2022 走看看