When attempting to restart Nginx, you encounter the critical error:
bind() to 0.0.0.0:80 failed (98: Address already in use)
Your diagnostic commands reveal several important details:
$ netstat -tulpn
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9468/nginx
Surprisingly, Nginx itself (PID 9468) is already listening on port 80, which explains why a restart fails - the master process hasn't properly released the port.
Your sites-enabled configuration shows:
server {
listen 80;
server_name my-website.com www.my-website.com;
return 301 https://$server_name$request_uri;
}
This is the only explicit port 80 listener in your active configuration. The configuration appears correct for HTTP-to-HTTPS redirection.
1. Graceful Nginx Shutdown
First attempt a proper shutdown sequence:
sudo nginx -s quit
sudo service nginx start
2. Forceful Cleanup
If graceful shutdown fails:
sudo pkill -9 nginx
sudo service nginx start
3. Verify Socket Status
Check for orphaned sockets:
sudo ss -lptn 'sport = :80'
Configuration Testing
Always validate configuration before restarting:
sudo nginx -t
Port Conflict Resolution
If another service occupies port 80:
sudo lsof -i :80
sudo kill -9 <PID>
1. Implement Systemd Socket Activation
Create /etc/systemd/system/nginx.socket:
[Socket]
ListenStream=80
ListenStream=443
[Install]
WantedBy=sockets.target
2. Configure Proper Shutdown Hooks
Add to nginx.conf:
worker_shutdown_timeout 10s;
After resolution, verify proper operation:
sudo netstat -tulpn | grep nginx
curl -I http://localhost
The error message bind() to 0.0.0.0:80 failed (98: Address already in use)
indicates that another process is already listening on port 80, preventing nginx from starting. This commonly occurs when:
- Another web server (like Apache) is running
- A previous nginx instance didn't shut down properly
- Multiple nginx configurations are conflicting
First, let's verify which process is using port 80:
sudo lsof -i :80
# OR
sudo netstat -tulpn | grep :80
In your case, the output shows nginx itself is already running on port 80 (PID 9468). This suggests a configuration reload might be better than a full restart.
Instead of service nginx restart
, try these alternatives:
sudo service nginx reload
# OR
sudo nginx -s reload
This gracefully reloads configuration without interrupting existing connections.
Your config shows multiple server blocks listening on port 80 and 443. While this isn't inherently wrong, it can cause issues if:
- Multiple
default_server
declarations exist - Port conflicts between HTTP and HTTPS redirects
Here's a cleaner way to handle HTTP to HTTPS redirects:
server {
listen 80;
server_name my-website.com www.my-website.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name my-website.com;
return 301 https://www.my-website.com$request_uri;
# SSL configuration here...
}
server {
listen 443 ssl default_server;
server_name www.my-website.com;
# Main website configuration...
}
If you must restart nginx completely, first stop it properly:
sudo service nginx stop
sudo pkill -9 nginx
sudo service nginx start
Add this to your nginx.conf to ensure only one worker process can bind to ports:
events {
worker_connections 1024;
accept_mutex on;
}
http {
# Other http config...
server_names_hash_bucket_size 64;
}
Remember to test your configuration before applying changes:
sudo nginx -t