日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

nginx中封禁ip和允許內網ip訪問的實現示例_nginx

作者:autofelix ? 更新時間: 2022-05-19 編程語言

Nginx不僅僅只是一款反向代理和負載均衡服務器,它還能提供很多強大的功能,例如:限流、緩存、黑白名單和灰度發布等等,我們先來了解一下nginx如何封禁ip和允許內網ip訪問。

一、語法

Nginx的ngx_http_access_module 模塊可以封配置內的ip或者ip段

deny IP; 
deny subnet; 
allow IP; 
allow subnet; 
# block all ips 
deny    all; 
# allow all ips 
allow    all;

二、封禁ip

假定nginx的目錄在/usr/local/nginx/
首先要建一個封ip的配置文件blockips.conf,然后在文件中輸入要封的ip?

deny 192.168.4.3;?
deny 31.42.145.0/24;?
deny 51.12.35.0/24;

然后保存此文件

并且打開nginx.conf文件,在http配置節內添加下面一行配置:

http {
    # 其他配置
 
    include blockips.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訪問

配置如下

下面配置中禁止了192.168.1.1,允許其他內網網段,然后deny all禁止其他所有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; 
}

原文鏈接:https://blog.csdn.net/weixin_41635750/article/details/123537951

欄目分類
最近更新