问答一下,轻松解决,电脑应用解决专家!
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win10/Win11
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascriptLinux系统
PHP教程CSS教程XML教程

Nginx重启报错Address already in use的解决

更新时间:2022-03-03 11:08 作者:佚名点击:

我服务器的系统是CentOS7.5 ,在修改Nginx配置后重启Nginx以使用配置生效,我使用了以下命令:

systemctl restart nginx

这次遇到错误:

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

我使用systemctl status nginx命令查看有如下提示:

nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2021-01-29 17:44:21 CST; 16s ago
  Process: 9242 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 9239 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 9238 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 28465 (code=killed, signal=KILL)
   CGroup: /system.slice/nginx.service
           ├─2655 nginx: master process /usr/sbin/nginx
           └─2656 nginx: worker process

Jan 29 17:44:19 vktlinux-1 nginx[9242]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Jan 29 17:44:20 vktlinux-1 nginx[9242]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 29 17:44:20 vktlinux-1 nginx[9242]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Jan 29 17:44:20 vktlinux-1 nginx[9242]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 29 17:44:20 vktlinux-1 nginx[9242]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Jan 29 17:44:21 vktlinux-1 nginx[9242]: nginx: [emerg] still could not bind(

这个错误怎么解决呢?看样子是由于80端口被占用引起的,可是我没能其它程序 使用80端口呢?
正确的解释是nginx自己在重启的过程中资源没有完全释放,我只需要把nginx进程强制关闭才行。
使用如下命令查看进程

# ps -e | grep nginx

# ps -e | grep nginx

然后使用如下命令结束进程

# kill -l PID

这个PID就是我们第一个命令返回的第一列。

然后我们再尝试启动Nginx,发现可以正常启动了。

systemctl start nginx

如果还是搞不定或还有一个终级解决办法,那就是重启服务器。

写在最后,那我们以前如何避免类似的错误呢?
可以使用以下方法,尽量不要使用 restart 重启Nginx,特别是在有大量的访问请求呢,很容易出错。我们可以选择在请求很少的深夜重启Nginx,如何确实要在白天进行修改配置,请使用以下命令来加载配置而不是重启。Nginx需要重启的情况很少。

systemctl reload nginx

或者如下命令

nginx -s reload

这样Nginx就能温和的使用新配置了,又避免了出现错误。

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容