1.17-1.18 rewrite实战

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/example.md 


域名重定向

场景1

不带条件的场景

效果:访问2.com 跳转到1.com

vim 2.com.conf 
server {
        listen 80;
        server_name 2.com;
        root /data/t-nginx/2.com;
        rewrite /(.*) http://1.com/$1 permanent;
}
# curl -x127.0.0.1:80 2.com/1.php -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 24 Oct 2018 07:14:10 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://1.com/1.php


带条件的场景

场景2

效果:当访问不是2.com的时候,跳转到www.2.com

vim 2.com.conf 
server {
        listen 80;
        server_name 2.com www.2.com;
        root /data/t-nginx/2.com;
        if ( $host != 'www.2.com' )
        {
        rewrite /(.*) http://www.2.com/$1 permanent;
        }
}
# curl -x127.0.0.1:80 2.com/1.php -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 24 Oct 2018 07:27:42 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.2.com/1.php

示例3(http跳转到https):

server {
        listen 80;
        server_name 2.com www.2.com;
        root /data/t-nginx/2.com;
        rewrite /(.*) https://2.com/$1 permanent;
}
# !curl
curl -x127.0.0.1:80 2.com/1.php -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 24 Oct 2018 07:58:59 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://2.com/1.php

示例4(域名访问二级目录)

示例5(静态请求分离)

效果:当访问图片文件,流向媒体文件的时候,跳转到特定的域名

server {
        listen 80;
        server_name 2.com www.2.com;
        root /data/t-nginx/2.com;
        if ( $uri ~* 'jpg|png|gif|js$' )
        {
        rewrite /(.*) https://img.2.com/$1 permanent;
        }
}
# curl -x127.0.0.1:80 2.com/1.png -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 24 Oct 2018 08:28:18 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://img.2.com/1.png

6 防盗链

2.com.conf配置:

server {
        listen 80;
        server_name  www.2.com;
root /data/t-nginx/2.com;
        location ~* ^.+.(jpg|png|gif|js|mp3|mp4)$
        {
        valid_referers none blocked server_names *.2.com 2.com;
        if ($invalid_referer)
                {
                        return 403;
                }
        }
}

curl 测试

# curl -x127.0.0.1:80 -e "http://linux.com" 'www.2.com/1.gif' -I
HTTP/1.1 403 Forbidden
Server: nginx/1.8.0
Date: Wed, 24 Oct 2018 09:10:37 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
当用refers-->http://linux.com访问www.2.com/1.gif的时候,linux.com不属于白名单,所以会反馈return 403代码。

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;
    }
}

解释:

  • set $rule 0 变量$rule的值为0,第一个条件

  • set $rule "${rule}1" 变量$rule的值为1,第二个条件

  • set $rule "${rule}2" 变量$rule的值为2,第三个条

  • if ($rule = "012") 当$rule满足0 1 2,也就是满足第一,二,三个条件的时候,会执行xxx。。。