X
X

Select Your Currency

₺ Türk Lirası $ US Dollar
X
X

Select Your Currency

₺ Türk Lirası $ US Dollar

Fixing 502 and 504 Errors with Nginx Settings

HomepageArticlesTechnical GuidesFixing 502 and 504 Errors with Ngin...

Nginx settings that appear to be causing 502 and 504 errors should not be addressed by randomly restarting the web server or increasing every timeout value. First, examine communication between Nginx and the backend application. A 502 generally indicates an invalid backend response or a response that could not be obtained, while a 504 indicates that the response did not arrive within the expected time. Choose the appropriate correction according to the concrete evidence in the error log.

First define the access scope and the boundaries of the error

This process is suitable for a server administrator who can access the Nginx configuration and error logs. If site-specific Nginx settings, proxy rules, or logs are not visible in a shared hosting account, do not try to change the global configuration; use the application logs and error records provided by the hosting provider through the control panel. If you do not have the necessary permissions, contacting the provider with the error time, affected URL, and error code is the safest approach.

First determine whether the error occurs on every request, only for logged-in users, or only during a long-running operation. If a static file opens without a problem but a URL routed to PHP, Node.js, or another backend application returns 502, inspect the upstream connection between Nginx and the application. If 504 occurs only while generating a long report, the connection may be established; in that case, evaluate the operation time, application log, and proxy timeout together.

What evidence do Nginx settings provide for 502 and 504 errors?

Nginx’s error log is more informative than the code displayed to the client. If the record contains wording such as connection refused, the application may not be listening on the port to which Nginx is trying to connect. The phrase upstream timed out suggests that the connection or response was not completed in time; however, the cause may be a slow database query, an application lockup, or a genuinely short proxy timeout. Records such as invalid header, on the other hand, indicate that the backend produced an HTTP-invalid response.

Make the decision after this distinction: if the connection is refused, first check whether the application service, port, or Unix socket and the Nginx upstream definition refer to the same target; if there is only a timeout, do not increase the timeout before measuring how long the application takes to complete the same request. If the backend never completes the request, giving Nginx more time to wait may hide the underlying problem.

Safe investigation and correction steps

  1. Record the error time. Note the affected URL, HTTP code, request time if available, and the application operation running at the same time. This prevents older log lines from being confused with new findings. If you can reproduce the same error in a controlled way, document the current state first.
  2. Locate the relevant Nginx error log. In the control panel, open the error logs belonging to the domain or virtual server; on a managed server, use the error_log destination defined in Nginx’s configuration. If the record identifies a specific upstream address and says connection refused, do not immediately increase an Nginx setting. Instead, check whether the application is actually listening on that port and whether the address in the upstream definition is current.
  3. Compare the backend application with the corresponding setting. Review the application’s status in its service manager, the application logs, and the proxy_pass or similar upstream target in Nginx together. If the application is listening on another port and Nginx is routing to the old port, change only the relevant upstream target to the correct value. If the application has stopped, first investigate why it stopped and review the application’s own safe startup procedure; do not treat a random service restart as the solution.
  4. Test the configuration before loading it. On a Linux server where you have administrator access, save a copy of the original file or panel setting before making the configuration change. Run Nginx’s configuration test tool, generally nginx -t, to check the syntax and file references. A successful test shows that the configuration is valid; by itself, it does not prove that the backend application is running or that the request will succeed. If the test fails, do not activate the change and correct the faulty line.
  5. Repeat the same request and inspect the new evidence. After the change, open the same URL that failed earlier, using the same operating conditions if possible. The timestamp in the new error log must belong to the new attempt. If the connection refused record disappears and an application response arrives, the connection target has been corrected. If the new record says upstream timed out, accept that the initial problem has been resolved but that the application’s response time or another layer still requires investigation.

If the error continues after the change, make sure that you changed only one factor. If the application log shows a database, memory, or processing-time problem during the same period, address the application layer instead of increasing the Nginx timeout. If the problem occurs only during a legitimately long-running operation that the application completes successfully, and the logs show that the connection was established, you may consider carefully increasing the relevant proxy_read_timeout value. Keep the original value, make a small change, and repeat the same request again.

How should a hypothetical finding be interpreted?

For example, suppose a report URL returns 504 and the Nginx log contains an upstream timed out while reading response record matching the request time. If the application log shows that the report query is still running, it is more appropriate to investigate why the query is taking so long first; increasing the Nginx timeout only extends the waiting period. If the application completes the report within its normal time but Nginx’s read-time limit is clearly shorter, record the original value and gradually increase only the relevant proxy read timeout. On the new attempt, you should see a completed response instead of 504 and a matching new log entry.

By contrast, in the same example, if the application log shows no new request and the Nginx record says connection refused, working on the timeout setting is the wrong direction. Compare the application’s port, socket path, or service status with the upstream definition. If the application begins to register the request after the correction, the connection layer has been repaired; if a new error occurs this time, evaluate it as a separate finding. If the problem continues, roll back the temporary change and ask the service provider or application administrator to investigate the backend status.

Checks before and after making a change

Safe work with Nginx settings depends on not increasing multiple limits at the same time. Record the original upstream address, timeout value, and time of the change; if the configuration test fails, do not activate it. A successful response alone is not enough: examine the new attempt for the same URL, the new timestamp in the Nginx error log, and the corresponding entry in the application log together.

If you use shared hosting, you can prepare the logs available to you and the error times, then review the relevant service and management options through İyibirNet. If you have administrator access, the next safe step is to change one setting, run the configuration test, and observe the failed request again under the same conditions.


Top