Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Nginx Failing to Restart Without Error Messages

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Nginx sometimes fails to restart or reload with no visible error output, leaving you staring at a silent command prompt. This usually means the error is being suppressed by systemd, a syntax check that passes but a config that fails at runtime, or a process that won't die. This guide walks you through diagnosing and fixing the issue step by step, even when nginx appears to give you nothing to work with.

What You'll Need

  • SSH or terminal access to the server running nginx
  • Root or sudo privileges
  • Basic familiarity with the command line
  • Your nginx configuration files (usually in /etc/nginx/)

Step-by-Step

  1. Check the exit code and systemd status. Run sudo systemctl restart nginx; echo $?. If the exit code is non-zero, systemd is failing the restart. Then run sudo systemctl status nginx --no-pager to see the last few log lines. Often the error is hidden here, but not shown when you just run systemctl restart.
  2. Look at the journal for the exact error. Use sudo journalctl -u nginx --no-pager -n 50. This shows the full output from the last restart attempt, including stderr. If it's empty, try sudo journalctl -u nginx --no-pager -n 200 to see more history.
  3. Test the configuration directly. Run sudo nginx -t. This checks syntax and opens all included files. If it reports success but the service still fails, the problem is likely at runtime (e.g., a missing directory, permission issue, or a port already in use).
  4. Run nginx in the foreground. Stop the service (sudo systemctl stop nginx) and run sudo nginx -g 'daemon off;'. This forces nginx to print errors directly to the terminal. If it starts, the issue is with how systemd launches it. If it fails, you'll see the exact error message.
  5. Check for port conflicts. If nginx fails to bind to port 80 or 443, it may exit silently. Run sudo ss -tlnp | grep -E ':80|:443' to see if another process is using those ports. Common culprits are Apache, another nginx instance, or a Docker container.
  6. Verify file permissions. Nginx runs as the nginx user (or www-data on Debian/Ubuntu). Ensure that all directories in the config paths (like /var/www) are readable and executable by that user. Use sudo -u nginx ls /var/www to test.
  7. Check for missing include files. If your config uses include /etc/nginx/sites-enabled/*; and one of those files has a syntax error, nginx -t will catch it. But if a file is missing (e.g., a symlink broken), nginx may fail silently. Run sudo find /etc/nginx -type l -exec ls -l {} \; to see if any symlinks are broken.
  8. Look at the error log. Nginx writes to /var/log/nginx/error.log by default. Tail it with sudo tail -n 30 /var/log/nginx/error.log. If the file is empty, check the error_log directive in your config – it may be pointing to a different path.
  9. Try a clean reload instead of restart. Sometimes a restart fails because the old process won't stop. Run sudo nginx -s reload. If that works, the issue is with the shutdown process. You can then force a stop with sudo systemctl kill -s QUIT nginx and start again.
  10. Check for SELinux or AppArmor. On RHEL/CentOS, SELinux may block nginx from reading certain files. Run sudo ausearch -m avc -ts recent to see denials. On Ubuntu/Debian, check sudo aa-status to see if AppArmor is confining nginx. You may need to adjust policies.

Common Mistakes

  • Forgetting to include the semicolon at the end of directives in the config – this causes syntax errors that nginx -t will catch, but if you skip the test, you'll get a silent failure.
  • Editing the wrong config file – if you have multiple nginx instances (e.g., from a package and a manual build), you might be testing one but restarting another.
  • Assuming the error log is in the default location – if you changed error_log in the main config, the default log may be empty.
  • Not checking if the old process is still running – if the master process is stuck, systemd may wait for it to exit and time out, giving no error.

Tips

  • Always run sudo nginx -t before restarting. It's fast and catches most issues.
  • Use sudo systemctl restart nginx --no-block to get the shell prompt back immediately, then check the status.
  • If you're using a config management tool (like Ansible or Puppet), test the config on a staging server first.
  • Keep a backup of your working config: sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak.
  • Enable verbose logging temporarily by adding error_log /var/log/nginx/error.log debug; to the main config, but remember to revert it.

Troubleshooting

If you've followed the steps and still see no error, try these additional checks:

  • Check systemd unit file overrides. Run sudo systemctl cat nginx to see if there are any overrides that change the executable or arguments. Sometimes a drop-in file sets a wrong ExecStart path.
  • Run nginx with strace. Install strace (sudo apt install strace or sudo yum install strace) and run sudo strace -f -e trace=file,network -o /tmp/nginx_trace.txt nginx -g 'daemon off;'. The trace will show exactly which file or socket fails.
  • Check for disk space. If the disk is full, nginx may fail to write to logs or pid files. Run df -h and sudo du -sh /var/log/nginx.
  • Look at the PID file. If /var/run/nginx.pid exists but the process is dead, nginx may refuse to start. Remove it with sudo rm -f /var/run/nginx.pid and try again.
  • Check for IPv6 issues. If your config has listen [::]:80; but IPv6 is disabled on the system, nginx may fail to bind. Run sudo sysctl net.ipv6.conf.all.disable_ipv6 to check.

Frequently Asked Questions

Why does nginx restart fail but nginx -t passes?

The config syntax is correct, but at runtime nginx can't do something like bind to a port, access a file, or create a socket. Use the foreground method (step 4) to see the runtime error.

What if the error log is empty?

Check the error_log directive in your config – it might be pointing to a different path. Also, if nginx fails before it opens the log, the error goes to systemd's journal. Use journalctl -u nginx.

Can I restart nginx without systemd?

Yes, you can run sudo nginx -s reload to reload config without a full restart. If the master process is dead, you can start it directly with sudo nginx.

How do I know if another process is using port 80?

Run sudo ss -tlnp | grep :80 or sudo lsof -i :80. The output will show the process name and PID.

Why does nginx start manually but not via systemd?

systemd may be setting a different working directory, environment variables, or user. Check the unit file with systemctl cat nginx and compare with your manual run.

What does the exit code 1 mean when restarting nginx?

Exit code 1 is generic. It means the service failed to start. The specific reason is usually in the journal or error log. Follow steps 1-4 to uncover it.