问题描述:
在使用宝塔面板管理 Nginx 服务过程中,遇到了服务突然停止的问题。日志显示大量的 close() channel failed (9: Bad file descriptor)
以及 worker process exited on signal 9
错误。此外,还出现了 fork() failed while spawning "worker process" (12: Cannot allocate memory)
,表明系统内存不足,无法创建新的工作进程。
2024/04/14 03:38:04 [alert] 2341#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:04 [alert] 2341#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:04 [alert] 6197#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:04 [alert] 6193#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:04 [alert] 6199#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:04 [alert] 6192#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:02 [alert] 6238#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:04 [alert] 6239#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:05 [alert] 6238#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:05 [alert] 6239#0: close() channel failed (9: Bad file descriptor)
2024/04/14 03:38:10 [alert] 2341#0: worker process 6238 exited on signal 9
2024/04/14 03:38:20 [alert] 2341#0: worker process 6192 exited on signal 9
2024/04/14 03:38:31 [alert] 2341#0: worker process 6197 exited on signal 9
2024/04/14 03:38:40 [alert] 2341#0: worker process 6346 exited on signal 9
2024/04/14 03:38:51 [alert] 2341#0: worker process 6357 exited on signal 9
2024/04/14 03:39:05 [alert] 2341#0: worker process 6199 exited on signal 9
2024/04/14 03:39:07 [alert] 2341#0: worker process 6311 exited on signal 9
2024/04/14 03:39:14 [alert] 2341#0: worker process 6193 exited on signal 9
2024/04/14 03:39:14 [alert] 2341#0: worker process 6397 exited on signal 9
2024/04/14 03:39:28 [alert] 2341#0: worker process 6239 exited on signal 9
2024/04/14 03:39:28 [alert] 2341#0: worker process 6417 exited on signal 9
2024/04/14 03:39:31 [alert] 2341#0: worker process 6416 exited on signal 9
2024/04/14 03:39:37 [alert] 2341#0: worker process 6423 exited on signal 9
2024/04/14 03:39:39 [alert] 2341#0: worker process 6424 exited on signal 9
2024/04/14 03:39:42 [alert] 2341#0: worker process 6429 exited on signal 9
2024/04/14 03:39:44 [alert] 2341#0: worker process 6330 exited on signal 9
2024/04/14 03:39:44 [alert] 2341#0: worker process 6432 exited on signal 9
2024/04/14 03:39:56 [alert] 2341#0: worker process 6374 exited on signal 9
2024/04/14 03:40:06 [alert] 2341#0: worker process 6486 exited on signal 9
2024/04/14 03:40:20 [alert] 2341#0: worker process 6467 exited on signal 9
2024/04/14 03:42:47 [alert] 2341#0: worker process 6433 exited on signal 9
2024/04/14 03:43:37 [alert] 2341#0: worker process 6523 exited on signal 9
2024/04/14 03:47:58 [alert] 2341#0: worker process 6538 exited on signal 9
2024/04/14 03:48:07 [alert] 2341#0: worker process 6389 exited on signal 9
2024/04/14 03:50:06 [alert] 2341#0: worker process 6463 exited on signal 9
2024/04/14 03:52:28 [alert] 2341#0: worker process 6883 exited on signal 9
2024/04/14 03:53:40 [alert] 2341#0: worker process 7715 exited on signal 9
2024/04/14 03:55:57 [alert] 2341#0: worker process 6970 exited on signal 9
2024/04/14 03:55:57 [alert] 2341#0: fork() failed while spawning "worker process" (12: Cannot allocate memory)
2024/04/14 03:55:57 [alert] 2341#0: could not respawn worker process
2024/04/14 04:48:32 [alert] 2341#0: worker process 8472 exited on signal 9
2024/04/14 04:48:32 [alert] 2341#0: close() channel failed (9: Bad file descriptor)
2024/04/14 04:48:32 [alert] 2341#0: close() channel failed (9: Bad file descriptor)
2024/04/14 04:48:32 [alert] 2356#0: close() channel failed (9: Bad file descriptor)
2024/04/14 04:48:32 [alert] 7853#0: close() channel failed (9: Bad file descriptor)
2024/04/14 04:48:32 [alert] 7730#0: close() channel failed (9: Bad file descriptor)
2024/04/14 04:48:32 [alert] 8237#0: close() channel failed (9: Bad file descriptor)
2024/04/14 04:48:32 [alert] 6466#0: close() channel failed (9: Bad file descriptor)
问题原因:
-
Nginx防火墙与加速插件冲突:安装的免费防火墙插件和网络加速工具可能与 Nginx 的某些配置或其他插件发生冲突,导致资源管理异常。
-
文件描述符泄露:频繁的
Bad file descriptor
错误提示可能是因为文件描述符被不当处理或泄露,导致 Nginx 无法正常关闭旧的连接或开启新的连接。 -
内存不足:
Cannot allocate memory
错误表明 Nginx 在尝试创建新的工作进程时,因系统可用内存不足而失败。
解决方案:
-
卸载或禁用冲突插件:
-
根据问题描述,首先尝试卸载或禁用 Nginx 的防火墙插件和网络加速工具,观察是否能恢复 Nginx 的正常运行。
-
如果确定是这些插件导致的冲突,考虑查找其他兼容性更好的插件或更新这些插件到最新版本。
-
-
检查和优化内存使用:
-
通过命令
free -m
检查当前系统的内存使用情况。 -
如果内存确实不足,尝试增加物理内存或配置交换空间来缓解内存压力。
-
优化 Nginx 的配置,比如调整
worker_processes
和worker_connections
,减少内存使用。
-
-
检查文件描述符限制:
-
使用
ulimit -n
检查当前的文件描述符限制。 -
如果数值较低,可以在系统级别增加文件描述符的限制。编辑
/etc/security/limits.conf
文件,增加如下配置:
markdownCopy code* soft nofile 65535 * hard nofile 65535
-
重新启动宝塔面板和 Nginx 服务,以应用更改。
-
-
查看详细日志:
-
检查 Nginx 的错误日志(通常位于
/var/log/nginx/error.log
),获取更多关于错误的具体信息。 -
根据日志中的详细错误描述,进一步分析和解决问题。
-
-
重新启动 Nginx:
-
在做出上述更改后,尝试重新启动 Nginx 服务,检查服务是否能够正常运行。
-
结论
这种类型的错误通常涉及到资源管理和软件冲突。通过逐步排查和应用上述解决方案,通常可以恢复 Nginx 的正常运行并防止未来类似的问题发生。如果问题依旧存在,建议深入查看系统日志或寻求专业技术支持。