{"id":1988,"date":"2026-10-04T23:25:16","date_gmt":"2026-10-04T23:25:16","guid":{"rendered":"https:\/\/pro.osassistance.com\/?p=1988"},"modified":"2026-10-05T00:04:45","modified_gmt":"2026-10-05T00:04:45","slug":"nginx-502-bad-gateway-fix-diagnose","status":"publish","type":"post","link":"https:\/\/osassistance.com\/blog\/nginx-502-bad-gateway-fix-diagnose\/","title":{"rendered":"Nginx 502 Bad Gateway Fix: 6 Proven Steps for Safe Recovery"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">An <strong>nginx 502 bad gateway fix<\/strong> starts with the upstream error in Nginx\u2019s log. A 502 means Nginx could not get a valid response from the application server it contacted. For a PHP site, that upstream is often PHP-FPM, but a dead service, mismatched socket, exhausted workers, crashed application, or slow dependency can produce similar symptoms.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Identify the failure before changing production settings. A restart may temporarily restore service, but it can interrupt requests and erase useful evidence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Need help with an active outage?<\/strong> OS Assistance can investigate Linux, Nginx, PHP-FPM, and control-panel incidents. <a href=\"https:\/\/osassistance.com\/contact\/?service=Emergency+Linux+support#quote-form\">Request emergency Linux support<\/a>. Response timing and scope are confirmed when the request is reviewed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_a_502_Bad_Gateway_error_means\"><\/span>What a 502 Bad Gateway error means<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx acts as a reverse proxy: it accepts a visitor\u2019s request and passes it to an upstream service such as PHP-FPM. If that service refuses the connection, closes it unexpectedly, or returns a response Nginx cannot use, Nginx may return <code>502 Bad Gateway<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A slow upstream may instead produce <code>504 Gateway Timeout<\/code>. The browser status and matching Nginx log line matter; do not treat every 502 or 504 as a PHP-FPM crash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Read_the_Nginx_error_log_first\"><\/span>1. Read the Nginx error log first<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the log while reproducing the affected URL. The location varies by distribution and hosting panel; <code>\/var\/log\/nginx\/error.log<\/code> is common on many Linux systems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo tail -n 50 \/var\/log\/nginx\/error.log\nsudo journalctl -u nginx --since \"15 minutes ago\" --no-pager<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Match the timestamp and upstream to one failed request. Common signatures point to different checks:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Log signature<\/th><th>Likely area to check<\/th><\/tr><\/thead><tbody><tr><td><code>connect() ... failed (111: Connection refused)<\/code><\/td><td>PHP-FPM is stopped, not listening on that address, or refusing connections.<\/td><\/tr><tr><td><code>connect() ... failed (2: No such file or directory)<\/code><\/td><td>The socket path is missing or does not match the PHP-FPM pool.<\/td><\/tr><tr><td><code>connect() ... failed (13: Permission denied)<\/code><\/td><td>Check socket ownership, mode, directory access, and SELinux or AppArmor denials.<\/td><\/tr><tr><td><code>upstream prematurely closed connection<\/code><\/td><td>The upstream may have crashed, been killed, or closed before sending a complete response.<\/td><\/tr><tr><td><code>upstream timed out<\/code><\/td><td>The application may be slow or blocked. Nginx commonly returns 504 for this; inspect the status and surrounding log lines.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Check_the_correct_PHP-FPM_service\"><\/span>2. Check the correct PHP-FPM service<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Service names vary by distribution, PHP version, and control panel. Find the installed unit before checking or changing it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl list-units --type=service --all | grep -E 'php.*fpm|plesk-php.*fpm'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then inspect the exact name shown by your server. Replace <code>php8.2-fpm<\/code> with the matching unit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status php8.2-fpm --no-pager\nsudo journalctl -u php8.2-fpm --since \"15 minutes ago\" --no-pager<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Look for failed starts, repeated crashes, out-of-memory messages, configuration errors, and pool warnings. Some systems write PHP-FPM logs to version-specific files instead of the system journal. Plesk can run separate services for different PHP versions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Match_Nginxs_upstream_to_the_PHP-FPM_listener\"><\/span>3. Match Nginx\u2019s upstream to the PHP-FPM listener<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For a Unix socket, the active Nginx server block\u2019s <code>fastcgi_pass<\/code> must match the PHP-FPM pool\u2019s <code>listen<\/code> value. A version mismatch is common after changing PHP versions or upgrading a control panel.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example Nginx directive\nfastcgi_pass unix:\/run\/php\/php8.2-fpm.sock;\n\n# Matching PHP-FPM pool directive\nlisten = \/run\/php\/php8.2-fpm.sock<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A TCP setup must match on both sides too, such as Nginx connecting to <code>127.0.0.1:9000<\/code> while the pool listens on that address and port. Keep PHP-FPM on a protected local or private network; never expose it to the public internet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a permission error, inspect the socket and parent directories using the actual path from your configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stat -c '%A %U:%G %n' \/run\/php\/php8.2-fpm.sock\nnamei -l \/run\/php\/php8.2-fpm.sock<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check <code>listen.owner<\/code>, <code>listen.group<\/code>, and <code>listen.mode<\/code> against the Nginx worker user and distribution defaults. Avoid <code>chmod 777<\/code>; broad permissions weaken security and may not fix the mismatch. If SELinux or AppArmor is enabled, inspect denial logs instead of disabling the control.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Diagnose_PHP-FPM_worker_exhaustion\"><\/span>4. Diagnose PHP-FPM worker exhaustion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When all PHP-FPM workers are busy, new requests wait in a queue. Slow PHP code, database queries, or external services can keep workers occupied until visitors see errors. A warning such as <code>server reached pm.max_children<\/code> shows that the pool reached its concurrency limit; it does not prove that raising the limit is safe.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Review pool settings, FPM logs, and process memory during representative peak traffic. This command gives a rough view of PHP-FPM processes and resident memory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ps -eo pid,rss,comm,args --sort=-rss | grep '&#91;p]hp-fpm' | head -20<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">RSS is only an estimate because processes can share memory. Prefer proportional set size (PSS) when available, and measure under realistic load. Leave capacity for the operating system, database, Nginx, monitoring agents, caches, and traffic bursts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A useful starting relationship is <strong>safe worker limit \u2248 memory budget for PHP workers \u00f7 measured memory per busy worker<\/strong>. There is no universal operating-system reserve or fixed worker size for every application. Raising <code>pm.max_children<\/code> without measuring can exhaust RAM and cause the kernel to kill PHP-FPM or the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check for slow requests and backend waits before increasing concurrency. PHP-FPM\u2019s <code>request_slowlog_timeout<\/code> and <code>slowlog<\/code> settings can help locate PHP paths that hold workers too long. If you enable <code>pm.status_path<\/code>, restrict it to localhost or trusted monitoring addresses; the status page reveals operational details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Treat_timeouts_and_FastCGI_buffers_as_targeted_settings\"><\/span>5. Treat timeouts and FastCGI buffers as targeted settings<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>fastcgi_read_timeout<\/code> sets the maximum interval between successive reads from PHP-FPM; it is not a blanket limit on total script runtime. If a log reports <code>upstream timed out<\/code>, first check PHP-FPM slow logs, database performance, external calls, and the request\u2019s expected duration. A higher timeout can keep workers occupied for longer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">FastCGI buffers handle response buffering, including unusually large response headers. Increase <code>fastcgi_buffer_size<\/code> or <code>fastcgi_buffers<\/code> only when a log or measured response shows a buffer problem. Larger buffers can use more memory across concurrent requests, so avoid copying a generic configuration into every site.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Apply_and_verify_one_change_at_a_time\"><\/span>6. Apply and verify one change at a time<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Save the relevant configuration and capture current error and service logs.<\/li>\n\n\n\n<li>Make one evidence-based change in the correct Nginx server block or PHP-FPM pool.<\/li>\n\n\n\n<li>Test the Nginx configuration before reloading it.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nginx -t<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the test succeeds, an Nginx reload applies the configuration while old workers can finish existing requests. On systemd systems, the command is commonly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl reload nginx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm the reload succeeded, request the affected page, and watch both logs. Do not reload after a failed configuration test. PHP-FPM reload behavior varies by service unit, and a restart can interrupt in-flight requests. Check supported actions and use a maintenance window when the risk is unclear. Repeated restarts can hide the cause while the outage returns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prevent_the_next_Nginx_502\"><\/span>Prevent the next Nginx 502<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Alert on PHP-FPM queue length, active and idle workers, and \u201cmax children reached\u201d events.<\/li>\n\n\n\n<li>Track PHP worker and database memory, swap activity, and kernel out-of-memory messages.<\/li>\n\n\n\n<li>Protect the PHP-FPM status page and enable a slow log where appropriate.<\/li>\n\n\n\n<li>Test PHP version changes and control-panel upgrades against the active socket or TCP listener.<\/li>\n\n\n\n<li>Investigate slow application and database paths before raising worker limits or timeouts.<\/li>\n\n\n\n<li>Keep disk space available for logs and temporary files. See our <a href=\"https:\/\/osassistance.com\/blog\/blog\/linux-disk-space-100\/\">Linux disk space recovery guide<\/a> if a full filesystem is part of the incident.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Frequently_asked_questions\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Will_restarting_PHP-FPM_fix_an_Nginx_502\"><\/span>Will restarting PHP-FPM fix an Nginx 502?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"461\" src=\"https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_troubleshooting-1-edited-1024x461.png\" alt=\"Nginx 502 Bad Gateway\" class=\"wp-image-2007\" srcset=\"https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_troubleshooting-1-edited-1024x461.png 1024w, https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_troubleshooting-1-edited-300x135.png 300w, https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_troubleshooting-1-edited-768x345.png 768w, https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_troubleshooting-1-edited-1536x691.png 1536w, https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_troubleshooting-1-edited.png 1721w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">It may restore service if PHP-FPM is stuck or stopped, but it does not explain the cause. Capture logs first when possible, confirm the exact unit and pool, and expect that a restart can interrupt active requests.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Does_a_502_always_mean_PHP-FPM_is_down\"><\/span>Does a 502 always mean PHP-FPM is down?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. A wrong socket, permission issue, worker exhaustion, application crash, or another upstream service can also produce a 502. Use the error log to identify which upstream failed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Should_I_set_pmmax_children_to_200\"><\/span>Should I set <code>pm.max_children<\/code> to 200?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only if measured worker memory and available RAM support that concurrency under real load. An arbitrary high limit can exhaust memory and trigger kernel out-of-memory kills.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Should_I_increase_fastcgi_read_timeout\"><\/span>Should I increase <code>fastcgi_read_timeout<\/code>?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only when the application has a justified long-running request and the log confirms a read timeout. Check slow PHP code, database locks, and unavailable dependencies first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Can_I_change_the_socket_to_mode_777\"><\/span>Can I change the socket to mode 777?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Check the configured socket path and the pool\u2019s owner, group, and mode. Broad permissions weaken security and may not solve the underlying mismatch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Which_Linux_distributions_and_PHP_versions_does_this_apply_to\"><\/span>Which Linux distributions and PHP versions does this apply to?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The diagnostic approach applies to common systemd-based Linux servers running Nginx and PHP-FPM. Service names, socket paths, log locations, and control-panel behavior vary, so verify them on the affected server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_can_I_get_ongoing_help_with_recurring_Nginx_and_PHP-FPM_problems\"><\/span>How can I get ongoing help with recurring Nginx and PHP-FPM problems?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">OS Assistance\u2019s Linux Care plan is <strong>$4,000 per month<\/strong> for unlimited Linux servers, with <strong>one active workstream at a time<\/strong>. Requests are triaged and planned within 24 hours during service hours; delivery timing depends on scope, access, and external dependencies. <a href=\"https:\/\/osassistance.com\/contact\/\">Discuss whether Linux Care fits your environment<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Official_references\"><\/span>Official references<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_fastcgi_module.html\" rel=\"nofollow noopener\" target=\"_blank\">NGINX FastCGI module documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/install.fpm.configuration.php\" rel=\"nofollow noopener\" target=\"_blank\">PHP-FPM configuration reference<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/fpm.status.php\" rel=\"nofollow noopener\" target=\"_blank\">PHP-FPM status page documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/nginx.org\/en\/docs\/beginners_guide.html\" rel=\"nofollow noopener\" target=\"_blank\">NGINX beginner\u2019s guide, including configuration reloads<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>An nginx 502 bad gateway fix starts with the upstream error in Nginx\u2019s log. A 502 means Nginx could not get a valid response from the application server it contacted. For a PHP site, that upstream is often PHP-FPM, but a dead service, mismatched socket, exhausted workers, crashed application, or slow dependency can produce similar&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2005,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"taxonomy_info":{"category":[{"value":1,"label":"Uncategorized"}]},"featured_image_src_large":["https:\/\/osassistance.com\/blog\/wp-content\/uploads\/2026\/10\/OCA_blog-1-1024x576.png",1024,576,true],"author_info":{"display_name":"admin","author_link":"https:\/\/osassistance.com\/blog\/author\/wp_intcisweb\/"},"comment_info":2,"category_info":[{"term_id":1,"name":"Uncategorized","slug":"uncategorized","term_group":0,"term_taxonomy_id":1,"taxonomy":"category","description":"","parent":0,"count":7,"filter":"raw","cat_ID":1,"category_count":7,"category_description":"","cat_name":"Uncategorized","category_nicename":"uncategorized","category_parent":0}],"tag_info":false,"_links":{"self":[{"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/posts\/1988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/comments?post=1988"}],"version-history":[{"count":9,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions"}],"predecessor-version":[{"id":2010,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions\/2010"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/media\/2005"}],"wp:attachment":[{"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/media?parent=1988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/categories?post=1988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osassistance.com\/blog\/wp-json\/wp\/v2\/tags?post=1988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}