Skip to content

Instantly share code, notes, and snippets.

@Luc45
Created December 22, 2018 15:36
Show Gist options
  • Save Luc45/b8ca3620f302f1731331c2f3d865c415 to your computer and use it in GitHub Desktop.
Save Luc45/b8ca3620f302f1731331c2f3d865c415 to your computer and use it in GitHub Desktop.
WordPress Multisite serve script for Nginx (Homestead, etc)
#!/usr/bin/env bash
block="# WordPress multisite subdirectory rules.
# Designed to be included in any server {} block.
map \$uri \$blogname{
~^(?P<blogpath>/[^/]+/)files/(.*) \$blogpath ;
}
map \$blogname \$blogid {
default -999;
#Ref: http://wordpress.org/extend/plugins/nginx-helper/
include $2/wp-content/uploads/nginx-helper/map.conf;
}
server {
listen ${3:-80};
listen ${4:-443} ssl http2;
server_name $1;
root \"$2\";
index index.html index.htm index.php;
charset utf-8;
location ~ ^(/[^/]+/)?files/(.+) {
try_files /wp-content/blogs.dir/\$blogid/files/\$2 /wp-includes/ms-files.php?file=\$2 ;
access_log off; log_not_found off; expires max;
}
#avoid php readfile()
location ^~ /blogs.dir {
internal;
alias $2/wp-content/blogs.dir ;
access_log off; log_not_found off; expires max;
}
if (!-e \$request_filename) {
# Don't use `$uri` here, see https://github.com/yandex/gixy/issues/77
rewrite /wp-admin$ \$scheme://\$host\$request_uri/ permanent;
rewrite ^(/[^/]+)?(/wp-.*) \$2 last;
rewrite ^(/[^/]+)?(/.*\.php) \$2 last;
}
location / {
try_files \$uri \$uri/ /index.php?\$args ;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php$5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
$paramsTXT
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
#add some rules for static content expiry-headers here
}"
echo "$block" > "/etc/nginx/sites-available/$1"
ln -fs "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment