Nginx Configuration Errors
Diagnose and fix Nginx configuration errors, syntax issues, permission problems, and failed reloads
Nginx configuration errors prevent the service from starting or reloading. This guide covers the most common errors and how to fix them.
Step 1: Test the configuration
Always run this before restarting Nginx:
sudo nginx -tA valid config outputs:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulA broken config shows the exact file and line number:
nginx: [emerg] unexpected "}" in /etc/nginx/sites-enabled/mysite.conf:24Check logs for more detail:
sudo journalctl -u nginx -n 50 --no-pager
sudo tail -30 /var/log/nginx/error.logError 1: Syntax error
nginx: [emerg] unexpected "}" in /etc/nginx/sites-enabled/mysite.conf:24
nginx: [emerg] invalid parameter "proxY_pass" in ...Fix: Open the file at the indicated line and fix the syntax error. Common mistakes:
- Missing semicolon at end of directive:
root /var/www/html→root /var/www/html; - Unclosed block: missing
}at end ofserver {}orlocation {} - Typo in directive name:
proxY_pass→proxy_pass - Wrong quotes: using
"instead of standard ASCII quotes
sudo nano /etc/nginx/sites-enabled/mysite.confAfter fixing, always re-test:
sudo nginx -t && sudo systemctl reload nginxError 2: Port already in use
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)Find what's using port 80 or 443:
sudo ss -tlnp | grep -E ':80|:443'If Apache is running alongside Nginx:
sudo systemctl stop apache2
sudo systemctl disable apache2
sudo systemctl start nginxIf another Nginx process is stuck:
sudo pkill nginx
sudo systemctl start nginxError 3: Permission denied (socket or log)
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)Fix log file ownership:
sudo chown -R www-data:www-data /var/log/nginx
sudo chmod 755 /var/log/nginx
sudo systemctl restart nginxFor PID file issues:
sudo mkdir -p /var/run/nginx
sudo chown www-data:www-data /var/run/nginxError 4: upstream host not found (reverse proxy)
nginx: [emerg] host not found in upstream "myapp" in /etc/nginx/sites-enabled/myapp.conf:10This means the upstream hostname doesn't resolve at startup. Use an IP or ensure the hostname resolves:
# Use IP instead of hostname
upstream myapp {
server 127.0.0.1:3000;
}Or add resolver directive for dynamic upstreams:
resolver 127.0.0.53 valid=30s;Error 5: SSL certificate not found
nginx: [emerg] cannot load certificate "/etc/letsencrypt/live/example.com/fullchain.pem": BIO_new_file() failedCheck if the certificate file exists:
sudo ls -la /etc/letsencrypt/live/example.com/If missing, issue or renew the certificate:
sudo certbot certonly --nginx -d example.com -d www.example.comIf it exists but permissions are wrong:
sudo chmod 755 /etc/letsencrypt/live/
sudo chmod 755 /etc/letsencrypt/archive/Error 6: Conflicting server_name
nginx: [warn] conflicting server name "example.com" on 0.0.0.0:80, ignoredTwo server blocks have the same server_name. Check which files define the same domain:
grep -r "server_name example.com" /etc/nginx/Remove or merge the duplicate, then reload.
Error 7: include file not found
nginx: [emerg] open() "/etc/nginx/sites-enabled/mysite.conf" failed (2: No such file or directory)The symlink or file is missing:
# Recreate the symlink
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/mysite.conf
# Or check for broken symlinks
find /etc/nginx/sites-enabled/ -xtype lRemove broken symlinks:
sudo find /etc/nginx/sites-enabled/ -xtype l -deleteNginx won't reload without errors
Sometimes nginx -t passes but the reload fails silently. Check:
sudo systemctl status nginx
sudo journalctl -u nginx -n 30Force a full restart instead of reload:
sudo systemctl restart nginxUseful debugging commands
# Test config
sudo nginx -t
# Test with verbose output
sudo nginx -T | less
# Check which config file has a specific directive
sudo nginx -T | grep proxy_pass
# List all enabled sites
ls -la /etc/nginx/sites-enabled/
# View live error log
sudo tail -f /var/log/nginx/error.log
# View live access log
sudo tail -f /var/log/nginx/access.logCommon config file locations
| File/Directory | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main config |
/etc/nginx/sites-available/ | All virtual host configs |
/etc/nginx/sites-enabled/ | Active virtual hosts (symlinks) |
/etc/nginx/conf.d/ | Additional includes |
/var/log/nginx/error.log | Error log |
/var/log/nginx/access.log | Access log |