SoFunction
Updated on 2025-05-04

Nginx restart failure troubleshooting and solutions

Preface

In Linux systems, NGINX is a high-performance web server and reverse proxy server, and is widely used in various production environments. However, as a powerful and flexible tool, NGINX also often faces some common failure problems, especially errors during restarting. Especially when we need to modify or upgrade NGINX configuration, failure of restart operation may cause great trouble. This article will explain in detail the troubleshooting and solutions for NGINX restart failures, helping everyone quickly locate and solve problems, and ensuring that NGINX can operate normally.

1. Confirm that all NGINX processes have been terminated

1.1 Forced termination of NGINX process

A common reason for failure of NGINX restart is that the process is not completely cleaned. In Linux, sometimeskillThe command cannot completely terminate some child processes, causing NGINX to fail to start normally. We can usepkillCommand to force terminate all NGINX processes.

sudo pkill -9 nginx            Forced termination of all nginx process
sudo pkill -9 nginx: worker    If there is worker process残留

By usingps aux | grep nginxThe command checks the NGINX process in the system again to ensure that there are no residual NGINX processes:

ps aux | grep nginx

If you still see the NGINX process, it is possible that some processes are not closed correctly. At this point you can try to use more forced termination methods, or manually find and end a specific process.

1.2 Check for other potential processes

In some cases, even if the NGINX process has been killed, there may still be other processes that occupy NGINX-related resources (for example, some web applications or database services). At this time, uselsoforssThe command to check the occupancy of port 80 or 443 may be helpful.

sudo ss -tulnp | grep -E ':80|:443'   Check 80/443 Port occupation

Through this command, we can see if there are other services that occupy NGINX's default port 80 or port 443. If the port is occupied by other services, you need to stop these services or configure NGINX to use other ports.

2. Delete the remaining PID files and lock files

2.1 Delete PID file

After NGINX stops or crashes, a file may be left behind, which stores the PID (process ID) of the main NGINX process. If the file still exists, NGINX will think that the service is still running, resulting in the failure to restart. Therefore, before restarting, make sure to delete all relevant PID files.

Common PID file paths include:

sudo rm -f /run/       delete PID document
sudo rm -f /var/run/   Some systems may be in this path

2.2 Delete the lock file

In some systems, NGINX may also generate lock files (e.g./var/lock/) prevents multiple instances from starting at the same time. Make sure to delete these lock files to avoid affecting NGINX's restart operation.

sudo rm -f /var/lock/   Delete the lock file(If there is)

3. Check port occupancy

Even if we have terminated the NGINX process, port 80 or 443 may still be occupied by other applications. This usually happens in environments where multiple services coexist, such as running Apache, Docker, or other web servers simultaneously. To troubleshoot port occupation, you can use the following command:

sudo ss -tulnp | grep -E ':80|:443'   Check 80/443 Port occupation

If the port is occupied, usesystemctlorserviceCommands stop the service that occupies the port, or change the listening port by modifying the NGINX configuration file.

sudo systemctl stop apache2       Stop the service that occupies the port
sudo systemctl stop docker        stopdockercontainer

In addition, modifying the NGINX configuration file (usually located in /etc/nginx/or /etc/nginx/sites-enabled/) to change the port configuration is also a solution.

4. Check the NGINX configuration file

A common reason for failure of NGINX restart is a configuration file error. To troubleshoot configuration files, you can use the configuration test commands that come with NGINX:

sudo nginx -t

This command will check whether the syntax of the NGINX configuration file is correct. If there is a syntax error in the configuration file, the command output will give detailed error information, helping us locate the problem.

If the output prompts that some configuration files have problems, we can fix the corresponding configuration files according to the prompt. Common configuration file paths include:

  • /etc/nginx/
  • /etc/nginx/sites-enabled/Configuration files in the directory

4.1 Common configuration errors

  • Port conflict: If multiple services try to listen to the same port (such as 80 or 443), it will cause NGINX to fail to start.
  • Syntax error: The NGINX configuration syntax is incorrect, such as missing semicolons or mismatched brackets.

When troubleshooting configuration files, pay attention to checking line by line and ensuring the correctness of each configuration.

5. Check file permissions and logs

5.1 Check permissions

NGINX requires read and write permissions to the website directory or related resource directory. If the NGINX user does not have permission to access these files, it may cause startup failure. Ensure NGINX users (usuallywww-dataornginx) Have correct permissions to the relevant directories.

sudo chown -R www-data:www-data /var/www/html   Debian/Ubuntu system
sudo chown -R nginx:nginx /usr/share/nginx/html   RHEL/CentOS system

5.2 View log files

NGINX's error log can help us quickly diagnose problems. Viewing NGINX's error logs, especially recent log entries, can provide us with a solution. Common error log paths are:

sudo tail -50 /var/log/nginx/   Check out the latest50Log

According to the prompts in the log, we can further confirm whether it is a permission problem, a binding failure or another error.

6. Force reload Systemd

In some cases,systemdThe old state of NGINX may be cached, causing the restart operation to fail. Can be reloadedsystemdTo solve this problem:

sudo systemctl daemon-reload      Reload the service configuration
sudo systemctl reset-failed      Reset failed status
sudo systemctl start nginx       Start again NGINX

This will ensure that the NGINX service is started with the latest configuration.

7. Temporarily shut down SELinux or firewall (for test)

7.1 Close SELinux

In RHEL/CentOS systems, SELinux may prevent NGINX from booting, especially in environments with strict security policies. You can try to temporarily disable SELinux to check if this problem causes NGINX to fail to boot.

sudo setenforce 0              Temporary Close SELinux
sudo systemctl start nginx     Whether the test was successful

If NGINX is successfully started after SELinux is disabled, it means that the limitations of the SELinux policy are causing the problem. At this point, you can adjust the SELinux policy to allow NGINX to pass, or consider keeping SELinux off in the test environment.

7.2 Configuring the firewall

Firewall settings may prevent NGINX from accessing external requests. Ensure that firewall rules allow HTTP/HTTPS traffic to pass. For Ubuntu systems, you can use the ufw command:

sudo ufw allow 80/tcp          allowHTTPflow

In RHEL/CentOS system, you can usefirewall-cmdOrder:

sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --reload

8. Ultimate solution: Reinstall NGINX after complete uninstallation

If none of the above methods solve the problem, it may be that the installation or configuration file of NGINX may be corrupted. At this point, you can consider completely uninstalling NGINX and reinstalling it.

sudo apt purge nginx*            Debian/Ubuntu system
sudo yum remove nginx*          RHEL/CentOS system
sudo rm -rf /etc/nginx /var/log/nginx   Clean the residual configuration
sudo apt install nginx          Reinstall
sudo systemctl start nginx      Start a new instance

After reinstallation, NGINX should boot normally.

Conclusion

The reasons for the failure of NGINX restart may be multifaceted, from the process not completely terminated to port occupation, configuration file errors, permission problems, etc. Through the troubleshooting steps in this article, you can troubleshoot and resolve problems one by one to ensure that NGINX can start smoothly. If the conventional method cannot solve the problem, you can consider uninstalling and reinstalling NGINX to ensure the stable operation of the service.

The above is the detailed content of Nginx restart failure troubleshooting and solutions. For more information about Nginx restart failure, please follow my other related articles!