Nginx is one of the popular web servers used in DigitalOcean Droplets.
Unfortunately, server owners often see Nginx errors when it cannot bind to the correct port of the server.
One such error is “Nginx: [emerg] bind() to failed (98: Address already in use).”
At Bobcares, we help server owners to fix these Nginx errors as part of our Dedicated Support Services.
Today, we’ll see the steps involved in fixing the “Nginx: bind() failed: Address already in use” error.
What causes the error “Nginx: [emerg] bind() to failed (98: Address already in use)”?
First of all, we’ll see the exact cause for the “Nginx:[emerg] bind() to failed (98: Address already in use)” error.
It’s a matter of fact that all services on the server need a unique port to run. Thus, mail service runs on port 25, web service on port 80, secure web traffic on port 443 and so on.
Additionally, this means that two services can never run on the same port.
Therefore, this error simply means that some other service is already running on the port.
For instance, customer’s Droplet showed the following Nginx errors in the log file /var/log/nginx/error.log
2018/08/29 16:04:40 [emerg] 14744#14744: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/08/29 16:04:40 [emerg] 14744#14744: bind() to 0.0.0.0:443 failed (98: Address already in use)
And because of the error, web server was not listening on port 80 or 443.
How to fix “Nginx: [emerg] bind() to failed (98: Address already in use)” in a DigitalOcean Droplet
Now, we know that the error happens as another service is running on Nginx port. So to fix, we need to stop the current process and make Nginx listen on the port.
Let’s see the exact steps that our Expert Engineers do on the Droplet to fix the error.
1. Identify the service on port
First and foremost, we need to find out the process that is already running on port 80. For this, we use the command netstat. Using server root login, the exact command would be:
netstat -tulpn | grep --color :80
And, it shows the service that is already listening on port 80 of the server.
tcp6 0 0 :::80 :::* LISTEN 2078/apache2
Here, the server had both Apache and Nginx installed. And, the server owner had already started Apache and it was listening on port 80 of the Droplet. That’s why, Nginx restart showed errors.
2. Stop the alternate service
As the second step, we need to stop the alternate service running. To kill the process listening on port 80, we run the following command as root user.
fuser -k 80/tcp
Similarly, to kill process listening on 443, we can use the command as
fuser -k 443/tcp
After killing the process, our Support Engineers additionally verify that no service is running on port 80 of the Droplet.
3. Start Nginx
Now, we can start Nginx again. Prior to the restart, it is worth to check for any configuration errors in Nginx. Therefore, the exact sequence of commands that we do include:
nginx -t
Followed by a restart with the command,
service nginx start
That fixes the Nginx error in the Droplet.
[Having trouble with the web server? We can fix the Nginx errors for you.]
Conclusion
“Nginx:[emerg] bind() to failed (98: Address already in use)” is a common error that happens in DigitalOcean Droplets when more than one service tries to listen on a port. Today, we’ve seen how our Dedicated Support Engineers diagnose and fix this error.
Thanks
Wow, thank you very much this website helped me solve the problem I was with struggling to fix. ?
Thanks for the feedback Diego.
We are glad to know that it worked for you ? .
still fighting with nginx for rtmp only on 1935,,, netstat returns nothing on 1935. but it stills fails,,, any idea??
pi@raspberrypi:~ $ sudo netstat -p -a | grep 1935
pi@raspberrypi:~ $ 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: e
Active: failed (Result: exit-code) since Wed 2023-02-01 07:23:50 EST; 6min a
Docs: man:nginx(8)
Process: 21702 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_proces
Process: 21703 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (co
Feb 01 07:23:48 raspberrypi systemd[1]: Starting A high performance web server
Feb 01 07:23:48 raspberrypi nginx[21703]: nginx: [emerg] bind() to 0.0.0.0:1935
Feb 01 07:23:48 raspberrypi nginx[21703]: nginx: [emerg] bind() to 0.0.0.0:1935
Feb 01 07:23:49 raspberrypi nginx[21703]: nginx: [emerg] bind() to 0.0.0.0:1935
Feb 01 07:23:49 raspberrypi nginx[21703]: nginx: [emerg] bind() to 0.0.0.0:1935
Feb 01 07:23:50 raspberrypi nginx[21703]: nginx: [emerg] bind() to 0.0.0.0:1935
Feb 01 07:23:50 raspberrypi nginx[21703]: nginx: [emerg] still could not bind()
Feb 01 07:23:50 raspberrypi systemd[1]: nginx.service: Control process exited,
Feb 01 07:23:50 raspberrypi systemd[1]: nginx.service: Failed with result ‘exit
Feb 01 07:23:50 raspberrypi systemd[1]: Failed to start A high performance web
pi@raspberrypi:~ $
Hi Greg,
Please contact our support through live chat(click on the icon at right-bottom).