How to fix Nginx 502 Bad Gateway Error

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone this is tony teaches tech i'm tony and in this video i'm going to show you how to fix the oso infamous 502 bad gateway error on nginx now there is a few ways that this error could happen we're going to go through a couple of them in this video but if that's something we'll learn how to fix then let's go ahead and hop on into the tutorial here and as you can see we do have that 502 bad gateway error on our nginx server at this website here this ip address and the goal here is to fix it so let's go ahead and log into our nginx web server via ssh and as you can see here i went ahead and did that prior to starting this video and basically in a nutshell this error most of the time comes from nginx's inability to communicate with either the whatever terminology you want to use like the upstream server or the php fpm server or the fast cgi server so basically nginx is having trouble communicating with php now let's look at our uh nginx configuration file in order to to like determine what's actually going on here so that's usually at var no i'm sorry that's none of our etc nginx sites available and i only have one configuration file here called default and specifically what we want to look for is something to do with our php location block here and a lot of nginx configuration files look different so just what we're looking for is either fast cgi path something like this specifically this line right here for our cases so the the first thing i suggest that you do is to check that either this file exists and if it doesn't then we have to do something about that so let's look on our file system for run php php 7.2 fpm sock so let's go ahead and get out of here and do ls run php php and i'm doing the tab complete here so if we if we just list out this directory we'll see that we have php 7.0 installed on our system not 7.2 so there's a couple different ways we can fix this we can either install php version 7.2 or modify our nginx configuration file to point to the actual uh socket file that exists on our file system so let's do that that's the easiest option in this case so instead of nginx looking for php 7.0 let's have it look for the actual file that exists on our file system php 7.0-fpm dot sock and again this this could be different for whatever is on your file system versus mine so um because uh we made a change to our configuration file we should restart or reload nginx so let's do a system ctl restart nginx hit enter and we don't see any output no errors or anything like that so we should be good to go let's go ahead and refresh our page and we will see that that 502 bad gateway error has gone away now like i said there's a few different ways that these 502 bad gateway errors can happen that was one of them let's see another one here and it's kind of along the same lines as what we've just seen before what if the php fpm service in the background wasn't even running that could also cause a 502 bad gateway error so let's let's stop that service let's do system ctl php 7.0 dash fpm stop i'm sorry this this is a not system ccl this is service php let's see if it taps complete yep 7.0 fpm stop okay so that let us stop the service uh the php fpm service and if we refresh the website now nginx cannot can communicate with the php service so we get the 502 bad gateway error and obviously the simple fix in this case is to start that back up so we can do php service php 7.0.f fpm start hit enter and that should be running now and we can check this again refresh the page and we're back to a normal working website running on nginx so guys again this is not a comprehensive uh video but this is what some of the most common ways that you can see a 502 back gateway error with nginx so if you still have questions let me know in the comments below i'll try my best to help you out otherwise if you did get value out of this video give it a thumbs up if you want to see more videos like this from me in the future definitely consider subscribing and if you do i'll see you in the next one [Music] foreign
Info
Channel: Tony Teaches Tech
Views: 110,654
Rating: undefined out of 5
Keywords: 502 bad gateway nginx, nginx 502, 502 bad gateway nginx fix, 502 error nginx, 502 bad gateway nginx 1.14 0 ubuntu, 502 bad gateway nginx 1.13 10, 502 bad gateway nginx 1.17 5, how to fix 502 bad gateway nginx 1.4 7, nginx 502 bad gateway reverse proxy, nginx bad gateway
Id: hYVriHb43wU
Channel Id: undefined
Length: 5min 10sec (310 seconds)
Published: Wed Mar 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.