Yoa*_*ner 18 configuration nginx log-files logging
我在 Ubuntu 14.04 服务器上使用默认的 nginx 包。它用/etc/nginx/nginx.conf
作主配置,然后包含来自/etc/nginx/conf.d/*.conf
和 的配置/etc/nginx/sites-enabled/*
。
默认的 nginx 配置有这个指令用于记录到访问日志
access_log /var/log/nginx/access.log;
我想添加 X-Forwarded-For 标头,所以我在conf.d
文件夹内执行此操作:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,然后我在 access.log 文件中得到了两条记录——一条带有标题信息,另一条没有。
我知道我可以覆盖nginx.conf
文件本身,但如果可能的话,我宁愿避免它。我还想继续使用相同的日志文件 ( access.log
)。
有没有办法告诉 nginx 覆盖之前的指令并简单地更改日志格式而不修改主nginx.conf
文件?
小智 7
您的问题的答案是否定的,您不能在 nginx 中的任何级别覆盖 log_format 并且在同一级别时您不能覆盖 access_log,除非将其关闭。但是,您可以在不更改 nginx.conf 的情况下实现您想要的,但您必须在服务器 {} 级别执行此操作。
这里的问题是 conf.d/* 的包含在 http {} 中,这正是 access_log 指令所在的位置。您可以在不触及 nginx.conf 的情况下更改您正在使用的任何服务器(如果您没有设置,您将使用位于 /etc/nginx/sites-enabled/default 的默认服务器)。因此,要在不更改 nginx.conf 的情况下实现相同的目标,您应该将 conf.d 文件夹中的文件更改为:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log off;
然后在您的服务器中{} 放置:
access_log /var/log/nginx/access.log main;
这应该让你得到你想要的东西。
归档时间: |
|
查看次数: |
8376 次 |
最近记录: |