Kong自定义日志格式

2020-02-17  本文已影响0人  印随2018

一、背景

Kong使用nginx作为运行时,因此把nginx的配置文件也进行了封装,当你在使用kong的时候,不需要直接修改nginx的配置文件nginx.conf,而是由Kong在启动的时候自动生成。尽管是这样,Kong不可能把nginx.conf的每一个配置项都进行封装,结果就导致开发者无法对nginx.conf进行自定义配置

二、自定义配置

值得庆幸的是,Kong提供了另一种途径来实现自定义nginx.conf,使用--nginx-conf即可

[root@iZt4ngnsfulysl7yiqp769Z kong]# kong start -h
Usage: kong start [OPTIONS]

Start Kong (Nginx and other configured services) in the configured
prefix directory.

Options:
 -c,--conf        (optional string)   Configuration file.

 -p,--prefix      (optional string)   Override prefix directory.

 --nginx-conf     (optional string)   Custom Nginx configuration template.

 --run-migrations (optional boolean)  Run migrations before starting.

 --db-timeout     (default 60)        Timeout, in seconds, for all database
                                      operations (including schema consensus for
                                      Cassandra).

 --lock-timeout   (default 60)        When --run-migrations is enabled, timeout,
                                      in seconds, for nodes waiting on the
                                      leader node to finish running migrations.
 --v              verbose
 --vv             debug

自定义日志格式

  1. 创建nginx的配置文件的模板
cat > nginx.template <<EOF
pid pids/nginx.pid;
error_log logs/error.log notice;

# injected nginx_main_* directives
daemon on;
worker_rlimit_nofile 16384;
worker_processes auto;

events {
    # injected nginx_events_* directives
    worker_connections 16384;
    multi_accept on;
}

http {
    log_format my_format '[\$remote_addr] - [\$time_local] '
        '[\$request_uri] [\$status] [\$body_bytes_sent] '
        '["\$http_referer"] ["\$http_user_agent"]';

    include 'nginx-kong.conf';
}
EOF
  1. 修改/etc/kong/kong.conf
proxy_access_log = logs/access.log my_format
  1. 使用nginx的配置文件模板启动kong
kong start --nginx-conf nginx.template
  1. 检查配置模板是否生效
cat /usr/local/kong/nginx.conf
  1. 查看日志文件
curl http://127.0.0.1:8000
cat /usr/local/kong/logs/access.log
上一篇下一篇

猜你喜欢

热点阅读