【问题标题】:Nginx with /var/log on tmpfsNginx 与 /var/log 在 tmpfs
【发布时间】:2012-05-08 19:55:35
【问题描述】:

我已将开发机器设置为将 tmpfs 用于 /var/log。鉴于此,在每次启动时,内容都是空白的,并逐渐填满在记录其胆量时创建的任何进程。

问题是(根据我对错误消息的解释):nginx 非常不愿意创建它自己的日志目录(在这种情况下是 /var/log/nginx),所以在启动过程中它不断向我抛出错误:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2012/05/08 21:42:35 [emerg] 2368#0: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

创建目录后,一切正常。 有没有办法告诉 nginx(至少尝试)创建日志目录?

【问题讨论】:

  • 创建它作为初始化脚本的一部分?
  • 我突然想到,解决方案很简单,在 /etc/init.d/nginx 脚本中添加 3 行代码。 :) 很抱歉占用您的时间。 :]
  • 请回答您自己的问题或更改标题以反映其已解决。谢谢

标签: logging nginx tmpfs


【解决方案1】:

作为脚本/etc/init.d/nginx中do_start函数的第一行,添加以下命令:

# Create the folder "nginx" in /var/log otherwise nginx can't start
mkdir -p /var/log/nginx

我不喜欢这个解决方案,但这也是我发现的最简单/最快的解决方案。

问候

编辑 - 如果使用 Debian,init.d 脚本有点不同。 “mkdir”行就在“start”行之后:

start)
    mkdir -p /var/log/nginx

【讨论】:

  • 如果您在 Debian 上使用 systemd,您必须将其添加到 nginx.service 文件中(您可以通过运行 systemctl show nginx.service | grep FragmentPath 找到该文件)。您必须在ExecStartPre=/usr/bin/nginx -t ... 行之前添加ExecStartPre=/bin/mkdir -p /var/log/nginx(因为这会在启动之前测试配置,如果找不到目录,它将失败。)如果您在运行时收到Starting nginx (via systemctl) ...,您的系统将使用systemd /etc/init.d/nginx.
  • sudo sed -i '/^ExecStartPre.*/i ExecStartPre=/bin/mkdir -p /var/log/nginx' $(systemctl show nginx.service | grep FragmentPath | cut -d'=' -f2) 执行@matega 告诉的操作的简短命令。
【解决方案2】:

我意识到这个帖子比小行星上的泥土还要古老,但我想在此处包含这个答案,以便为后代勇敢的搜索者提供帮助,这样他们就可以避免我刚刚让自己经历的辛勤工作。

如果您收到消息:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2012/05/08 21:42:35 [emerg] 2368#0: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

确保你的 var/log 文件夹没有设置为 tmpfs!!!! 我已经设置了这种方式来减少对我系统中 SD 卡的写入,这意味着每次使用 nginx开始,它在 /var/log/nginx 找不到日志或 nginx 文件夹 - 所以它失败并退出。如果在 /etc/fstab 中看到 /var/log 输入,将其注释掉,重新启动,手动创建一次 nginx 文件夹(我还手动创建了 error.log 和 access.log(空白文件))并重新启动 nginx 服务。从那时起,您的 nginx 服务器将运行良好(除非您执行其他操作来破坏它,但那是另一个线程)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    相关资源
    最近更新 更多