{"id":286,"date":"2018-01-08T20:28:17","date_gmt":"2018-01-08T20:28:17","guid":{"rendered":"http:\/\/cheapwindowsvps.com\/blog\/?p=286"},"modified":"2025-01-20T10:22:40","modified_gmt":"2025-01-20T10:22:40","slug":"how-to-troubleshoot-a-502-bad-gateway-error-on-nginx","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/how-to-troubleshoot-a-502-bad-gateway-error-on-nginx\/","title":{"rendered":"How to Troubleshoot a 502 Bad Gateway Error on Nginx"},"content":{"rendered":"<p>One of the most frustrating errors the sysadmin comes across when working with Nginx is the 502 error. The text accompanying this error is &#8216;Bad Gateway&#8217;, which, unfortunately, doesn&#8217;t say much about what might have gone wrong. In contrast, the other errors clearly say that the resource or page was not found, or that you don&#8217;t have permissions to view a particular resource.<\/p>\n<p>&nbsp;<\/p>\n<p>In this article, then, we&#8217;ll look at the 502 Bad Gateway error in Nginx in more detail, and will suggest some time-tested ways of troubleshooting it.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>What is the 502 Bad Gateway error?<\/strong><\/h2>\n<p><strong>\u00a0<\/strong><\/p>\n<p>When a server responds to an HTTP request, the class of response codes tells a lot. For instance, response like 4xx are to do with bad requests; similarly, the 5xx series is reserved for server errors. This means if you get a 500 or 502 response, it&#8217;s not your fault \u2013 something went wrong with the server.<\/p>\n<p>&nbsp;<\/p>\n<p>In the 502 error, what the server is trying to tell you is that it failed to connect with an important service through a connecting &#8216;gateway.&#8217; If this sounds too generic to be helpful, that&#8217;s just the nature of the beast; depending on what stack you&#8217;re running and how Nginx is set up serve requests, this error would mean different things.<\/p>\n<p>&nbsp;<\/p>\n<p>In the most common case, for example when your new WordPress installation throws this error, it means that Nginx wasn&#8217;t able to communicate properly with the PHP (or PHP-FPM) service. Next, let&#8217;s look at an example of how we would resolve this error in PHP.<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Resolving the 502 Bad Gateway Error in Nginx and PHP<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>There are several steps you need to systematically check for resolving the 502 Bad Gateway error.<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>PHP-FPM isn&#8217;t present<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p>In the first scenario, perhaps the config file you have provided to Nginx says that it must connect through PHP-FPM, while that service is actually not installed on your server. This is very common with new server installs.<\/p>\n<p>&nbsp;<\/p>\n<p>To resolve this, all you need to do is (assuming you&#8217;re on Ubuntu 16.04):<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<p>$ sudo apt install php-fpm<\/p>\n<p>$ sudo service php-fpm restart<\/p>\n<p>$ sudo service nginx restart<\/p>\n<\/blockquote>\n<p>After this, try accessing the URL again, and most likely, the error will be gone.<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>PHP-FPM isn&#8217;t able to connect to port or socket<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p>Another typical case is when you have the Nginx config file asking it to listen on a socket, whereas in reality that socket doesn&#8217;t even exist. Another possibility is that the config file tells Nginx to connect over port 9000 (an extremely common setting) whereas the FPM process is configured to send output through a socket file.<\/p>\n<p>&nbsp;<\/p>\n<p>To see if there&#8217;s this mismatch, open the PHP-FPM configuration file. For PHP 7.1 on Ubuntu 16.04, the location is \/etc\/php\/7.1\/fpm\/pool.d\/www.conf and look for the following line:<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<p>listen = \/run\/php\/php7.1-fpm.sock<\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>Now, check if the file \/run\/php\/php7.1-fpm.sock actually exists. If it doesn&#8217;t, simply create it and set the relevant permissions:<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<p># touch \/run\/php\/php7.1-fpm.sock<\/p>\n<p># chown :www-data \/run\/php\/php7.1-fpm.sock<\/p>\n<p># chmod 775 \/run\/php\/php7.1-fpm.sock<\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>Once this is done, try restarting FPM and Nginx, and things should work.<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>APC not working<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p>Sometimes the Alternative PHP Cache (APC) is a really bad idea and is best avoided. It&#8217;s often the cause of 502 Bad Gateway errors. If you&#8217;re running APC, please disable it and replace with more robust caching schemes (Memcache is a good alternative).<\/p>\n<p>&nbsp;<\/p>\n<p>Depending on your environment, you&#8217;d need to do similar detective work to see what has gone wrong with Nginx connection. The best place to start is always the Nginx error logs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most frustrating errors the sysadmin comes across when working with Nginx is the 502 error. The text accompanying this error is &#8216;Bad Gateway&#8217;, which, unfortunately, doesn&#8217;t say much about what might have gone wrong. In contrast, the other errors clearly say that the resource or page was not found, or that you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":3,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}