当前位置:操作系统 > 玩转网络 >>

宝塔面板fail2ban提示:日志文件不存在,无法创建解决

【已记录】:fail2ban提示:日志文件不存在,无法创建解决
宝塔用户_vzcwhi发表在BUG提交2024-9-7 23:14[复制链接]41958

环境:fail2ban1.9版本,面板9.0.0。
防cc和防站点扫描时报错:[]日志文件不存在,无法创建。

出问题的站点时时nginx目录网站
查fail2ban_main.py (/www/server/panel/plugin/fail2ban/fail2ban_main.py)
中_get_nginx_log_path方法:
    def _get_nginx_log_path(self, website):
        try:
            nginx_conffile = '/www/server/panel/vhost/nginx/{}.conf'.format(website)

            nginx_conf = public.readFile(nginx_conffile)
            reg = 'access_log\s+(.*\.log)'
            log_path = re.search(reg, nginx_conf)

            nginx_log_path = ""
            if log_path:
                nginx_log_path = log_path.groups(1)[0]
            return nginx_log_path
        except:
            return ""
其中/www/server/panel/vhost/nginx/{}.conf'.format(website) 配置文件找不到,发现:
网站(html)项目的配置,文件名前缀会带有"html_"前缀,所以def _get_nginx_log_path方法一直return“"
解决办法:nginx_conffile = '/www/server/panel/vhost/nginx/{}.conf'.format(website)  改成nginx_conffile = '/www/server/panel/vhost/nginx/{}.conf'.format("html_" + website)即可
改完,不带html_的不能用了。

再次修改:    def _get_nginx_log_path(self, website):


        try:
            # 优先检查不带 "html_" 前缀的配置文件
            nginx_conffile = '/www/server/panel/vhost/nginx/{}.conf'.format(website)
            nginx_conf = public.readFile(nginx_conffile)
            
            if not nginx_conf:
                # 如果没有找到,再检查带 "html_" 前缀的配置文件
                nginx_conffile = '/www/server/panel/vhost/nginx/{}.conf'.format("html_" + website)
                nginx_conf = public.readFile(nginx_conffile)
            
            if not nginx_conf:
                raise FileNotFoundError("未找到Nginx配置文件")
            
            # 在配置文件中查找 access_log 的路径
            reg = r'access_log\s+(.*\.log)'
            log_path = re.search(reg, nginx_conf)
            
            if log_path:
                return log_path.group(1)
            else:
                return ""
        except Exception as e:
            return ""
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,