站群站点日志优化

需求:

1,每个站点的日志需要记录到请求的域名

2,日志需要自动切割保存前三天的内容,防止日志无限增长

3,日志要有利于二次分析

实现:

1,nginx需要修改两个地方

nginx配置

                    时间           IP            真实IP                  请求方法GET POST   协议http   请求域名     请求连接        返回状态    请求来源          发送大小            相应时间         客户端信息
log_format custom '$time_local ▐ $remote_addr ▐ $http_x_forwarded_for ▐ $request_method ▐ $scheme ▐ $http_host▐  $request_uri ▐ $status ▐ $http_referer ▐  $body_bytes_sent ▐  $request_time ▐ $http_user_agent';

需要在http内,server外增加以上代码

 站点配置

access_log  /www/wwwlogs/www.xxx.cn.log custom;

 

2,开启宝塔定时任务

 

3,站点在站群系统上传时,修改配置文件

 

$sites = Db::table('web_site')->where(['status' => 1, 'delete_at' => 0])->select();
foreach ($sites as $site) {
    // 配置自定义站点日志代码
    $client = Db::table('web_server_client')->where(['id' => $site['server_client_id']])->find();
    if($client) {
        $bt = new Bt($client['url'], $client['key']);
        $nginx = $bt->GetFileBody('/www/server/panel/vhost/nginx/'.$site['domain'].'.conf');
        if($nginx['status'] && isset($nginx['data'])) {
            preg_match('/access_log  \/www\/wwwlogs\/(.*?);/', $nginx['data'], $result);
            $content = '';
            if($result && isset($result[1])) {
                if(strstr($result[1], 'custom') === false) {
                    $content = str_replace($result[1], $result[1].' custom', $nginx['data']);
                    $msg = $bt->SaveFileBody('/www/server/panel/vhost/nginx/'.$site['domain'].'.conf', $content);
                    var_dump('更新站点配置'.$site['domain']);
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值