Unlike Apache's graceful
option, Nginx provides an even more efficient way to apply configuration changes. The key command is:
sudo nginx -s reload
This sends a HUP signal to the Nginx master process, which then:
- Checks the new configuration for syntax errors
- If valid, starts new worker processes with the new configuration
- Gracefully shuts down old worker processes after finishing current requests
Always test your configuration first to avoid downtime:
sudo nginx -t
Sample output when successful:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
For more control, you can directly signal the master process:
# Find the master process ID
ps -ef | grep nginx | grep master
# Send HUP signal directly
sudo kill -HUP [master_pid]
To ensure smooth reloads:
# In nginx.conf
worker_shutdown_timeout 30s; # Give workers time to finish
reuse_port on; # For seamless socket binding
Common problems and solutions:
- Port in use: Check for stale Nginx processes with
sudo lsof -i :80
- Permission denied: Ensure Nginx user has access to all config files
- Partial reload: Some modules may require full restart (e.g., stream module changes)
When working with Apache, administrators commonly use apachectl graceful
to apply configuration changes without interrupting active connections. Nginx offers similar functionality through its reload
command, which maintains existing connections while loading the new configuration.
The standard method to reload Nginx configuration is:
sudo systemctl reload nginx
# or alternatively:
sudo nginx -s reload
When you execute the reload command, Nginx:
- Checks the configuration file syntax
- If valid, spawns new worker processes with the updated configuration
- Gracefully terminates old worker processes after they complete current requests
- Maintains all active connections during the transition
Here are typical scenarios where you'd want to reload rather than restart:
# After modifying nginx.conf
sudo nginx -t && sudo nginx -s reload
# When adding new virtual hosts
sudo cp new-site.conf /etc/nginx/conf.d/
sudo nginx -t && sudo systemctl reload nginx
For mission-critical environments, consider this enhanced approach:
# First test the configuration
sudo nginx -t
# If successful, send USR2 signal for binary upgrade
sudo kill -USR2 $(cat /var/run/nginx.pid)
# Then send WINCH to old master
sudo kill -WINCH $(cat /var/run/nginx.pid.oldbin)
# Finally terminate old master
sudo kill -QUIT $(cat /var/run/nginx.pid.oldbin)
If reload fails, check:
- Configuration syntax errors (
nginx -t
) - Permission issues on config files
- Available memory for new worker processes
- Error logs (
tail -f /var/log/nginx/error.log
)