Nginx - Nginx: [Emerg] Bind() to [::]:80 Failed (98: Address Already in Use)

Better Stack Team
Updated on October 7, 2024

The error nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) indicates that Nginx is unable to start because another process is already using port 80 on the IPv6 address [::]. This can happen if another service is bound to the same port or if there’s a configuration issue. Here’s a step-by-step guide to troubleshoot and resolve this issue:

1. Identify the Conflicting Process

Determine which process is using port 80.

On Linux:

  1. Check Listening Ports:

    Use netstat or ss to find the process listening on port 80.

     
    sudo netstat -tuln | grep :80
    

    Or with ss:

     
    sudo ss -tuln | grep :80
    

    This command shows the process and the address it’s bound to.

  2. Find the Process:

    Once you identify that port 80 is in use, find the process using it:

     
    sudo lsof -i :80
    

    Or:

     
    sudo fuser -v 80/tcp
    

    These commands will list the processes using port 80.

  3. Kill or Reconfigure the Conflicting Process:

    If another service (e.g., Apache, another instance of Nginx) is using port 80, you may need to stop it:

     
    sudo systemctl stop apache2    # Example for Apache
    sudo systemctl stop nginx      # Example for Nginx if it's another instance
    

    Alternatively, reconfigure the conflicting service to use a different port.

2. Check Nginx Configuration

Ensure that there are no conflicting Nginx configuration files or duplicate server blocks.

Steps:

  1. Check Nginx Configuration Files:

    Look through your Nginx configuration files to ensure there are no duplicate server blocks attempting to bind to the same port.

     
    sudo grep -R "listen 80" /etc/nginx/
    

    This will help you identify if there are multiple configurations binding to port 80.

  2. Validate Nginx Configuration:

    Use the following command to test the Nginx configuration for syntax errors or other issues:

     
    sudo nginx -t
    

    This command will check the configuration files and report any errors.

3. Restart Nginx

After resolving any conflicts or configuration issues, restart Nginx.

 
sudo systemctl restart nginx

Or, if you're using older init scripts:

 
sudo service nginx restart

4. Review Logs

Check Nginx’s error log for more details if the issue persists.

 
sudo tail -f /var/log/nginx/error.log

5. Handle IPv6 Bindings

If you only need Nginx to listen on IPv4, you can modify your configuration to only bind to IPv4 addresses.

Example Configuration:

 
server {
    listen 80;   # This listens on IPv4
    listen [::]:80 ipv6only=off;  # This listens on IPv6
    server_name example.com;
    root /var/www/html;
}

If you only need IPv4, you can remove or comment out the listen [::]:80 directive.

Summary

  • Identify Conflicting Process: Use netstat, ss, lsof, or fuser to find which process is using port 80.
  • Check and Fix Configuration: Ensure Nginx configurations are correct and do not conflict.
  • Restart Nginx: Apply changes and restart Nginx.
  • Review Logs: Check error logs for additional details.
  • IPv6 Considerations: Adjust configuration if only IPv4 is needed.

By following these steps, you should be able to resolve the issue and start Nginx without the port conflict error.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github