The post 502 Bad Gateway on NGINX with BuddyPress appeared first on Justin Silver.
]]>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:
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
The post 502 Bad Gateway on NGINX with BuddyPress appeared first on Justin Silver.
]]>The post Activate WP SlimStat Dashboard Widgets in WordPress Multisite appeared first on Justin Silver.
]]>If you have WordPress Multisite installed and the WP SlimStat plugin activated network wide, the optional WP SlimStat Dashboard widgets won’t be available to you. The problem is that the dashboard widgets file is using get_option('active_plugins')
to see if the plugin is active, and if it’s active network wide it won’t be in this array – it will be in get_site_options('active_sitewide_plugins')
. Rather than modifying the WP SlimStat plugin files (which will be overwritten when you upgrade), I recommend adding the following filter to a custom functions plugin to add the value to the array before it is read by the dashboard widgets. Using functions.php
in your theme is out because get_option()
in the WP SlimStat plugin is called before it loads. You could also use a Must Use Plugin placed in /wp-content/mu-plugins
.
There is some checking to make sure this only happens on the admin index page, but that’s just to be safe – it really shouldn’t have any adverse affects on your site except a few edge cases.
function activate_slimstat_dashboard_multisite($plugins){ // only run on multisite admin index screens. // $pagenow isn't available, so we have to preg_match the SCRIPT_NAME (should work for subdirectories) if (is_multisite() && preg_match("~/wp-admin/index\.php$~", $_SERVER['SCRIPT_NAME'])){ $slimstat_plugin = 'wp-slimstat/wp-slimstat.php'; // check if plugin is active network wide and if so add to site plugins if (array_key_exists($slimstat_plugin, get_site_option('active_sitewide_plugins', array()))) $plugins[] = $slimstat_plugin; } return $plugins; } add_filter( 'option_active_plugins', 'activate_slimstat_dashboard_multisite' );
The post Activate WP SlimStat Dashboard Widgets in WordPress Multisite appeared first on Justin Silver.
]]>The post WP_HOME and WP_SITEURL for WordPress Multisite Development & Migration appeared first on Justin Silver.
]]>I needed to be able to copy the wp_options
table(s) to my development environment as they contain configurations that are needed. To overcome the issue of not being able to set the WP_SITEURL
and WP_HOME
values in WordPress MultiSite, I wrote a custom filter to replace the _config_wp_siteurl()
and _config_wp_home()
functions that are available for non-multisite installs that is included in a plugin that is available network-wide and is configured in wp-config.php
. I am then able to copy all of the database tables except wp_site
and wp_blogs
to a local database.
I highly recommend the URL Token Replacement Techniques for WordPress 3.0 article by Chris Murphy to help handle URLs in your content.
This example assumes a subdomain multisite install, with a domain of example.com and two subdomains, www.example.com and second.example.com. The local development URLs will be www.example.local and second.example.local respectively.
Update the domain value in wp_site:
UPDATE wp_site SET domain = 'example.local' WHERE domain = 'example.com';
Update the domain value(s) in wp_blogs:
UPDATE wp_blogs SET domain = 'www.example.local' WHERE domain = 'www.example.com'; UPDATE wp_blogs SET domain = 'second.example.local' WHERE domain = 'second.example.com';
The following plugin should be installed network-wide.
<?php /* Plugin Name: MultiSite WP_HOME and WP_SITEURL Plugin URI: http://doublesharp.com/ Description: Allows wp_options values to be overwritten in wp-config.php for MultiSite Author: Justin Silver Version: 1.0 Author URI: http://doublesharp.com License: GPL2 */ /** * Replace this site's WP_SITEURL URL based on constant values * * @param String $url - The original URL * @return String - The replaced URL if overridden in wp-config.php */ function _ms_config_wp_siteurl( $url = '' ) { if (is_multisite()): global $blog_id, $current_site; $cur_blog_id = defined( 'BLOG_ID_CURRENT_SITE' )? BLOG_ID_CURRENT_SITE : 1; $key = ( $blog_id!=$cur_blog_id )? $blog_id.'_' : ''; $constant = 'WP_'.$key.'SITEURL'; if ( defined( $constant ) ) return untrailingslashit( constant($constant) ); endif; return $url; } add_filter( 'option_siteurl', '_ms_config_wp_siteurl' ); /** * Replace this site's WP_HOME URL based on constant values * * @param String $url - The original URL * @return String - The replaced URL if overridden in wp-config.php */ function _ms_config_wp_home( $url = '' ) { if (is_multisite()): global $blog_id; $cur_blog_id = defined( 'BLOG_ID_CURRENT_SITE' )? BLOG_ID_CURRENT_SITE : 1; $key = ( $blog_id!=$cur_blog_id )? $blog_id.'_' : ''; $constant = 'WP_'.$key.'HOME'; if ( defined( $constant ) ) return untrailingslashit( constant($constant) ); endif; return $url; } add_filter( 'option_home', '_ms_config_wp_home' ); ?>
Add new constants to wp-config.php. The primary site should use the standard WP_HOME and WP_SITEURL and the tertiary URLs should use WP_{$blog_id}_HOME
and WP_{$blog_id}_SITEURL
where {$blog_id}
is the numeric blog ID value that you want to replace from the wp_blogs
table.
define('WP_HOME', 'http://www.example.local'); define('WP_SITEURL', 'http://www.example.local'); define('WP_2_HOME', 'http://secondary.example.local'); define('WP_2_SITEURL', 'http://secondary.example.local');
This post is the result of a question that I originally asked on StackOverflow – Override WP_SITEURL and WP_HOME for WordPress Multisite – and ultimately answered with the help of Chris Murphy.
The post WP_HOME and WP_SITEURL for WordPress Multisite Development & Migration appeared first on Justin Silver.
]]>