0
0
Nginxdevops~5 mins

Common error diagnosis in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your nginx web server does not work as expected. Common errors can stop your website from loading or cause wrong content to show. Diagnosing these errors helps you fix problems quickly and keep your site running smoothly.
When your website shows a 502 Bad Gateway error after nginx restart
When nginx fails to start due to configuration mistakes
When your site loads slowly or not at all and you want to check logs
When you want to verify if nginx is running and listening on the right port
When you want to test if your nginx configuration file has syntax errors
Commands
This command tests the nginx configuration file for syntax errors without starting the server. It helps catch mistakes before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command shows if the nginx service is running or stopped. It also displays recent logs to help identify startup errors.
Terminal
sudo systemctl status nginx
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-07 10:00:00 UTC; 5min ago Main PID: 1234 (nginx) Tasks: 3 (limit: 4915) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1235 nginx: worker process
This command shows detailed nginx logs from the last 10 minutes. It helps find specific error messages or warnings.
Terminal
sudo journalctl -u nginx --since "10 minutes ago"
Expected OutputExpected
Jun 07 10:00:00 hostname systemd[1]: Starting A high performance web server and a reverse proxy server... Jun 07 10:00:00 hostname nginx[1234]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Jun 07 10:00:00 hostname systemd[1]: nginx.service: Failed with result 'exit-code'. Jun 07 10:00:00 hostname systemd[1]: Failed to start A high performance web server and a reverse proxy server.
--since - Shows logs starting from a specific time
This command checks if any process is already using port 80, which nginx needs to listen on. It helps diagnose port conflicts.
Terminal
sudo netstat -tulnp | grep :80
Expected OutputExpected
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5678/apache2
-tulnp - Shows TCP/UDP ports with process IDs and names
Key Concept

If you remember nothing else from this pattern, remember: always test your nginx config syntax and check service status and logs to find errors quickly.

Common Mistakes
Not running 'nginx -t' before restarting nginx
This causes nginx to fail starting if there are syntax errors, leading to downtime.
Always run 'sudo nginx -t' to verify configuration before restarting.
Ignoring service status and logs when nginx fails
Without checking logs, you miss important error messages that explain the problem.
Use 'sudo systemctl status nginx' and 'journalctl' to read error details.
Not checking if port 80 is already in use
If another process uses port 80, nginx cannot start and will fail with a bind error.
Run 'sudo netstat -tulnp | grep :80' to find and stop conflicting services.
Summary
Use 'sudo nginx -t' to check configuration syntax before restarting nginx.
Check nginx service status with 'sudo systemctl status nginx' to see if it is running.
Read recent nginx logs using 'sudo journalctl -u nginx' to find error messages.
Verify port 80 is free with 'sudo netstat -tulnp | grep :80' to avoid conflicts.