NGINX error 98: Address already in use – How to fix

Updated: January 20, 2024 By: Guest Contributor Post a comment

The Error

If you’re managing web servers, encountering errors can be a routine affair, but knowing how to swiftly address these issues is what makes an efficient system administrator. One common issue, particularly with NGINX, is the ‘error 98: Address already in use’ message. This error signals that NGINX is attempting to bind to a server port that is already occupied by some other process.

Understanding the root causes of this error is critical to resolving it. Often, this could be because an instance of NGINX is already running or another application is using the same port. Occasionally, it’s due to lingering processes that haven’t been properly shut down or a conflict with another service’s configuration.

Solutions

Solution 1: Check for Running Processes

The most straightforward solution is to check if any NGINX processes are already running. Two NGINX processes trying to bind to the same port will cause one to raise an error.

  • Use the ps command to search for running NGINX processes.
  • If a process is found, you need to decide whether to stop the existing process or change the configuration for the one you’re trying to start.
  • To stop the existing NGINX process, you use the kill command with the process ID (PID).
ps -aux | grep nginx
kill -QUIT {PID}

Note: Using -QUIT allows NGINX to shut down gracefully. Alternatively, if you’re sure it’s safe to do so, you could use kill -9 {PID} to force the process to stop immediately. Remember, forcefully killing a process can lead to data loss.

Solution 2: Check for Conflicting Services

Sometimes other services may be listening on the same port that NGINX is configured to use. In such cases, you have to identify and stop the conflicting service.

  • Use the netstat or ss utility to check which service is using the port.
  • If a conflicting service is identified, either stop that service or reconfigure NGINX or the other service to use a different port.
netstat -tulnp | grep :80 # Replace 80 with your port number
# or
ss -tulnp | grep :80

Note: It’s important to know what services are running on your system and if it’s safe to stop them. Stopping critical services may lead to system instability.

Solution 3: Reuse the Port

NGINX has a configuration setting called reuseport which, when enabled, allows multiple sockets to listen on the same IP address and port combination without errors.

  • Add the reuseport parameter to your listen directive in the NGINX configuration.
  • Test if the NGINX configuration is correct with nginx -t.
  • Reload the NGINX configuration to apply changes.
http {
    server {
        listen 80 reuseport;
        ...
    }
}
nginx -t
systemctl reload nginx

Note: reuseport can improve performance by load-balancing incoming connections across multiple worker processes. However, it requires a kernel version of at least 3.9 and can lead to uneven load distribution in some scenarios.