Popular blog tags

Nginx 启动错误处理排查方法(Ubuntu)

Published

1.系统环境

vps主机:https://www.vultr.com

os:Ubuntu 18*64

2.重启nginx失败,具体排查过程记录如下:

step 1:首先检查80端口是否监听

:~# netstat -ntlp

发现80和443端口没有被nginx监听

output:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6000          0.0.0.0:*               LISTEN      542/dotnet          
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      497/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      674/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1332/cupsd          
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      19401/sshd:  
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      656/select          
tcp        0      0 127.0.0.1:5000          0.0.0.0:*               LISTEN      32686/dotnet        
tcp6       0      0 :::40491                :::*                    LISTEN      608/array           
tcp6       0      0 ::1:6000                :::*                    LISTEN      542/dotnet          
tcp6       0      0 :::22                   :::*                    LISTEN      674/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      1332/cupsd          
tcp6       0      0 ::1:6010                :::*                    LISTEN      19401/sshd:  
tcp6       0      0 ::1:5000                :::*                    LISTEN      32686/dotnet        

 

step 2:执行重启nginx命令,看看是个什么情况

:~# service nginx restart

启动失败提示信息:
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.service
nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-04-24 12:36:17 UTC; 1min 18s ago
     Docs: man:nginx(8)
  Process: 19612 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 19602 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0

Apr 24 12:36:14 vultr.guest systemd[1]: Starting A high performance web server and a reverse proxy server..
Apr 24 12:36:14 vultr.guest nginx[19612]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already 
Apr 24 12:36:15 vultr.guest nginx[19612]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already 
Apr 24 12:36:15 vultr.guest nginx[19612]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already 
Apr 24 12:36:16 vultr.guest nginx[19612]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already 
Apr 24 12:36:16 vultr.guest nginx[19612]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already 
Apr 24 12:36:17 vultr.guest nginx[19612]: nginx: [emerg] still could not bind()
Apr 24 12:36:17 vultr.guest systemd[1]: nginx.service: Control process exited, code=exited status=1
Apr 24 12:36:17 vultr.guest systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 24 12:36:17 vultr.guest systemd[1]: Failed to start A high performance web server and a reverse proxy s

step 3:检查nginx配置是否正确

因为曾经修改过nginx配置参数,首先检查nginx配置是否正确

执行检查nginx配置命令

:~# nginx -t

 

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

配置没有问题

step 4:查看ngix错误日志

vi  /var/log/nginx/error.log

output:

2020/04/24 12:40:00 [emerg] 19889#19889: bind() to 0.0.0.0:443 failed (98: Address already in use)
2020/04/24 12:40:00 [emerg] 19889#19889: bind() to 0.0.0.0:443 failed (98: Address already in use)
2020/04/24 12:40:00 [emerg] 19889#19889: still could not bind()

提示端口被占用了,怪不得nginx启动失败。

step 4:查看是哪个程序占用了该端口

ps -ef |grep nginx
netstat -tln | grep 443
lsof -i :443