How I fixed Nginx 502 Bad Gateway error

Today to my grate surprise I got “502 Bad Gateway” error while trying to open some specific URL on my web site:

Nginx 502 Bad Gateway

I took a look at the Nginx log file located in /var/log/nginx/ directory on my machine and seen the following:

2012/09/17 16:48:47 [error] 15421#0: *16 upstream sent too big header while reading response header from upstream, client: 192.168.1.5, server: milomag.ru, request: “GET /store/rastitelnie-extrakty HTTP/1.1”, upstream: “fastcgi://unix:/var/run/www-beauty.sock:”, host: “milomag.ru”, referrer: “…”

After doing a simple Google search I solved the problem by adding two lines to Nginx configuration file /etc/nginx/nginx.conf :

 
http {
    ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    ...
}

I am not sure that it is absolutely necessary, but each time I install Nginx I add this to /etc/nginx/nginx.conf, at least in versions 1.1.19 on Ubuntu 12.04 and 1.10.3 on Ubuntu 16.04 this works fine:

 
http {
        #My custom settings

        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;

        client_max_body_size 24M;
        client_body_buffer_size 128k;
        ...
}

Without this settings I got ‘502 Bad Gateway error’ while trying to list products or orders in Joomla 1.5/VirtueMart administrative panel.

65 Responses to How I fixed Nginx 502 Bad Gateway error

  1. M1 says:

    thanks for this. had the same prob. fixed my prob also.

  2. bill says:

    solved the problem

  3. andrej says:

    This also solved the problem Nginx 502 Bad Gateway error on my site

  4. 0108SR says:

    Ohh.. how nice. But it is too technical for me. Can u please let me have a step by step guidance? I have Internet Explorer 7 and use Windows Vista Home Edition.

    Thnx in advance

    1. dmitriano says:

      Hello! You need Ubuntu Linux, not Windows 🙂 Seriously, the “502 Bad Gateway” error happens on the web server, so it doesn’t matter what OS you’re running and what browser you’re using.

  5. boem says:

    This magically solved my problem. I thought it was about joomla internal rewrite engine conflicting with nginx configuration and spent hours trying to figure out the solution from that angle.

  6. gijs says:

    lol, so if i see this when visiting a website (wich isn’t mine) this website just totally crashed right .___.

    1. dmitriano says:

      I’m not sure about “totally crashed”, but I can say with certainty that it does not work at the moment 🙂

  7. Luckyfire says:

    Works great! realy helps. Thank You!

  8. Davor says:

    doesn´t solved my problem… but thx for help

  9. Paul says:

    Did not solve my 502 error either, so there must be a couple of solutions. This is obviously one of them though. Thanks for posting a solution – even if it didnt solve mine !

  10. Francisco says:

    Good morning,

    I am also having this message when I try to log to my Warez-bb account:

    “502 BAD gateway – NGINX”

    I want to try your solution but I am unable to finde the “path” /etc/nginx/nginx.conf

    How do I access NGINX configruation page? I’m running W7 64bit.

    Thanks!

    Regards

    1. dmitriano says:

      Hello! I suppose something temporarily wrong with the web site you are trying to open.

    2. Noxxim says:

      You can’t fix this on your (Browser) side. It’s a Server issue.

  11. Paul Thomson says:

    Seems to have done the trick,

    Keep up the good work, stay safe!
    Paul 🙂

  12. John says:

    It got rid of the problem… Thanks!

  13. Sepp says:

    This also solved the problem Nginx 502 Bad Gateway error on my Magento site.
    Thank You!

  14. Claude says:

    Is there a reason why this has started happening recently on various sites where it never happened previously?

    Is there anything I can do about it as a users of the sites?

    1. dmitriano says:

      Hello, Claude! One of the possible reasons is a software update or some changes in Nginx configuration. If you have the access to Nginx configuration files there are a lot of things you can do.

  15. WIlson says:

    Fixed my problem also! Tanks and success!

  16. Nyein Nyein says:

    I faced “502 Bad Gateway : ngix” error at my site. To solve this problem, I used this “/etc/nginx/nginx.conf” at command. But When I entered it, it appear this error “blush : access denied for /etc/nginx/nginx.conf”.
    If so , I can’t continue to run your following code
    http {

    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;

    }
    How can I fixed this error. Please can you give your advice.

    1. superadmin says:

      Probably you need the root access to “/etc/nginx/nginx.conf”, try sudo -s or something like this.

  17. Tezel Celebi says:

    hi dmitriano,

    thanks for the simple and great solution. it worked !!

  18. zodiac signs says:

    Thank you
    By reading your articles I’ve solved my own

  19. Rajavamsidhar chowdary says:

    Fixed my problem!! Thanks a lot

  20. Fixed the problem, but next to how much should i increase the value?

  21. Grateful Developer says:

    Fixed my problem too – thanks!

  22. Dealsoff says:

    This fixed my 502 gateway problem. Thanks i found this website.

  23. Michel says:

    I’m having this problem on my iPhone 4S. Any solution?

  24. Ali says:

    I had the same problem and There was no directory or config file as stated above, What worked for me was:
    going to command com, cmd.exe and doing DNS flush:

    How to clear dns cache on Windows XP, 2000, Vista or Windows7 OS?
    Open the Windows Start menu.
    Go to Run.
    If you do not see the Run command in Vista, search for “run” in the Search bar.
    In the Run text box, type: ipconfig /flushdns
    Press Enter or Return, and dns cache of Windows OS will be flushed.

    How to clear dns cache on MacOS?
    Go to Applications.
    Go to Utilities.
    Open the Terminal application.
    Type: dscacheutil -flushcache
    Press Enter or Return, and your MacOS DNS cache will be cleared.

  25. Tunive says:

    Thanks, i fixed that!!!!

  26. Szabi says:

    Thanks a lot. Fixed my problem.

  27. Vanessa says:

    Ali, that did not work.

  28. prem726 says:

    I solved this problem by click on “rebuild” in category menu.
    it’s work in my joomla 3.2 website
    http://www.happykorat.com

  29. Fixed the problem. Thank you very much

  30. Asysteo says:

    Hey guys! Today something wierdo happend to my site. i got error like this Bad Gateway or something http://www.asysteo.pl any idea what could happend?
    i recently updated php version so maybe thats a problem?

  31. Ben Mackay says:

    Thanks, this solved my problem too. No way I would have fixed this on my own.

  32. pirates says:

    I want to kiss you! Thank you so much! 😀

  33. Narasimha Mudiraj says:

    still facing same issue when i was added above code

  34. Narasimha Mudiraj says:

    Please help me on this issue

    1. superadmin says:

      Take a look at Nginx log files. Is there something suspicious?

  35. Samurai says:

    Cant fix it. Help. Im using safari browser on iphone

  36. Fuxy says:

    Thx. This turned out to be the issue on my server as well.

  37. T says:

    Fixed. had the same issue. Thank you!

  38. harshada says:

    Thank you so much this really worked

  39. Octávio says:

    Thank you, thank you, thank you!
    2 years and finally solved.

  40. Tony says:

    A wensite I visit seems to habe this problem. I might try to contact them and link them to this.

    Alot of other websites I surf have this problem, too. Tney all seem to be using nginx because the error page indicates that. Also, the errors seem to last one minute or so then correct themselves, so they may have not seen it in action.

    So in cases like these, the only solution available client-side is to drop an email to the site’s webmaster, as they are the only ones that can fix things on their side. Annoying, but it may help the owner of your favorite site improve their reliability.

  41. Steven says:

    Still gold!

  42. Franz says:

    Slightly different problem, but I’ll leave here the solution for future reference:
    Error 502 on magento customer account creation.
    Nginx error reporting a 104 php gateway error
    /var/log/php-fpm.log reports segmentation fault.
    Solution: restart php-fpm with ‘sudo service php-fpm restart’

  43. Foto bugil says:

    i had similiar issue, this tutorial solve few 502 problem but im still get 502 error every time saving in wordpress menu settings

  44. rekha says:

    502 Bad Gateway
    nginx/1.4.6 (Ubuntu)

    Getting error

  45. Avishai says:

    That did the trick for me too. Thanks!

  46. Claudia says:

    Hello!
    I was trying to open the COUCHSURFING site (www.couchsurfing.com) in my laptop and tables, and I got this message:

    502 Bad Gateway
    nginx/1.10.3 (Ubuntu)

    It seems that the way you solved your problem has helped many others. The problem is that I have limited knowledge regarding How, Where or What to type to solve it (you mentioned adding couple of lines) or configurating (or re-configurating) that error message.

    … would you mind helping me in a more detailed way?

    Not sure if I had to fill out in the requested DATA if you needed the website where I found the 502 Bad Gateway (I don’t have a website myself).

    Thanks!

    1. dmitriano says:

      Hello!
      It looks like a temporary misconfiguration at the server side, but not on your laptop. Wait a while and try again.

  47. david says:

    solved my problem within minutes

    brilliant!!

    i have no idea why this problem even occurred!

  48. Kevingreek says:

    Solved my problem at Magento 2.1.6 – varnish – nginx – redis & ubuntu 16.04 LTS! The error was happening only when at the front end i was trying to show all the products form a specific category, with the magento_URL+ ?product_list_limit=all while quantity of all these products were more than 128.

    Thanks for this fix!!

  49. I solved the issue by implementing Varnish caching. It hugely reduced loads and overcame the bottlenecks I was experiencing.

  50. shailesh Jaiswar says:

    i’m getting 502 Bad Gateway nginx/1.10.3 (Ubuntu)please give some solution for that

  51. I’m having the problem where I get 502 Bad Gateway – nginx/1.15.6.

    I’m using Windows 10. I have no problem using Google Chrome. Internet Explorer 11 has the following issue:

    When I first open the page I get the “502 Bad Gateway” error, however; if I reload the page with CTRL+F5 it loads fine. If I reload the page with just F5 I get the same error again. I can continue going back and forth with CTRL+F5 and F5 and it keeps going back and forth from working to “502 Bad Gateway” to working to “502 Bad Gateway”…

  52. I fixed my issue with the “502 Bad Gateway” error:

    1. I logged into “homestead ssh” and changed to myself to the root user “sudo -s”.

    2. I modified the file: /etc/nginx/sites-enabled/homestead.test (my first Laravel web page) by changing the line that read:
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    to read:
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    (My version of php is 7.2 not 7.0)

    3. Ran: sudo service nginx restart

    4. Ran: sudo service php7.2-fpm restart

    Fixed !!!

  53. Astro says:

    Man! You saved my bacon, thx!

  54. Juanmi Marquez says:

    Thanks. It solved my problem.

  55. hon says:

    Thanks, full. It solved my problem.

Leave a Reply

Your email address will not be published. Required fields are marked *