New Relic Now Demo new agentic integrations June 24.
Save your seat.

NGINX errors contribute to downtime, poor user experiences, and security vulnerabilities that can run rampant across your web server. It’s critical to not only understand NGINX errors, but to also effectively know how to troubleshoot them.

That’s why we created this succinct guide, which covers:

  • What is NGINX and how it’s used.
  • What causes NGINX errors.
  • How to check for NGINX errors.
  • How to fix NGINX errors.
  • Ways you can prevent future NGINX errors.

What is NGINX?

NGINX (pronounced "engine-x") is a popular and powerful piece of open-source software used in modern web infrastructure. Known for its speed, stability, and low resource consumption, the software is ideal for high-traffic websites and applications, though it has a variety of use cases.

  • Web server: As is its core functionality, an NGINX web server efficiently delivers web content—HTML, CSS, JavaScript, images, videos—to users over the internet.
  • Reverse proxy: NGINX can also act as an intermediary and gatekeeper, enhancing security and optimizing the flow of traffic between clients and backend servers.
  • Load balancer: For load balancing, NGINX distributes incoming network traffic across multiple servers, ensuring that no single server is overwhelmed and improving service availability.
  • HTTP cache: When functioning as an HTTP cache, NGINX stores frequently accessed web content to improve performance.

What causes NGINX errors?

NGINX errors are messages generated by the NGINX web server that indicate problems or issues encountered while processing requests. These errors are crucial for diagnosing and troubleshooting problems with your web server and the websites or applications it serves.

A few common NGINX errors include:

  • Configuration errors such as incorrect syntax in the nginx.conf file, errors in virtual host configurations, issues with location blocks or rewrite rules, or incorrect file paths.
  • Resource limitations like insufficient memory or disk space, exceeding the maximum number of open files, CPU overload, or too many connections.
  • Network issues such as DNS resolution failures, firewall restrictions, routing difficulties, physical network cable complications, or latency or packet loss.
  • Permission issues like overly restrictive read permissions for static files (HTML, CSS, images, etc.), incorrect read permissions for SSL/TLS certificate files, or missing write permissions for log directories.
  • Upstream/backend server errors such as 502 Bad Gateway, 503 Service Unavailable, and 504 Gateway Timeout.

How to check NGINX errors

Logs and status commands are complementary tools for identifying and diagnosing NGINX errors, providing crucial insights into the server's behavior and potential problems. Below are methods for using these tools.

Checking NGINX status

To check NGINX status, use the systemctl command, which shows you the current status of the NGINX service (active, inactive, failed, etc.).

sudo systemctl status nginx

To check if NGINX is running, use the ps aux | grep command, which filters and displays running processes that contain the specified string. If NGINX is running, you'll see processes listed in the output.

ps aux | grep nginx

To safely reload NGINX, use the sudo command in conjunction with the -s and reload signals. This reloads the NGINX configuration without interrupting existing connections. It's the safest way to apply changes.

sudo nginx -s reload

Checking NGINX error logs

Checking the NGINX error log involves a systematic hunt-and-analyze approach:

  1. Locate and examine NGINX error logs. NGINX logs errors to a file, typically error.log. Use command-line tools like tail, less, or cat to view the log file; tail -f /var/log/nginx/error.log is especially useful for real-time monitoring. Look for error messages, timestamps, and context information.
  2. Analyze HTTP status codes. When clients encounter errors, NGINX often returns HTTP status codes. Look for the server-side 5xx errors (502, 503, 504, etc.) and client-side 4xx errors (401, 403, 404, etc.) for clues.
  3. Correlate log entries with HTTP status codes. Match the timestamps of HTTP status code errors with corresponding entries in the NGINX error logs to help identify the underlying cause. For example, a 502 Bad Gateway error may coincide with "upstream connection refused" messages in the error log.
  4. Check NGINX configuration. Use nginx -t to test the NGINX configuration for syntax errors. Review the nginx.conf file and any included configuration files for incorrect directives or settings.

How to fix common NGINX errors

Chances are while working with NGINX you’ve asked at least one of these questions:

  • What does 404 not found mean with NGINX?
  • Why is NGINX giving me a 502 Bad Gateway error?
  • How do I fix an NGINX error?

That’s because you’ve likely come across one of the errors in this section. Luckily, we’ve got fixes in case you have.

404 not found error

Cause: The requested file or resource does not exist on the server.

Fix: Verify the requested URL for typos or incorrect paths.

500 internal server error

Cause: A generic error occurred on the server, often due to issues with backend applications.

Fix: Check the backend server logs for specific error messages.

502 bad gateway error

Cause: NGINX received an invalid response from a backend server.

Fix: Check logs and verify network connectivity between NGINX and the backend server.

503 service unavailable

Cause: The backend server is temporarily unable to handle requests, often due to overload.

Fix: Increase backend server resources (CPU, memory).

504 gateway timeout

Cause: NGINX did not receive a timely response from a backend server.

Fix: Increase the timeout values in NGINX's proxy_read_timeout and proxy_connect_timeout directives.

High CPU usage

Cause: NGINX is overloaded, often due to excessive traffic or inefficient configuration.

Fix: Optimize NGINX configuration (e.g., enable gzip compression, cache static files).

Preventing future NGINX errors

To keep your system running smoothly and mitigate NGINX errors, implement these best practices:

  • Test system configuration thoroughly. Always test your NGINX configuration for syntax errors before reloading or restarting. This prevents service disruptions caused by configuration errors that would otherwise bring down NGINX or cause unexpected behavior.
  • Regularly monitor and analyze logs. Regularly monitor NGINX error logs for warnings and errors. This allows early detection of potential problems, enabling proactive troubleshooting and preventing major outages.
  • Employ robust resource monitoring. Monitor system resources (CPU, memory, disk space) and set up alerts for resource thresholds. This mitigates NGINX from crashing or becoming unresponsive due to resource exhaustion.
  • Keep NGINX updated. Regularly update NGINX to the latest stable version and any installed modules, including security patches.
  • Perform backend server health checks. When using NGINX as a reverse proxy or load balancer, configure health checks for backend servers. This allows NGINX to detect and remove unhealthy servers from the pool.