【AlpineLinux】使用goaccess分析nginx日志

此脚本可以在alpinelinux上直接运行,如果你是其他操作系统,请适当修改后执行。

脚本依次执行如下操作
0、goaccessnginx 变量对照表
1、安装 goaccess 软件包
2、配置 goaccess 参数
3、配置 nginx 日志
4、下载 geoip 数据库
5、启动 goaccess 服务

#!/bin/sh
#
# Author: wang@rehiy.com
# Refer : https://www.rehiy.com/post/291
#
# Nginx Log Conversion table:
#   $time_local         %d:%t %^
#   $host               %v
#   $http_host          %v
#   $remote_addr        %h
#   $request_time       %T
#   $request_method     %m
#   $request_uri        %U
#   $server_protocol    %H
#   $request            %r
#   $status             %s
#   $body_bytes_sent    %b
#   $bytes_sent         %b
#   $http_referer       %R
#   $http_user_agent    %u
#

#################################################

if ! type goaccess >/dev/null; then
  apk add goaccess
fi

##########################################

cat <<EOF >/etc/goaccess/goaccess.conf 
time-format %T
date-format %d/%b/%Y
log_format %h - %e [%d:%t %^] "%r" %s %b "%R" "%u" "%^" %^ %^ %^ %T
geoip-database /usr/share/geoip/GeoIPCity.dat
ws-url wss://goac.example.org:443/report/d
EOF

##########################################

sed -i 's/  access_log off/# access_log off/' /etc/nginx/nginx.conf

cat <<EOF >/etc/nginx/http.d/access-log.conf
log_format access
    '\$remote_addr - \$remote_user [\$time_local] "\$request" '
    '\$status \$body_bytes_sent "\$http_referer" "\$http_user_agent" "\$http_x_forwarded_for" '
    '\$connection \$upstream_addr \$upstream_response_time \$request_time'
;
access_log /var/log/nginx/access.log access;
EOF

##########################################

cd /usr/share/geoip/

wget https://mailfud.org/geoip-legacy/GeoIP.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPv6.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPCity.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPCityv6.dat.gz

gunzip *.gz

cd /tmp

#################################################

odir=/var/www/default/net.jidns.spry/report

mkdir -p $odir $odir/archive

goaccess /var/log/nginx/access.log \
    -o $odir/index.html \
    --real-time-html \
    --daemonize \

php7实现JWT认证

class JwtAuth
{
    //交换密钥
    private static $key = '123456';

    //头部信息
    private static $header = [
        'alg' => 'HS256', //算法名称
        'typ' => 'JWT', //令牌类型
    ];

    //加密方式
    private static $hash_algos = [
        'HS256' => 'sha256',
    ];

    /**
     * 获取 jwt token
     * @param array $payload jwt载荷,格式如下非必须
     * [
     * 'iss'=>'jwt_admin', //该JWT的签发者
     * 'iat'=>time(), //签发时间
     * 'exp'=>time()+7200, //过期时间
     * 'nbf'=>time()+60, //该时间之前不接收处理该Token
     * 'sub'=>'www.admin.com', //面向的用户
     * 'jti'=>md5(uniqid('JWT').time()) //该Token唯一标识
     * ]
     * @return bool|string
     */
    public static function getToken($payload)
    {
        if (is_array($payload)) {
            $base64header = self::base64UrlEncode(json_encode(self::$header, JSON_UNESCAPED_UNICODE));
            $base64payload = self::base64UrlEncode(json_encode($payload, JSON_UNESCAPED_UNICODE));
            $sign = self::signature($base64header . '.' . $base64payload, self::$header['alg']);
            return $base64header . '.' . $base64payload . '.' . $sign;
        }
        return false;
    }

    /**
     * 验证token是否有效,默认验证exp,nbf,iat时间
     * @param string $token 需要验证的token
     * @return bool|string
     */
    public static function verifyToken($token)
    {
        $tokens = explode('.', $token);
        if (count($tokens) != 3) {
            return false;
        }

        list($base64header, $base64payload, $sign) = $tokens;

        //获取算法
        $header = json_decode(self::base64UrlDecode($base64header), JSON_OBJECT_AS_ARRAY);
        if (empty($header['alg'])) {
            return false;
        }

        //签名验证
        if ($sign != self::signature($base64header . '.' . $base64payload, $header['alg'])) {
            return false;
        }

        $payload = json_decode(self::base64UrlDecode($base64payload), JSON_OBJECT_AS_ARRAY);

        //签发时间大于当前服务器时间验证失败
        if (isset($payload['iat']) && $payload['iat'] > time()) {
            return false;
        }

        //过期时间小宇当前服务器时间验证失败
        if (isset($payload['exp']) && $payload['exp'] < time()) {
            return false;
        }

        //该nbf时间之前不接收处理该Token
        if (isset($payload['nbf']) && $payload['nbf'] > time()) {
            return false;
        }

        return $payload;
    }

    /**
     * base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现
     * @param string $input 需要编码的字符串
     * @return string
     */
    private static function base64UrlEncode($input)
    {
        return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
    }

    /**
     * base64UrlDecode https://jwt.io/ 中base64UrlEncode解码实现
     * @param string $input 需要解码的字符串
     * @return bool|string
     */
    private static function base64UrlDecode($input)
    {
        $remainder = strlen($input) % 4;
        if ($remainder) {
            $addlen = 4 - $remainder;
            $input .= str_repeat('=', $addlen);
        }
        return base64_decode(strtr($input, '-_', '+/'));
    }

    /**
     * HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现
     * @param string $input 为base64UrlEncode(header).'.'.base64UrlEncode(payload)
     * @param string $alg  算法方式
     * @return mixed
     */
    private static function signature($input, $alg = 'HS256')
    {
        return self::base64UrlEncode(
            hash_hmac(self::$hash_algos[$alg], $input, self::$key, true)
        );
    }
}

//测试和官网示例是否匹配
$payload = ['sub' => '1234567890', 'name' => 'John Doe', 'iat' => 1516239022];
$token = JwtAuth::getToken($payload);

//对token进行验证签名
$getPayload = JwtAuth::verifyToken($token);

echo $token;
echo "\n\n";
var_dump($getPayload);