简体   繁体   English

如何关闭或指定nginx错误日志位置?

[英]How to turn off or specify the nginx error log location?

I compiled the nginx on Ubuntu myself. 我自己在Ubuntu上编译了nginx。 I start my nginx with -c nginx.conf parameter. 我用-c nginx.conf参数启动我的nginx。 In my nginx.conf file, I try to turn off error log with but failed. 在我的nginx.conf文件中,我尝试关闭错误日志但失败了。

error_log /dev/null crit;

Still got the error message: nginx: [alert] could not open error log file: open() "/usr/nginx/logs/error.log" failed (2: No such file or directory) 仍然收到错误消息:nginx:[alert]无法打开错误日志文件:open()“/ usr / nginx / logs / error.log”失败(2:没有这样的文件或目录)

How could I turn off this log or change its location? 我怎么能关掉这个日志或改变它的位置?

The syntax for disabling the error log is ok, but the docs state that a default logfile is used before the config is read. 禁用错误日志的语法是可以的,但文档声明在读取配置之前使用了默认日志文件。 (which seems reasonable because how would it otherwise tell you you have an error in your config) (这似乎是合理的,因为它会如何告诉你配置中有错误)

Try creating this file by hand with the correct permissions for the user that runs nginx. 尝试手动创建此文件,并为运行nginx的用户提供正确的权限。 Or try starting the server as root. 或者尝试以root身份启动服务器。

I solved this issue using the -p parameter when starting nginx eg: 我在启动nginx时使用-p参数解决了这个问题,例如:

/home/ubuntu/nginx/sbin/nginx -c /home/ubuntu/nginx/conf/nginx.conf -p /home/ubuntu/nginx

This will prepend any log paths specified in the config with the prefix directory. 这将在配置中使用前缀目录预先添加任何日志路径。

I just solved this by using this configure parameter: 我刚刚使用这个configure参数解决了这个问题:

./configure --prefix="$HOME/nginx" --error-log-path=/dev/stderr

Since I never use this nginx as a daemon and always run it in a shell, it makes sense for it to log errors to stderr. 因为我从不使用这个nginx作为守护进程并且总是在shell中运行它,所以将错误记录到stderr是有意义的。

You can't solve the problem by specifying a -p prefix; 您无法通过指定-p前缀来解决问题; because that would only apply to the directives in the configuration file; 因为那只适用于配置文件中的指令; and as RickyA already noted the problem is that there is a compiled-in error log that nginx wants to open even before it opens the configuration. 并且正如RickyA已经注意到的问题是,即使在打开配置之前,nginx也希望打开一个编译错误日志。 Changing permissions of the compiled-in error log is not ideal, for obvious reasons. 由于显而易见的原因,更改已编译的错误日志的权限并不理想。

A workaround is to specify the error log as a configuration on the command line: 解决方法是在命令行上将错误日志指定为配置:

$ nginx -p . -g 'error_log error.log;'

or 要么

$ nginx -p . -g 'error_log stderr;'

You'll still get an [alert] but at least it allowed me to start nginx as non-root on Ubuntu. 你仍然会得到[警告],但至少它允许我在Ubuntu上以非root身份启动nginx。

Per this post on the nginx.org mailing list (extract quoted below), the %%ERROR_LOG_PATH%% is set as a compile option, and checked immediately on startup, causing the "Could not open" alert. 根据nginx.org邮件列表中的这篇文章 (下面引用的摘录), %%ERROR_LOG_PATH%%被设置为编译选项,并在启动时立即检查,导致“无法打开”警报。 Specifying the prefix (with -p ) may help suppress the alert, but only if the %%ERROR_LOG_PATH%% was specified as a relative path at compile time. 指定前缀(与-p )可以帮助抑制警报,但只有%%ERROR_LOG_PATH%%被指定为在编译时的相对路径。

You can check how it is specified in your current executable with 您可以查看当前可执行文件中的指定方式

nginx -V 2>&1 | grep -oE 'error-log-path=\\S*'

That is why the solution proposed by @Michael works for some, but not for others. 这就是为什么@Michael提出的解决方案适用于某些人,但不适用于其他解决方案的原因。

See changelog: 请参阅changelog:

Changes with nginx 0.7.53 与nginx 0.7.53的变化

... ...

*) Change: now a log set by --error-log-path is created from the very start-up. *)更改:现在,从启动时创建--error-log-path设置的日志。

*) Feature: now the start up errors and warnings are outputted to an error_log and stderr. *)功能:现在将启动错误和警告输出到error_log和stderr。

... ...

Now compiled in error_log value always used to log errors at start time (and emits warning if nginx can't open it). 现在编译为error_log值,始终用于在开始时记录错误(如果nginx无法打开它,则发出警告)。 Once config file have been read - error_log from config will be used instead. 一旦读取了配置文件,将使用来自config的error_log。

If you have compiled nginx with error_log path relative to prefix - it should be possible to override startup error_log via -p switch. 如果你已经使用相对于前缀的error_log路径编译了nginx,那么应该可以通过-p switch覆盖启动error_log。

Maxim Dounin Maxim Dounin

There's no need to use a command line parameter. 无需使用命令行参数。 Simply ensure you add the error_log directive to the very top of nginx.conf or at least before any error messages may occur. 只需确保将error_log指令添加到nginx.conf的最顶层,或者至少在发生任何错误消息之前。

nh2's comment from Jan 2016 suggests that this option would have been available for at least a couple years now. nh2从2016年1月开始的评论表明,这个选项现在至少可以使用几年了。 I can confirm it works and it's less hassle. 我可以确认它有效并且不那么麻烦。 Customisation to nginx.conf is less likely to cause issues with updates to a packaged nginx installation than the alternatives. 对于nginx.conf的自定义不太可能导致更新打包的nginx安装问题而不是替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM