Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created June 21, 2018 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarstens/5022d8f5c3c7ac6b4f7b763c1afa5a85 to your computer and use it in GitHub Desktop.
Save scarstens/5022d8f5c3c7ac6b4f7b763c1afa5a85 to your computer and use it in GitHub Desktop.
Nginx configuration based on FanSided's local development shows has to setup nginx fallback for images that exist in production, so that you don't need to download them locally.
#matches all .dev TLDs
log_format main 'MAIN: $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
log_format wpc 'WPCONTENT: $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
log_format PROXY 'PROXY: $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
server {
listen 80;
listen 443 ssl;
server_name ~^.*\.dev$;
root /srv/www/fansidedblogs.test/htdocs;
access_log /var/log/custom.log main;
location /wp-content {
access_log /var/log/custom-image.log wpc;
}
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpe?g|gif|ico)$ {
expires 24h;
log_not_found off;
access_log /var/log/custom-image.log wpc;
try_files $uri $uri/ @production;
}
location @production {
rewrite_log on;
resolver 8.8.8.8;
access_log /var/log/custom-image.log PROXY;
error_log /var/log/custom-error.log debug;
rewrite ^(.*\/blogs.dir\/.*\/)(getty-images\/.*)$ /wp-content/uploads/$2?$args&fsproxy=true break;
rewrite ^(.*\/blogs.dir\/[0-9]+\/files\/)sites\/[0-9]+\/(.*)$ $1$2?$args&fsproxy=true break;
proxy_pass https://cdn.fansided.com;
}
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}
# Specify a charset
charset utf-8;
# Weird things happened to me when I had gzip on, may need to try
# again in the future as it could have been related to many other
# factors - JF
gzip off;
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# this prevents hidden files (beginning with a period) from being served
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
# Pass uploaded files to wp-includes/ms-files.php.
rewrite /files/$ /index.php last;
if ($uri !~ wp-content/plugins) {
#rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}
# Rewrite multisite in a subdirectory '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
location ~ \.php$ {
# Try the files specified in order. In our case, try the requested URI and if
# that fails, try (successfully) to pass a 404 error.
try_files $uri =404;
# Include the fastcgi_params defaults provided by nginx
include /etc/nginx/fastcgi_params;
# The amount of time for upstream to wait for a fastcgi process to send data.
# We keep this *extremely* high so that one can be lazy when remote debugging.
fastcgi_read_timeout 3600s;
# Buffer size for reading the header of the backend FastCGI process.
# This defaults to the value of a single fastcgi_buffers, so does not
# need to be specified in our case, but it's good to be explicit.
fastcgi_buffer_size 128k;
# The number and size of the buffers into which the reply from the FastCGI
# process in the backend is read.
#
# 4 buffers at 128k means that any reply by FastCGI greater than 512k goes
# to disk and replies under 512k are handled directly in memory.
fastcgi_buffers 4 128k;
# SCRIPT_FILENAME is a required parameter for things to work properly,
# but was missing in the default fastcgi_params on upgrade to nginx 1.4.
# We define it here to be sure that it exists.
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Use the upstream defined in the upstream variable.
fastcgi_pass $upstream;
# And get to serving the file!
fastcgi_index index.php;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment