我尝试将以下内容添加到my_proxy.conf
文件中,该文件将被装入容器中的/etc/nginx/conf.d/
,如https://github.com/jwilder/nginx-proxy#custom-nginx-configuration中所述,但日志未进入logstash。
最初我将其添加到conf文件中:
http {
# Custom log format that also includes the host that processed the request
log_format logstash '$remote_addr - $remote_user [$time_local] "$host" '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
# Send logs to Logstash
access_log syslog:server=logstash:5140,tag=nginx_access logstash;
error_log syslog:server=logstash:5140,tag=nginx_error notice;
}
但当我启动容器时,它告诉我"http" directive is not allowed here in /etc/nginx/conf.d/my_proxy.conf:11
所以现在我就拥有了
# Custom log format that also includes the host that processed the request
log_format logstash '$remote_addr - $remote_user [$time_local] "$host" '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
# Send logs to Logstash
access_log syslog:server=logstash:5140,tag=nginx_access logstash;
error_log syslog:server=logstash:5140,tag=nginx_error notice;
启动nginx时没有任何抱怨,但也没有把任何东西放入logstash。从这里我不知道如何排除故障。
Syslog不会在您的容器中运行,因此将其用于日志记录是没有意义的。你的选择:
1.)在Docker守护程序级别配置gelf
日志记录驱动程序:
将日志消息写入Graylog扩展日志格式(GELF)端点,例如Graylog或Logstash。
Doc:https://docs.docker.com/config/containers/logging/configure/
2.)启动/配置专用日志记录容器,该容器将所选容器的数据发送到logstash。请参阅Logspout
或其他类似工具。
我所做的是修改nginx.tmpl
文件,这是用于生成/etc/nginx/conf.d/default.conf
的模板。
我添加了一行到任何地方access_log
出现。例如:
access_log /var/log/nginx/access.log vhost;
access_log syslog:server=111.222.111.222:1025 custom;
我在另一种格式旁边定义了custom
格式。例如。
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
log_format custom '$remote_addr - $remote_user [$time_local]'
'"$request" $status $body_bytes_sent'
'"$http_referer" "$http_user_agent"'
'"$request_time" "$upstream_connect_time"';
然后将nginx.tmpl
装入容器中。例如。
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./config/nginx.conf:/etc/nginx/nginx.conf
- ./nginx.tmpl:/app/nginx.tmpl
- ./logs:/var/log/nginx
要验证,请重新启动nginx-proxy,bash到nginx-proxy容器中,然后查看生成的/etc/nginx/conf.d/default.conf
。
这就是主意。能够在某处指定syslog URL而不必手动将粘贴复制到nginx.tmpl
中会很高兴。
请注意,尽管Jan指出syslog不能在容器内工作,但这确实有效。不知道为什么,It Just Works™