Tips / Nginx

How to Fix 502 Bad Gateway Error Using Nginx

Many times we can browse the web and find a 502 bad gateway error at Nginx.   There are a few reasons why you will find this message in  your webserver log, and here we will teach you how to fix it.

How you might see the 502 Bad Gateway error

Different websites may customize the 502 bad gateway error message. Here are the most common 502 error messages:

  • “502 Bad Gateway”
  • “502 Bad Gateway NGINX”
  • “502 Service Temporarily Overloaded”
  • “502 Proxy Error”
  • “Error 502”
  • “HTTP Error 502 – Bad Gateway”
  • “HTTP 502”

When you will find 502 Bad Gateway error

  1. Nginx running as proxy for Apache web server.
  2. Nginx running with PHP-FPM daemon.
  3. Nginx running with other services as gateway.
  4. Bad buffering/timeout configuration.

Before getting deep into all of this options, you must understand what does the 502 Bad Gateway error message mean. So, the error is an HTTP Status Code error and it’s happening at the gateway. What the hell is a gateway? In simple words, a gateway, is like an access point, a bridge that communicate one service with another, in this case the gateway can be a service/application that is working and receiving requests from Nginx web server.

Now, let’s explore each one of this reasons to understand the 502 Bad Gateway error message

  1. Nginx as Proxy for Apache: in this case, the gateway is Apache. When you use Nginx as proxy for Apache, if apache dies or it’s not well configured, it can cause this 502 Bad Gateway error. How to fix it? Most of the times, restarting apache web server will get rid of this, but you must check the log files to know why exactly this was caused.
  2. Nginx with PHP-FPM: same as Apache example, let’s imagine your php daemon stop working, or get’s overloaded by requests, it will not work properly and the all the php requests from nginx would not be served, so, a 502 Bad Gateway error will appear. How to fix it? Restart php-fpm daemon and check the logs.
  3. Nginx with other services/apps: try restarting the other service behind nginx and explore the logs to find the reason why it happened.

Other quick solutions for 502 Bad Gateway error:

1) Increase buffer and timeouts inside http block:

http {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

2) Ensure your php-fpm service is listening according to what you’ve configured in nginx, it can be either this two options:

Edit www.conf file (in CentOS it is located at /etc/php-fpm.d/www.conf and try with one of this two options:

listen = /var/run/php5-fpm.sock


listen =

After that, just restart the php-fpm service.

3) Disable APC Cache if used, instead try Xcache, apc can cause this kind of issues under particular enviroments causing segmentation faults.

4)  I recently found another cause of 502 Bad Gateway error, check it out here: php5-fpm.sock failed (13: Permission denied) error

Additional Resources

Popular search terms:

  • 502 bad gateway nginx
  • 502 bad gateway
  • nginx bad gateway
  • nginx 502 bad gateway

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Pavel M.

    In regards to the issues between PHP-FPM and APC, what I found is that after a server reboot, PHP-FPM wouldn’t start any longer.

    I went and tried executing it manually from /usr/sbin/php-fpm <- this is where I saw there was an issue with APC, and after looking a bit online, I saw that by simply removing the "M" in /etc/php5/conf.d/apc.ini for the property: apc.shm_size=128 solved the issue.

    Hope this helps anyone.

    • I am pretty sure that this is a bad idea. In the php.ini file this is the rules The available options are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes; available since PHP 5.1.0), these are case insensitive. Anything else assumes bytes.. I am getting this from the php website here. It does say this only pertains to the php.ini file but I bet this changes the value completely. It may have been that your was set too high. Maybe setting it to `48M` or lower would have been a good idea.

  • Denise lenker

    How do i fix my iphone 4 that has the message 502 bad gateway nginx i have verizon

    • admin

      It’s not a problem on your iPhone, it’s a problem on Verizon’s network, they have to fix it.

  • Halil

    What the hell is a gateway?
    That was my first response when I saw the 502 error on the page. 🙂

    Thanks for helping out with this.

  • Chris

    What a brilliant site.
    I actually put into the location bar in Firefox for a google search: nginx “502 bad gateway” and instead of search results I got your page which fixed my problem in 5 min. I disabled apc and all my problems went away.
    Thank you.
    (How did you get google to treat you specially?)

    • aakash

      i have installed nginx inside docker container and i am getting 502 bad gateway on localhost plz help

  • Asri

    Hi, just to add another case based on my experience. When one change the ‘user’ and ‘group’ ini php.ini with other value. Then ‘502 Bad Gateway nginx’ is raised.

  • If you are using ubuntu then path of http://www.conf
    you can simple find path
    locate http://www.conf

  • Thanks…. now my site has been running again after follow this step by step

  • Rafal Poniatowski

    Hope this tip will save someone else’s life. In my case the problem was that I ran out of memory, but only slightly, was hard to think about it. Wasted 3hrs on that. I recommend running:

    sudo htop
    sudo free -m

    …along with running problematic requests on the server to see if your memory doesn’t run out. And if it does like in my case, you need to create swap file (unless you already have one).

    I created swap file on Ubuntu Server 14.04 and it worked just fine.

  • I am having problems of this kind with my unblocker website, I increased the buffers to 512k and increase the timeouts even more.
    Thanks for the tips, I hope this will get my problem solved.

    I was also thinking to create a cronjob to check if the php5-fpm or the website is running, if not to restart it. (somehow i think the buffers timeouts)

  • thanks a lot for the article.
    please correct the following line “recieving requests from Nginx web server”

  • Rui Monteiro

    I have a script witch i use :

    echo ‘********* kill instance ******************’

    sudo kill -HUP `cat /tmp/`
    sudo uwsgi –reload /tmp/
    sudo touch /tmp/server.sock

    sudo uwsgi –emperor /etc/uwsgi/vassals/ –master –thunder-lock –enable-threads

    Then i use another script on another terminal :

    sudo service uwsgi restart
    sudo service nginx restart

    This two scripts resolve me the 502 gateway problem.

  • Avi

    > 1) Increase buffer and timeouts inside http block:

    Where is the “http block” ?

    I can’t find it in any standard config files.


    • Teition Solutions

      http block is inside the /etc/nginx/nginx.conf file, most likely. If it’s not in there, it’ll be in your sites-available virtual domain blocks.

  • Lex

    Please add this to the list: LOW MEMORY

    It will help a few people. I just had the same problem and after issuing some commands in the shell I found nodemon was using a whole lot more memory than node used to. Switched back to node and 502 went away.

    To check memory on linux, these two commands will help you out:
    free -m

    ps aux –sort -rss

    The second one will show you the per process memory consumption on the RSS column (odd, I know). It may not be the issue you’re having, but when you’re out of options it’s worth a try.

  • pdisme

    Does anyone know how to get nginx to log occurrences of this error? I have customers report seeing it from time to time, and I’m fairly sure it’s occurring at times when apache is being restarted for things like SSL cert installation, but I need the exact times to confirm that.

    • Gweltaz

      It doesn’t appear in /var/log/nginx/error.log ?

  • Johan

    Make sure that you’re using the right PHP version as well! Many users have problem because they’ve simply copied the nginx config from somewhere and it’s asking for php5-fpm and not php7.0-fpm. I’ve found this to be a common error everytime there’s a major php ugrade 🙂