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:, server:, request: “GET /store/rastitelnie-extrakty HTTP/1.1”, upstream: “fastcgi://unix:/var/run/www-beauty.sock:”, host: “”, 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 on January 9, 2013 at 1:46 AM:

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

  2. bill on January 22, 2013 at 9:23 AM:

    solved the problem

  3. tin tuc 9x on February 5, 2013 at 12:04 AM:

    i fix error

  4. andrej on March 3, 2013 at 12:25 AM:

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

  5. 0108SR on March 7, 2013 at 5:16 PM:

    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

    • dmitriano in reply to 0108SR on March 7, 2013 at 5:39 PM:

      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.

  6. boem on March 11, 2013 at 5:16 PM:

    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.

  7. gijs on March 16, 2013 at 4:34 PM:

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

    • dmitriano in reply to gijs on March 16, 2013 at 6:29 PM:

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

  8. Luckyfire on March 23, 2013 at 4:10 PM:

    Works great! realy helps. Thank You!

  9. Davor on March 26, 2013 at 5:40 PM:

    doesn´t solved my problem… but thx for help

  10. Paul on April 3, 2013 at 2:38 PM:

    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 !

  11. Francisco on April 11, 2013 at 10:44 AM:

    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.



    • dmitriano in reply to Francisco on April 11, 2013 at 7:31 PM:

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

    • Noxxim in reply to Francisco on April 19, 2013 at 5:22 AM:

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

  12. Paul Thomson on July 22, 2013 at 6:08 AM:

    Seems to have done the trick,

    Keep up the good work, stay safe!
    Paul 🙂

  13. John on August 18, 2013 at 11:36 PM:

    It got rid of the problem… Thanks!

  14. Sepp on August 27, 2013 at 5:54 PM:

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

  15. Claude on September 1, 2013 at 6:57 AM:

    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?

    • dmitriano in reply to Claude on September 1, 2013 at 7:15 AM:

      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.

  16. WIlson on November 3, 2013 at 4:26 PM:

    Fixed my problem also! Tanks and success!

  17. Nyein Nyein on November 15, 2013 at 5:20 AM:

    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.

    • superadmin in reply to Nyein Nyein on November 15, 2013 at 8:29 AM:

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

  18. Tezel Celebi on December 13, 2013 at 7:50 AM:

    hi dmitriano,

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

  19. zodiac signs on December 22, 2013 at 9:49 AM:

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

  20. Rajavamsidhar chowdary on January 2, 2014 at 12:46 PM:

    Fixed my problem!! Thanks a lot

  21. Post job free on January 3, 2014 at 9:11 AM:

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

  22. Grateful Developer on February 24, 2014 at 1:13 AM:

    Fixed my problem too – thanks!

  23. Dealsoff on March 8, 2014 at 9:05 PM:

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

  24. Michel on April 9, 2014 at 12:03 PM:

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

  25. hack game android on June 25, 2014 at 4:27 PM:

    Thanks, i fixed that

  26. Ali on July 12, 2014 at 6:20 PM:

    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.

  27. Tunive on August 8, 2014 at 4:01 AM:

    Thanks, i fixed that!!!!

  28. Szabi on September 6, 2014 at 11:07 AM:

    Thanks a lot. Fixed my problem.

  29. Vanessa on September 21, 2014 at 12:34 PM:

    Ali, that did not work.

  30. prem726 on November 24, 2014 at 5:03 PM:

    I solved this problem by click on “rebuild” in category menu.
    it’s work in my joomla 3.2 website

  31. Jasa wordpress on January 29, 2015 at 1:43 AM:

    Fixed the problem. Thank you very much

  32. Asysteo on May 11, 2015 at 10:13 AM:

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

  33. Ben Mackay on May 18, 2015 at 9:15 PM:

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

  34. pirates on August 5, 2015 at 7:47 PM:

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

  35. Narasimha Mudiraj on October 16, 2015 at 5:52 AM:

    still facing same issue when i was added above code

  36. Narasimha Mudiraj on October 16, 2015 at 5:53 AM:

    Please help me on this issue

    • superadmin in reply to Narasimha Mudiraj on October 16, 2015 at 6:36 AM:

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

  37. Samurai on October 17, 2015 at 7:41 AM:

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

  38. Fuxy on November 24, 2015 at 11:56 AM:

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

  39. T on January 24, 2016 at 12:05 AM:

    Fixed. had the same issue. Thank you!

  40. harshada on March 7, 2016 at 5:28 AM:

    Thank you so much this really worked

  41. Octávio on March 11, 2016 at 2:46 PM:

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

  42. Tony on May 31, 2016 at 4:08 AM:

    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.

  43. Steven on July 14, 2016 at 9:11 PM:

    Still gold!

  44. Franz on August 10, 2016 at 10:01 AM:

    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’

  45. Foto bugil on October 15, 2016 at 10:03 AM:

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

  46. rekha on March 1, 2017 at 11:56 AM:

    502 Bad Gateway
    nginx/1.4.6 (Ubuntu)

    Getting error

  47. Avishai on March 24, 2017 at 8:54 AM:

    That did the trick for me too. Thanks!

  48. Claudia on August 15, 2017 at 6:46 AM:

    I was trying to open the COUCHSURFING site ( 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).


    • dmitriano in reply to Claudia on August 15, 2017 at 9:24 AM:

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

  49. david on September 24, 2017 at 1:04 AM:

    solved my problem within minutes


    i have no idea why this problem even occurred!

  50. Kevingreek on February 13, 2018 at 8:48 PM:

    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!!

  51. Adrian Lawrence on May 16, 2018 at 6:09 AM:

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

  52. shailesh Jaiswar on August 23, 2018 at 11:49 AM:

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

  53. Greg Rothfuss on January 21, 2019 at 3:30 PM:

    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”…

  54. Greg Rothfuss on January 23, 2019 at 6:23 PM:

    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 !!!

  55. Astro on January 28, 2019 at 9:17 PM:

    Man! You saved my bacon, thx!

  56. Juanmi Marquez on March 28, 2019 at 8:55 AM:

    Thanks. It solved my problem.

  57. hon on October 3, 2019 at 6:03 AM:

    Thanks, full. It solved my problem.

Leave a Reply

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