Nginx stops unknowingly with error bind() to 0.0.0.0:80 failed (98: Address already in use)

nginxUbuntu

We are running a node js application with Nginx as a reverse proxy in demo and prod server.

Nginx has been stopped unknowingly with the below error. I login to the server and simply start nginx manually sudo service nginx start and it starts successfully. But still could not find out the root cause for the shutdown. This occurred twice in demo server and thanks god not in prod server so far.

2018/04/05 12:44:03 [notice] 20030#20030: signal process started
2018/04/06 00:35:49 [notice] 23507#23507: signal process started
2018/04/06 12:08:18 [notice] 25473#25473: signal process started
2018/04/06 12:08:18 [error] 25473#25473: open() "/run/nginx.pid" failed (2: No such file or directory)
2018/04/06 12:08:20 [notice] 25479#25479: signal process started
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to [::]:80 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/04/06 12:08:22 [emerg] 25514#25514: still could not bind()
2018/04/06 12:08:24 [alert] 25475#25475: unlink() "/run/nginx.pid" failed (2: No such file or directory)
2018/04/07 00:02:58 [notice] 28369#28369: signal process started
2018/04/07 00:02:58 [error] 28369#28369: invalid PID number "" in "/run/nginx.pid"
2018/04/07 12:12:44 [notice] 30352#30352: signal process started
2018/04/07 12:12:44 [error] 30352#30352: invalid PID number "" in "/run/nginx.pid"
2018/04/08 00:43:19 [notice] 811#811: signal process started
2018/04/08 00:43:19 [error] 811#811: invalid PID number "" in "/run/nginx.pid"

As per the question, it says the about problem when restarting, but in my case it shutdowns automatically.

As per this question, it suggests adding ipv6only=on to [::]:80, but the below is what the nginx default file has from the installation and I have added nginx config for node js as per the answer

default server config

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

vhost config

server {
    listen 80;
    listen [::]:80;
    server_name demo.api.example.com;

Additional Clarification Needed

I think 20030#20030: signal process started refers to nginx reload. Why does nginx reload twice daily? Is it anything to do with Nginx Amplify Agent or it's nginx's default behavior?

Server Details:

  • OS: Ubuntu 16.04.2 LTS
  • Nginx: nginx/1.10.3
  • Additional Tools: Nginx Amplify Agent, PM2 2.9.2

Best Answer

Check if there is any service using 80 port.

netstat -plnt | grep :80

This will show u which is running on the 80 port