Nginx - 封鎖IP

# vi /usr/local/nginx/conf/nginx.conf
http {
include blockips.conf;
..........
以下省略# vi /usr/local/nginx/conf/blockips.conf
allow 192.168.11.2;                 //用法: deny|allow  {IP或整個網段}
deny 192.168.11.0/24;

# service nginx restart

只allow 192.168.11.2; 其餘都deny; 注意:rule是由上至下比對,只要符合就不會再往下比, 如果將剛例子,先192.168.11.0/24 然後是192.168.11.2的話,是被deny 的.
可以直接用include方式;也可以直接在 location 上作限制; 例如
location / {
root   html;
index  index.html index.htm;
deny 192.168.11.2;
allow all;        }

因被限制的client會被回應一個code 403;表示不能存取, 故我們最好再對403頁面客製化, 以免太多資料被知道!!!

# vi /usr/local/nginx/conf/nginx.conf
error_page 403 /error_403.html;
location = /error_403.html {
root    html;
allow   all;
}

# vi /usr/local/nginx/html/error_403.html
<html>
<head><title>Error 403 - IP Address Blocked</title></head>
<body>
Your IP Address is blocked. If you this an error, please contact webmaster with your IP at[email protected]
</body>
</html>
# service nginx restart

By tony

自由軟體愛好者~喜歡不斷的思考各種問題,有新的事物都會想去學習嘗試 做實驗並熱衷研究 沒有所謂頂天的技術 只有謙虛及不斷的學習 精進專業,本站主要以分享系統及網路相關知識、資源而建立。 Github http://stnet253.github.io

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料