【AWS+Nginx1.10】无法指定错误日志的日志格式

环境

    • AWS

 

    • nginx version: nginx/1.10.3 (Ubuntu)

 

    • Rails5.1

 

    Unicorn

想做的事

    • Nginxのログフォーマットを変更したい

AWSのALBのアクセス元IPを表示したい
アクセス元プロトコルを表示したい
ついでにエラーログもフォーマットを変更したい

设定示例

/etc/nginx/nginx.conf 可以被改写为:nginx的主配置文件位于/etc/nginx/nginx.conf。

在access_log和error_log中指定了名为main的自定义格式。

        ##
        # Logging Settings
        ##

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" $request_time $upstream_response_time '
                        '"$http_x_forwarded_for" "$http_x_forwarded_proto" $host';

        access_log /var/log/nginx/access.log main;
        error_log /var/log/nginx/error.log main;

发生的问题

    Nginxが起動しない

导火线

由于在Nginx的错误日志中指定了独特的主格式,所以在进行语法检查时会显示以下错误。

$ nginx -t -c /etc/nginx/nginx.conf
2018/01/16 15:34:00 [emerg] 29059#29059: invalid log level "main" in /etc/nginx/nginx.conf:53
nginx: configuration file /etc/nginx/nginx.conf test failed

正确的设置

错误日志不能指定格式。

        ##
        # Logging Settings
        ##

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" $request_time $upstream_response_time '
                        '"$http_x_forwarded_for" "$http_x_forwarded_proto" $host';

        access_log /var/log/nginx/access.log main;
        error_log /var/log/nginx/error.log;
广告
将在 10 秒后关闭
bannerAds