
How To Troubleshoot Common Nginx Errors
Common commands to troubleshoot nginx:
sudo cat /var/log/nginx/error.log
: This is used to print a log with a list of errors and details about them.sudo nginx -t
: This is used to check for syntax error in configuration.systemctl status nginx
: This is used to check if the Nginx service is active or inactive.
Troubleshooting with the Error Log
To get a full overview of the nginx errors happening, run:
sudo cat /var/log/nginx/error.log
Above command results in error log. Example:
2022/11/28 23:58:22[emerg] 168641#168641: invalid hostin"[::]443" of the"listen" directivein /etc/nginx/sites-enabled/test.do-community.com:122022/11/28 23:59:44[emerg] 168664#168664: invalid number of argumentsin"root" directivein /etc/nginx/sites-enabled/test.do-community.com:42022/11/29 00:00:19[emerg] 168701#168701:"server" directive is not allowed herein /etc/nginx/sites-enabled/test.do-community.com:62024/08/03 10:41:37[notice] 28504#28504: using inherited sockets from"6;7;"2024/08/03 10:52:29[notice] 31898#31898: signal process started2024/08/03 11:02:04[error] 31899#31899:*5 open()"/var/www/tech.local/foss" failed(2: No such file or directory), client: 127.0.0.1, server: tech.local, request:"GET /foss HTTP/1.1", host:"localhost", referrer:"http://localhost/"2024/08/03 11:02:45[error] 31899#31899:*5 open()"/var/www/tech.local/foss" failed(2: No such file or directory), client: 127.0.0.1, server: tech.local, request:"GET /foss HTTP/1.1", host:"localhost", referrer:"http://localhost/"2024/08/03 11:05:54[error] 31899#31899:*7 open()"/var/www/tech.local/foss" failed(2: No such file or directory), client: 127.0.0.1, server: tech.local, request:"GET /foss HTTP/1.1", host:"localhost", referrer:"http://localhost/"2024/08/03 11:28:19[notice] 43309#43309: signal process started2024/08/03 11:28:30[crit] 43310#43310:*31 connect() to unix:/this/is/error failed(2: No such file or directory)whileconnecting to upstream, client: 127.0.0.1, server: tech.local, request:"GET /500error HTTP/1.1", upstream:"fastcgi://unix:/this/is/error:", host:"localhost"2024/08/03 11:37:53[crit] 43310#43310:*37 connect() to unix:/this/is/error failed(2: No such file or directory)whileconnecting to upstream, client: 127.0.0.1, server: tech.local, request:"GET /500error HTTP/1.1", upstream:"fastcgi://unix:/this/is/error:", host:"localhost"
The log provideskey information about the specific error.
The error message is divided into:
- first part: date and time
- second part [...]: provides types of error message, e.g: notice, crit, error, emerg(emergency) etc.
- last part: error message itself (with what file and specific line can be found)
Checking for Syntax Errors
Most common errors nginx has to do with syntax in configuration error. To check syntax error, run:
sudonginx-t
The output if everything is ok:
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conftestis successful
If there is syntax errors, example of log:
[emerg] invalid hostin"[::]443" of the"listen" directivein /etc/nginx/sites-enabled/test.do-community.com:12nginx: configuration file /etc/nginx/nginx.conftestfailed
After verifying syntax is correct always reload nginx. Command:
sudosystemctl reload nginx# ORsudonginx-s reload
Troubleshooting withsystemctl status nginx
Always verify if nginx service is active and working on your system. Usesystemd
init system:
systemctl status nginx
If your service is running, it will read asactive (running)
in output.
If your service is not running, it will read asinactive (dead)
in the output. If that happens restart Nginx service:
sudosystemctl restart nginx
Verify if it has restarted with:
systemctl status nginx
References
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse