nginx 封ip或ip段

首先要建一個封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配置節(jié)內(nèi)添加下面一行配置:

include blockips.conf;

保存nginx.conf文件,然后測試現(xiàn)在的nginx配置文件是否是合法的:

/usr/sbin/nginx -t

如果配置沒有問題

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

如何禁止所有外網(wǎng)ip,僅允許內(nèi)網(wǎng)ip呢?

如下配置文件

location / {? # block one workstation? deny? ? 192.168.1.1;? # allow anyone in 192.168.1.0/24? allow? 192.168.1.0/24;? # drop rest of the world? deny? ? all;}

上面配置中禁止了192.168.1.1,允許其他內(nèi)網(wǎng)網(wǎng)段,然后deny all禁止其他所有ip。

如何格式化nginx的403頁面呢?

首先執(zhí)行下面的命令:

cd /usr/local/nginx/html

vi error403.html

然后輸入403的文件內(nèi)容,例如:

Error 403 - IP Address BlockedYour IP Address is blocked. If you this an error, please contact webmaster with your IP at webmaster@example.com

如果啟用了SSI,可以在403中顯示被封的客戶端ip,如下:

Your IP Address is blocked.

保存error403文件,然后打開nginx的配置文件vi nginx.conf,在server配置節(jié)內(nèi)添加下面內(nèi)容

# redirect server error pages to the static page error_page? 403? /error403.html; location = /error403.html {? ? ? ? root? html; }

然后保存配置文件,通過nginx -t命令測試配置文件是否正確,若正確通過nginx -s reload載入配置。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容