歡迎您光臨本站 註冊首頁

Nginx限制IP訪問某些頁面的操作

←手機掃碼閱讀     hongdian2012 @ 2020-05-14 , reply:0

1、要禁止所有IP訪問a1.htm a2.htm a3.htm這個三個頁面在location可以這樣寫

 location ~* /(a1.htm|a2.htm|a3.htm)$ { deny all; condition………; } 

2、只允許指定的ip訪問a1.htm a2.htm a3.htm這個三個頁面,其他IP的訪問都拒絕 location ~* /(a1.htm|a2.htm|a3.htm)

$ { allow 10.0.0.2; deny all; condition………; } 

這種設置只有ip地址為10.0.0.2的主機可以放問這三個頁面,其他的ip都被拒絕了。 其他情況可以以此類推。 比如我需要指定只能8.8.8.8這個ip訪問info.php頁面。那麼就可以在nginx-server中添加如下配置,即可 如果非8.8.8.8訪問info.php頁面,則返回403 後面需要加上跳轉地址,proxy_pass http://192.168.1.110:10480;否則會404錯誤。 location ~/info.php$ { if ($remote_addr != '8.8.8.8' ) { return 403; } proxy_pass http://192.168.1.110:10480; } } 也可以在server代碼中添加 location ~/info.php$ { allow 8.8.8.8; deny all; condition………; } 

一樣的效果 如何配置禁用ip或ip段呢? 

下面說明假定nginx的目錄在/usr/local/nginx/ 首先要建一個封ip的配置文件blockips.conf,然後vi blockips.conf編輯此文件,在文件中輸入要封的ip。 deny 1.2.3.4; deny 91.212.45.0/24; deny 91.212.65.0/24; 然後保存此文件,並且打開nginx.conf文件,在http配置節內添加下面一行配置: include blockips.conf; 保存nginx.conf文件,然後測試現在的nginx配置文件是否是合法的: /usr/local/nginx/sbin/nginx -t 如果配置沒有問題,就會輸出: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful 如果配置有問題就需要檢查下哪兒有語法問題,如果沒有問題,需要執行下面命令,讓nginx重新載入配置文件。 /usr/local/nginx/sbin/nginx -s reload 只允許某些的ip訪問頁面,或者禁止某些ip訪問頁面 

server_name es.mila66.com;

 location / { include /etx/nginx/all/ip.conf; deny all;

 ip.conf裡面的文件格式:

 allow 192.168.1.11; allow 192.168.1.12; 

這樣就實現了只允許某些ip訪問頁面。 如果禁止某些IP訪問,只需要修改如下:把allow改成deny即可。 server_name es.mila66.com; 

location / { include /etx/nginx/all/ip.conf; allow all; ip.conf裡面的文件格式:

 deny 192.168.1.11; deny 192.168.1.12; nginx -s reload 重啟服務器即可


[hongdian2012 ] Nginx限制IP訪問某些頁面的操作已經有687次圍觀

http://coctec.com/docs/nginx/show-post-234518.html