Yesterday I was configuring WordPress and Jetpack when I got a 502 Bad Gateway error error from Nginx.

It’s simple enough to fix but I’ve not used Nginx too much, here’s the problem and solution…

When you’re using Nginx as a reverse proxy to Apache sometimes the upstream server can send headers too big to fit in the Nginx buffer. We’re using Plesk Control Panel on our server, so I’m not sure if it’s to do with the defaults set for Nginx within that but to fix it do the following.

Open /etc/nginx/nginx.conf in your favourite editor

Add the following inside the http directive:

proxy_buffers 8 32k;
proxy_buffer_size 64k;

Save it, restart Nginx and Apache, ta da!

Your mileage may vary so play about with the number of buffers and their sizes.

Published by Rich

Geek, cyclist, ecoist and bad photographer. I develop systems for HE and fiddle with WordPress. #MIEE #MCE #AdobeEduCreative Apple Teacher

Join the conversation

2 Comments

  1. I’ve had exactly the same problem. At first I was getting ‘Allowed memory size of 268435456 bytes exhausted’ when I tried to activate Jetpack so I increased the memory limit in wp-config.php, eventually getting up to 1024mb. At this point I started getting the 502 errors you’ve described. I made the suggested changes in nginx.conf and restarted nginx and php5-fpm, but still I’m getting the 502 errors. I’ve altered the respective values to:

    proxy_buffers 8 256k;
    proxy_buffer_size 512k;

    However I’m still having no joy. If there’s anything you can suggest I’m all ears!

    1. We actually migrated to a new server recently and I had a chance to revisit these values and settings.

      We only allocate PHP (5.6.26) 256MB of memory for our large WordPress MU install and it seems to be OK with that.

      As for nginx we now run it with the following globally (notice the change to fastcgi)

      fastcgi_buffers 8 128k;
      fastcgi_buffer_size 256k;

      At the site level I use the following timeout, just so that it wasn’t configured for every site on the server:

      fastcgi_read_timeout 300;

      Obviously your mileage may vary, but on our setup this is working fine without errors.

Leave a comment

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