nginx操作cookie使测试IP不写入access.log

开发者常常有这样的需求,测试的IP或者站长的IP不要写入nginx的access.log中,这样会影响日志阅读的质量。读者第一个想法就是在nginx.conf中配置,如果测试IP和站长的IP不写入access.log,但是常常有这样的情况,比如博主的公司有一个IP池,博主自己也搞不清有多少IP,这样就无法设置通过IP来控制日志。博主想了一个方法,在站长的浏览器端种下cookie,如果nginx解析到这个cookie,则不写入access.log。

nginx.conf如下设置

location =/

{
                if ($http_cookie ~ 'nolog') {
                        access_log off;
                }

                proxy_set_header Host $host;
                proxy_set_header   X-Real-IP   $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://localhost:8080$lang;

}

可以在网站中放一个servlet专门种客户端浏览器的cookie

public class NoNginxLogServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
Cookie cookie = new Cookie("nolog", "nolog");
cookie.setMaxAge(60 * 60 * 24 * 365);
cookie.setPath("/");
response.addCookie(cookie);
}
}

http://www.findmaven.net 是博主的网站,是一个findjar和findmaven的搜索引擎,可以根据class名或者jar名找到包含它的jar和maven gav


 

 

种下cookie后,访问网站就提交了这个nolog cookie,这样这次请求就不会在access.log中留下痕迹。



 

from flask import Flask, render_template, request from datetime import datetime import re app = Flask(__name__) # 基础配置 app.config.update( SESSION_COOKIE_HTTPONLY=True, REMEMBER_COOKIE_HTTPONLY=True, SESSION_COOKIE_SAMESITE='Lax' ) # 检测规则 RULES = [ { 'name': 'admin_access', 'pattern': r'^/admin', 'method': 'GET', 'severity': 'high' }, { 'name': 'sql_injection', 'pattern': r"(\'|--|;|UNION)", 'method': 'POST', 'severity': 'critical' } ] # 日志记录函数 def log_request(current_request): log_entry = f"{datetime.now()} | IP: {current_request.remote_addr} | " \ f"Path: {current_request.path} | Method: {current_request.method}\n" with open('ids_logs.log', 'a') as f: f.write(log_entry) # 入侵检测函数 def detect_intrusion(req): for detection_rule in RULES: if req.method == detection_rule['method']: if re.search(detection_rule['pattern'], req.get_data(as_text=True)): log_alert(req, detection_rule) return True return False def log_alert(current_request, rule): alert_msg = f"[ALERT] {datetime.now()} | Rule: {rule['name']} | " \ f"IP: {current_request.remote_addr} | Severity: {rule['severity']}\n" with open('alerts.log', 'a') as f: f.write(alert_msg) # 请求拦截钩子 @app.before_request def before_request(): log_request(request) if detect_intrusion(request): return "Suspicious activity detected!", 403 # 路由定义 @app.route('/') def index(): return render_template('index.html') @app.route('/logs') def show_logs(): with open('ids_logs.log', 'r') as f: logs = f.readlines() return render_template('logs.html', logs=logs) if __name__ == '__main__': open('ids_logs.log', 'a').close() # 确保日志文件存在 open('alerts.log', 'a').close() app.run(debug=True) 利用此代码进行flask,同事保证各个模板的稳定, 必要时可以修改此代码
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值