php 阻止ip代码,php – 如何阻止100,000个独立的IP地址
你可以嘗試的一些事情是保留你想要在
text file中阻止的IP地址的列表或將其轉換為
dbm hash file,然后使用mod_rewrite的RewriteMap。你必須在你的服務器/ vhost配置中設置。您不能在htaccess文件中初始化映射。
RewriteEngine On
RewriteMap deny_ips txt:/path/to/deny_ips.txt
RewriteCond ${deny_ips:%{REMOTE_ADDR}|0} !=0
RewriteRule ^ - [L,F]
/path/to/deny_ips.txt文件看起來像這樣:
12.34.56.78 1
11.22.33.44 1
etc.
基本上,你想要拒絕的IP和空格,然后“1”。此文本文件中的任何IP將導致服務器返回403 Forbidden。為了加快速度,你可以使用httxt2dbm生成一個dbm哈希,然后定義映射如下:
RewriteMap deny_ips dbm:/path/to/deny_ips.dbm
我不知道什么性能命中是使用mod_rewrite像這樣與許多IPs,但在linache 2.2運行在3Ghz i686上的快速基準測試,列表中的5個IP與102418之間的差異是可以忽略不計。根據ab的輸出,他們幾乎相同。
解決具體問題:
Is it possible for htaccess to get the list from database (Redis,Crunchbase,Mongo, MySQL or even Sqlite) … any
使用重寫映射,可以使用“prg”映射類型為映射類型運行外部程序。然后,您可以編寫perl,php等腳本與數據庫通話,以查找IP地址。另請注意,“注意”下列出的警告。然后,您將像任何其他地圖一樣使用此地圖(RewriteCond $ {deny_ips:%{REMOTE_ADDR} | 0}!= 0)。這本質上會為所有請求創建瓶頸。不是與數據庫通信的最佳解決方案。
在apache 2.4中,有一個dbd/fastdbd地圖類型,它允許你通過mod_dbd創建查詢。這是一個更好的選擇,mod_dbd模塊管理與數據庫,池連接等的連接。所以地圖定義看起來像:
RewriteMap deny_ips "fastdbd:SELECT active FROM deny_ips WHERE source = %s"
假設你有一個表“deny_ips”,有2列“源”(IP地址)和“活動”(1表示活動,0表示不活動)。
Is there a visible solution to manage such kind of issue in production
如果要將所有阻止的IP存儲在數據庫中,則需要管理數據庫表的內容。如果您使用dbm映射類型,我至少知道perl has a DBI用于管理dbm文件,因此您可以使用它來從拒絕列表中添加/刪除IP條目。我從來沒有使用它,所以我不能真正地說很多。管理一個平面文本文件將是一個很麻煩,特別是如果你打算刪除條目,而不是只是追加到它。在使用數據庫和apache 2.4的mod_dbd之外,我不認為任何這些解決方案是開箱即用或生產準備。它需要自定義工作。
I know the best solution is Block the IPs at the firewall level is there any way to pragmatically add/remove IP to the firewall
對于IPtables,有一個perl interface標記為Beta,但我從來沒有使用它。有libiptc但根據netfilter’s faq:
Is there an C/C++ API for adding/removing rules?
The answer unfortunately is: No.
Now you might think ‘but what about libiptc?’. As has been pointed out numerous times on the mailinglist(s), libiptc was NEVER meant to be used as a public interface. We don’t guarantee a stable interface, and it is planned to remove it in the next incarnation of linux packet filtering. libiptc is way too low-layer to be used reasonably anyway.
We are well aware that there is a fundamental lack for such an API, and we are working on improving that situation. Until then, it is recommended to either use system() or open a pipe into stdin of iptables-restore. The latter will give you a way better performance.
所以我不知道libiptc解決方案是否可行,如果沒有API穩定性。
總結
以上是生活随笔為你收集整理的php 阻止ip代码,php – 如何阻止100,000个独立的IP地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中冠百年|到了30岁应该如何增加收入?
- 下一篇: php obclean函数,php函数o