502 Bad Gateway on NGINX with BuddyPress

I recently switched over from Apache to NGINX for my WordPress hosting, and it was surprisingly easier than I expected. Alongside the W3 Total Cache plugin to handle my minification and object/db caching my sites are blazing fast. The one problem I did run into was random 502 errors on some pages. The fix was to update the server{} block of nginx.conf to include parameters for proxy_* buffers and fastcgi_* buffers. In my configuration this is in an include file so that it can be easily imported into all the different servers that I run on this host.

My full configuration for WordPress (which also supports pretty URLs, MultiSite, BuddyPress, and WooCommerce) is:

# Use the Nginx-Helper plugin to automatically generate this map
# https://wordpress.org/plugins/nginx-helper/
map $http_host $blogid {
    default                         0;
    include /var/www/example.com/html/wp-content/uploads/nginx-helper/map.conf;
}

server {
    # accept connections on all IP addresses, port 80
    listen                          80;

    # listing on SSL port 443?
    #listen                         443 ssl;
    #ssl                            on;
    #ssl_certificate                /etc/nginx/ssl/example.com.crt;
    #ssl_certificate_key            /etc/nginx/ssl/example.com.key;

    # name this server
    server_name                     example.com www.example.com;

    # set the root folder for web files
    root                            /var/www/example.com/html;

    # MultiSite Configuration ################

        # avoid PHP readfile()
        location ^~ /blogs.dir {
            internal;
            alias                       /var/www/com.rovair/www/html/wp-content/blogs.dir;
            access_log                  off;
            log_not_found               off;
            expires                     max;
        }

        # WPMU files
        location ~ ^/files/(.*)$ {
            try_files                   /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1;
            access_log                  off;
            log_not_found               off;
            expires                     max;
        }

    # End MultiSite Configuration ############

    index                           index.php;

    # serve static files and send everything else to WordPress
    try_files $uri $uri/ /index.php?$args;

    # send PHP requests to fastcgi (uses spawn-fcgi)
    location ~ \.php$ {
        # zero-day exploit defense.
        try_files                       $uri =404;

        # performance boosts for PHP
        sendfile                        on;
        tcp_nopush                      off;
        keepalive_requests              0;

        # proxy buffers - no 502 errors!
        proxy_buffer_size               128k;
        proxy_buffers                   4 256k;
        proxy_busy_buffers_size         256k;

        # fastcgi buffers - no 502 errors!
        fastcgi_buffering               on;
        fastcgi_buffer_size             16k;
        fastcgi_buffers                 16 16k;

        # max timeouts (should match php.ini)
        fastcgi_connect_timeout         600s;
        fastcgi_send_timeout            600s;
        fastcgi_read_timeout            600s;

        # index page
        fastcgi_index                   index.php;

        # pass request to fastcgi/php-cgi via spawn-fcgi
        fastcgi_pass                    localhost:53217;

        # default fastcgi_params
        include                         fastcgi_params;

        # override fastcgi_params
        fastcgi_param                   SERVER_NAME $host;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;

        break;
    }
}

Some quick notes:

  • The various buffer parameters prevent semi-random 502 errors, for example trying to logout of a BuddyPress site always resulted in a 502, and remaining logged in
  • try_files will test to see if a file/directory exists and if not passes it off to WordPress’ index.php file – the ?$args at the end is important for WordPress to receive the entire $_REQUEST

You may also like...

1 Response

  1. lolux says:

    Thanks for tip. This was a life-saver. I couldn’t figure out why WordPress logout requests were returning 502’s after I activated Apache+NginX in Plesk. Added the proxy buffer and fastcgi buffer lines to the NginX config section in Plesk and boom, it worked.

Leave a Reply

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