nginx屏蔽無效請求方式

upstream tomcat {
02 ip_hash;
03 server 192.168.2.187:8080;
04 }
05  
06 location ~* /html {
07  
08 if ($request_method = PUT ) {
09  
10 return 403;
11  
12 }
13  
14 if ($request_method = DELETE ) {
15  
16 return 403;
17  
18 }
19  
20 if ($request_method = POST ) {
21  
22 return 403;
23  
24 }
25  
26 proxy_method GET;
27  
28 proxy_pass http://tomcat;
29  
30 }

當路徑包含/html的時候,則代理到server後端進行請求數據。這裏屏蔽了PUT,DELETE,POST方法,只是使用了GET,主要目的是爲了安全性,因爲DELETE,POST,PUT是可以修改數據的。

或者:

01 limit_except GET {
02 allow 192.168.1.1;
03 deny all;
04 }
05  
06  
07 if ($request_filename ~ /test/index.html) {
08 # return 404;
09 rewrite ^/(.*) /index.html;                                                                       
10 };

nginx禁止訪問txt|doc文件                              

方法一:全局設置,禁止訪問任何以後綴txt|doc的文件 
1 location ~* \.(txt|doc)$ {
2         deny all;
3   }
方法二:只禁止訪問某目錄下的txt|doc 
1 location ~* \.(txt|doc)$ {
2         if (-f $request_filename) {
3         root html/job;
4         break;
5          }
6    }
nginx禁止某中瀏覽器訪問:#瀏覽器類型可從日誌得知。 
1 server
2        {
3                listen       80;
4                server_name  test.domain.com;
5                index index.php index.html;
6                root   /opt/nginx/html/;
7                if $http_user_agent ~* "MSIE 6.0" ) {
8                return 403;
9                 }
設置目錄執行權限                                                                                 
在windows+iis下,可以設置上傳目錄,類似:upload,uploadfile,attachments,這樣的目錄下面無腳本執行權限,從而防止非法用戶上傳腳本得到webshell 

nginx上也很簡單,我們使用location 如下:

1 location ~ ^/upload/.*\.(php|php5)$
2 {
3 deny all;
4 }

其中upload換爲你要設置的目錄名字

這條規則的含義是匹配請求連接中開頭是/upload/,中間匹配任意字符,結尾匹配.php或者.php5的頁面,最後利用deny all禁止訪問,這樣就防止了上傳目錄的腳本執行權限


## Only allow these request methods (GET|HEAD|POST)

 if ($request_method !~ ^(GET|HEAD|POST)$ ) { 

   return 444; }


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