change access log support

This commit is contained in:
barnettZQG 2020-06-15 18:40:20 +08:00
parent 483632ea6c
commit 50601173c1
4 changed files with 17 additions and 10 deletions

View File

@ -55,6 +55,7 @@ type Config struct {
WorkerRlimitNofile int
ErrorLog string
AccessLogPath string
AccessLogFormat string
ErrorLogLevel string
WorkerConnections int
//essential for linux, optmized to serve many clients with each thread
@ -148,5 +149,8 @@ func (g *GWServer) CheckConfig() error {
}
g.HostIP = ip.String()
}
if os.Getenv("ACCESS_LOG_FORMAT") != "" {
g.Config.AccessLogFormat = os.Getenv("ACCESS_LOG_FORMAT")
}
return nil
}

View File

@ -42,8 +42,6 @@ const (
brotliTypes = "application/xml+rss application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component"
logFormatUpstream = `%v - [$the_real_ip] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id`
logFormatStream = `[$time_local] $protocol $status $bytes_sent $bytes_received $session_time`
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size

View File

@ -40,13 +40,18 @@ type AccessLog struct {
// NewHTTP creates a new model.HTTP
func NewHTTP(conf *option.Config) *HTTP {
return &HTTP{
HTTPListen: conf.ListenPorts.HTTP,
HTTPSListen: conf.ListenPorts.HTTPS,
DefaultType: "text/html",
SendFile: true,
StatusPort: conf.ListenPorts.Status,
AccessLogPath: conf.AccessLogPath,
AccessLogFormat: `%v - [$the_real_ip] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id`,
HTTPListen: conf.ListenPorts.HTTP,
HTTPSListen: conf.ListenPorts.HTTPS,
DefaultType: "text/html",
SendFile: true,
StatusPort: conf.ListenPorts.Status,
AccessLogPath: conf.AccessLogPath,
AccessLogFormat: func() string {
if conf.AccessLogFormat == "" {
return `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time $upstream_addr $upstream_response_length $upstream_response_time $upstream_status`
}
return conf.AccessLogFormat
}(),
DisableAccessLog: conf.AccessLogPath == "",
KeepaliveTimeout: Time{
Num: 30,

View File

@ -16,7 +16,7 @@ http {
lua_package_path "/run/nginx/lua/?.lua;;";
lua_shared_dict configuration_data {{$h.UpstreamsDict.Num}}{{$h.UpstreamsDict.Unit}};
log_format proxy {{$h.AccessLogFormat}}
log_format proxy '{{$h.AccessLogFormat}}';
{{ if $h.DisableAccessLog }}
access_log off;
{{ else if $h.AccessLogPath }}