1. 程式人生 > >12.13-12.16

12.13-12.16

nginx

12.13 Nginx防盜鏈

12.14 Nginx訪問控制

12.15 Nginx解析php相關配置

12.16 Nginx代理



12.13 Nginx防盜鏈


大綱

技術分享圖片

Nginx的防盜鏈思路和httpd是一樣的,配置也不難。

準備工作:

可以把其中不用的功能給註釋掉,

把配置靜態文件不記錄日誌的配置給註釋掉

插shell

#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

# {

# expires 7d;

# access_log off;

# }

#location ~ .*\.(js|css)$

# {

# expires 12h;

# access_log off;

# }


本節主題

1 Nginx防盜鏈 的配置如下,可以和上面的配置結合起來

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$

{

expires 7d;

valid_referers none blocked server_names *.test.com ;

if ($invalid_referer) {

return 403;

}

access_log off;

}


2 參數解析,

~*表示(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)裏面不區分大小寫,利用了正則表達式的語法。

^.+\. ^表示xx開頭

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$

整段參數的意思是:以xxxx(括號裏面的關鍵詞)結尾。

valid_referers none blocked server_names *.test.com ;

定義白名單的referer是*.test.com

if ($invalid_referer) {

return 403;

}

3 如果不是白名單的referer,就會返回到403


4 檢查語法,並重新加載

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload


5 curl 測試防盜鏈

先測試一個正常的curl訪問,狀態碼200

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/1.gif -I

技術分享圖片

curl -e指定referer測試,狀態碼403,實驗成功,因為當初配置就是要得到403效果。

[root@AliKvn test.com]# curl -e "http://www.baidu.com " -x127.0.0.1:80 test.com/1.gif -I

技術分享圖片


12.14 Nginx訪問控制

大綱

截圖

訪問控制是比較重要的一個部分,因為它還涉及到安全。

起原理跟httpd一樣,Nginx也可以限制某些IP不能訪問,或者只允許某些IP訪問。

配置方法和httpd很想,但更加簡潔,不像hettpd那樣全部遍歷一邊。

比如我們有個需求,“是訪問admin目錄的請求只允許127.0.0.1訪問,”


1 訪問控制的配置如下,

location /admin/

{

allow 127.0.0.1;

deny all;

}

參數解釋

在httpd中,利用order去規則allow與deny的順序,先allow後deny,或者先deny後allow.

但在nginx裏,如果參數的ip(127.0.0.1)被匹配到了,那麽這個規則就會到此結束。

不會考慮deny與allow的順序。如果是127.0.0.2訪問進來,那麽發現ip不被匹配,這樣的話,訪問會直接被deny,到此結束。


2 檢查語法並重新加載

[root@AliKvn test.com]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@AliKvn test.com]# /usr/local/nginx/sbin/nginx -s reload

訪問前的準備工作:

[root@AliKvn test.com]# mkdir -p /data/wwwroot/test.com/admin/

[root@AliKvn test.com]# cd /data/wwwroot/test.com/admin/

[root@AliKvn admin]# echo "admin test" > index.html



3 curl訪問測試

利用127.0.0.1測試,狀態碼200,正常通過訪問。

技術分享圖片


利用172.18.171.157測試訪問,狀態碼403,拒絕訪問。

因為172.18.171.157沒有被寫入白名單中。

技術分享圖片


訪問控制,還可以匹配正則

1 寫入如下參數

location ~ .*(upload|image)/.*\.php$

{

deny all;

}

參數解釋:

location ~ .*(upload|image)/.*\.php$

只要是匹配upload,image的,以.php結尾的url都給予deny.

寫入參數後檢查語法與重新加載。

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload


演示操作

2 準備工作,

建立upload目錄,然後在其下創建1.php文件,echo任意內容1.php

[root@AliKvn vhost]# mkdir /data/wwwroot/test.com/upload/

[root@AliKvn vhost]# cd !$

cd /data/wwwroot/test.com/upload/

[root@AliKvn upload]# echo "1111" > 1.php


3 curl測試

[root@AliKvn upload]# curl -x127.0.0.1:80 test.com/upload/1.php -I

技術分享圖片

當然,1.php不能訪問是正常的,起到了效果作用了。

下面嘗試一下訪問沒被標住的txt格式。

[root@AliKvn upload]# curl -x127.0.0.1:80 test.com/upload/1.txt -I

技術分享圖片

訪問通過,因為txt沒有被標住,所以沒有被限制訪問。

可以參看日誌內容,更詳細的記錄。

技術分享圖片


根據user_agent限制

1 寫入如下參數

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')

{

return 403;

}

參數解析,

$http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato'

return 403;

其中~是匹配符號,只要user_agent中含有Spider/3.0或者YoudaoBot或者Tomato字符串,

都會被拒絕訪問,並返回403狀態碼。

這裏的return 403跟deny all是一樣的效果。

2 檢查語法和重新加載

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload


3 curl 測試

curl -A 指定user_agent,

[root@AliKvn vhost]# curl -A "Tomato1111" -x127.0.0.1:80 test.com/upload/1.txt -I

技術分享圖片

當被比配到相應的字符串後,無論後面帶什麽字符都是生效了。

如果想匹配不區分大小寫,則在~加上*即可(~*),對應參數。

$http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato'

再利用curl -A 測試

技術分享圖片

12.13-12.16