1.17-1.18 rewrite實戰

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。。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章