nginxip過濾
Ⅰ 如何禁掉nginx惡意訪問ip
自己寫的網站,不知道為啥總是有很多惡意訪問的ip,根本不是正常的用戶訪問之前也有想過是否可能是運營商的代理伺服器出口ip,但是看起來好像也不像,先不管,ban掉再說
那如何來ban呢,規則比較簡單,就是超過一個閾值之後,就用下面的iptables命令
iptables -I INPUT -s <ip-to-ban> -j DROP
service iptables save
service iptables restart
那如何統計nginx的訪問ip呢,目前來說比較簡單就是讀取nginx的access.log文件,然後分析一下,如果一天超過閾值,我們就執行上面的命令,ban掉ip。
有個問題是我們需要的是一直監視access.log 文件,而不是讀一次,當然可以在每天快結束的時候事後來封住ip,但是這樣不能給爬蟲囂張的氣焰,所以
我覺得立即封掉是比較OK的。
之前寫過一個類似tail讀取一個文件的功能,就是打開的時候先定位到文件大小的位置,然後就開始循環讀行來處理,這次的處理有點不一樣,我是直接使用tail 把那個文件的流定位到我的程序的 sys.stdin ,這樣就簡單的可以讀到所有的流了
tail /var/log/nginx/access.log | python <your_python_script>.py
但是nginx的access.log每天都會做一次logrotate,它是怎麼做的呢,nginx官網的推薦方式
mv access.log access.log.0
kill -USR1 `cat master.nginx.pid`
sleep 1
gzip access.log.0 # do something with access.log.0
我的vps上面使用的是logrotated來處理的,可以在/etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
這里表示的create表示文件會重新創建。其實這樣老的文件就沒有新數據了,但是因為使用的是tail,也沒有eof, 這樣直接讀的時候會發現 file.readline() 的函數會卡住,導致程序假死,這樣程序就不能主動退出了,後面選擇了用select來處理,加了一個10秒的超時,從目前的流量來看,基本上每秒都好多的請求,10秒都沒有數據就是出問題了。
while sys.stdin in select.select([sys.stdin], [], [], 10)[0]:
然後如果沒有select上的話,說明基本上是nginx log文件rotate掉了,所以遇到這樣的情況,我就直接跳出程序,然後把程序終結掉。終結掉怎麼辦呢,本來想在程序裡面重新啟動一下程序,但是感覺可能不妥,所以使用crontab來處理了,每隔2分鍾檢查一下,為了防止多次啟動就用了flock來防止程序重新啟動
*/2 * * * * flock -xn /dev/shm/blocker.lock -c "sh /srv/www/beauty/daemon/nginx_ip_blocker.sh"
這里附上代碼
#coding=utf-8
import sys
import re
import os
import urllib
import urllib2
import datetime
import time, os
import logging
import json
import select
logging.basicConfig(level=logging.DEBUG, datefmt='%Y%m%d %H:%M:%S', format='[%(asctime)s] %(message)s')
"""
iptables -I INPUT -s <your_ip> -j DROP
service iptables save
service iptables restart
本腳本主要是用來把惡意訪問nginx的ip ban掉的腳本
"""
MAX_IP=7000
def get_date():
return time.strftime("%Y%m%d")
def ban_one_ip(ip):
os.system("iptables -I INPUT -s %s -j DROP ; service iptables save ; service iptables restart"%ip)
def find_ip_and_ban():
for ip in ip_map:
if ip_map.get(ip)>MAX_IP:
print "ban ip, count %s:%s"%(ip_map.get(ip),ip)
ip_map[ip] = 0
ban_one_ip(ip)
today = None
ip_map = {}
def process_log(lines):
global ip_map;
global today;
now = get_date()
if now != today:
today = now
ip_map = {}
for line in lines:
ip = line.split(" ")[0]
count = ip_map.get(ip,0)
count += 1
ip_map[ip]=count;
find_ip_and_ban()
COUNT=50
def main():
brk=False
while True:
tmp = 0;
lines =[]
brk=False
while sys.stdin in select.select([sys.stdin], [], [], 10)[0]:
line = sys.stdin.readline()
if not line:
brk=True
print 'read to eof'
break
lines.append(line)
tmp+=1
if tmp>COUNT:
break
else:
brk=True
print 'read time out'
break
process_log(lines)
print "read lines:%s"%len(lines)
if brk:
break
if brk:
print "break out"
if __name__ == "__main__":
main()
Ⅱ 如何突破nginx後台ip訪問限制
一、伺服器全局限IP
#vi nginx.conf
allow 10.115.0.116; #允許的IP
deny all;
二、站點限IP
#vi vhosts.conf
站點全局限IP:
location / {
index index.html index.htm index.php;
allow 10.115.0.116;
deny all;
站點目錄限制
location ^~ /test/ {
allow 10.115.0.116;
deny all;
注意事項:
1. deny 一定要加一個ip,否則直接跳轉到403,不往下執行了;如果403默認頁是同一域名下,會造成死循環訪問;
2. allow的ip段
從允許訪問的段位從小到大排列,如127.0.0.0/24 下面才能是10.10.0.0/16
24表示子網掩碼:255.255.255.0
16表示子網掩碼:255.255.0.0
8表示子網掩碼:255.0.0.0
3. deny all;結尾 表示除了上面allow的其他都禁止
如:
deny 192.168.1.1;
allow 127.0.0.0/24;
allo w 192.168.0.0/16;
allow 10.10.0.0/16;
deny all;
Ⅲ nginx只允許某IP段訪問,如何設置
是的 添的是你不能上網的IP 你用的什麼路由器?命令行模式的你就要寫一條ACL如下access-list 1 deny host 192.168.1.7-192.168.1.254 any any
Ⅳ 如何設置nginx可以讓ip可以直接訪問網站
設置你監聽的埠,設置server_name為ip+埠
server {
listen 9000;
charset utf-8;
server_name xx.xx.xx.xx:9000;
......
}
重啟,然後就內可以使用該IP訪問了容
Ⅳ nginx怎麼設置指定目錄ip訪問限制
nginx中針對目錄進行IP限制 ,這里以phpmyadmin目錄只能讓內網IP訪問,而外網不能訪問的配置方法。
nginxphpmyadmin針對內網ip用戶開放、外網ip用戶關閉(在前面的配置中,location ~ ^/目錄/使用正則, 優先順序高於location /的配置,所以nginx無法對首頁進行解析)
代碼如下:
server{
listen80;
server_nameexample.com;
access_loglogs/access.logmain;
location/{
roothtml;
indexindex.phpindex.htmlindex.htm;
}
location~^/phpmyadmin/{
allow192.168.1.0/24;
denyall;
location~.*.(php|php5)?${
root/var/mailapp/nginx/html;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
includefastcgi_params;
}
}
location~.*.(php|php5)?${
root/opt/nginx/html;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
includefastcgi_params;
}
}
Ⅵ nginx如何禁止代理IP訪問
nginx有禁止ip訪問的功能,比如你想禁止的代理ip是2.2.2.2,那麼配置可以寫:
location / {
deny 2.2.2.2;
}當然nginx非常的靈活,他內也可以禁止某個url,或者容是正則匹配的規則。黑白名單都可以做,功能很強大。我只給你舉了一個簡單的例子。
Ⅶ 如何配置nginx達到只允許域名訪問網址,禁止ip
Nginx 禁止IP訪問
我們在使用的時候會遇到很多的惡意IP攻擊,這個時候就要用到Nginx 禁止IP訪問了。版下面我們就先看權看Nginx的默認虛擬主機在用戶通過IP訪問,或者通過未設置的域名訪問(比如有人把他自己的域名指向了你的ip)的時候生效最關鍵的一點是,在server的設置裡面添加這一行:
listen 80 default;
後面的default參數表示這個是默認虛擬主機。
Nginx 禁止IP訪問這個設置非常有用。
比如別人通過ip或者未知域名訪問你的網站的時候,你希望禁止顯示任何有效內容,可以給他返回500.目前國內很多機房都要求網站主關閉空主機頭,防止未備案的域名指向過來造成麻煩。就可以這樣設置:
server {
listen 80 default;
return 500;
}
Ⅷ nginx 怎麼屏蔽通過ip訪問
我的伺服器也在阿里雲 按照你的說 接入阿里雲的waf對網站進行防護,但是如果回直接通過IP地址訪問網站即答可繞過阿里雲waf,於是希望禁止通過ip訪問網站
打開Nginx的配置文件nginx.conf
在server段里插入如下內容即可
if ($host != 'chaodiquan.com' ) {
return 403;
}
解釋一下,這段的意思是,如果訪問請求不是上面指定的域名,就返回403錯誤。
Ⅸ nginx怎麼配置IP和域名都能訪問
一個nginx伺服器只能有一個虛擬主機允許IP訪問
只要在server_name最後面添加一個default,就可以在其他專nginx沒有定義的屬域名下,使用當前server解析(例如,其他server都沒有定義ip地址作為server_name則用IP訪問會被打到default主機上)
Ⅹ nginx 怎麼配置 ip
工具原料:電腦+nginx
nginx 配置 ip方法如下:
一、將a和b兩個網站部署在同一台伺服器上專,兩個域名解析屬到同一個IP地址,但是用戶通過兩個域名卻可以打開兩個完全不同的網站,互相不影響,就像訪問兩個伺服器一樣,所以叫兩個虛擬主機。
二、配置代碼如下:
三、在伺服器8080和8081分別開了一個應用,客戶端通過不同的域名訪問,根據server_name可以反向代理到對應的應用伺服器。
四、server_name配置還可以過濾有人惡意將某些域名指向主機伺服器。