Skip to content

Instantly share code, notes, and snippets.

@mdrmike
Last active August 29, 2015 14:14
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 mdrmike/3a8045ad9ad0f9606ca3 to your computer and use it in GitHub Desktop.
Save mdrmike/3a8045ad9ad0f9606ca3 to your computer and use it in GitHub Desktop.
NGINX VHOST TEMPLATE FOR DRUPALPRO
##------------------------------------
# NGINX VHOST TEMPLATE FOR DRUPALPRO
# Based on: http://wiki.nginx.org/Drupal
#-------------------------------------
#
# FIND AND REPLACE ##__VARIABLES__## in text editor
#
# ##__SERVER_TLD__## -- FQDN (aka URL) Example: foobar.dev, *.foobar.dev, AwesomeWebsite.com (multiple comma separated URI's is ok).
# ##__PATH_TO_SITE__## -- ABSOLUTE PATH TO DRUPAL Example: /home/drupalpro/websites/foobar.dev/www
# ##__D7_D8__## Drupal 7-8, Find & ERASE these variables
# ##__D6_ONLY__## Drupal 6, Find & ERASE these variables
# ##__UBUNTU__## Ubuntu Servers with default php5-fpm, Find & ERASE these variables
# ##__ARCH__## Arch Servers with default php-fpm, Find & ERASE these variables
# ##__CENT__## CentOS Servers with default php-fpm, Find & ERASE these variables
# ##__FPM_TCP__## For a php-fpm setup with TCP, Find & ERASE these variables
#
# EXAMPLE
# # cut-and-paste into a teminal or add it to a shell script
#
# SERVER_TLD="drupal7.dev" ; PATH_TO_SITE="$HOME/websites/drupal7.dev/www"
# # Careful, copy & paste the line above first
# # Then cut and paste the following
# sudo cp /etc/nginx/sites-available/TEMPLATE_drupalpro.conf "/etc/nginx/sites-available/$SERVER_TLD.conf" # Copy template to new config file
# sudo sed -i "s|##__SERVER_TLD__##|$SERVER_TLD|g" "/etc/nginx/sites-available/$SERVER_TLD.conf" # Customize domain
# sudo sed -i "s|##__PATH_TO_SITE__##|$PATH_TO_SITE|g" "/etc/nginx/sites-available/$SERVER_TLD.conf" # Customize path
# sudo sed -i "s|##__D7_D8__##||g" "/etc/nginx/sites-available/$SERVER_TLD.conf" # Customize for Drupal7
# sudo sed -i "s|##__UBUNTU__##||g" "/etc/nginx/sites-available/$SERVER_TLD.conf" # Customize for Ubuntu
#
# sudo ln -s "/etc/nginx/sites-available/$SERVER_TLD.conf" /etc/nginx/sites-enabled/ # enable the configuration
#
##-----------------------------------+
server {
server_name ##__SERVER_TLD__##; # Server name = domain
root ##__PATH_TO_SITE__##; # Path to website root
gzip_static on; # Enable compression, this will help if you have for instance advagg‎ module by serving Gzip versions of the files.
location = /favicon.ico { # Disable logging of favicon not found from buggy browsers/robots. Ancient Drupal bug: https://www.drupal.org/node/174940
log_not_found off;
access_log off;
}
location = /robots.txt { # Disable logging if robots.txt isn't found
allow all;
log_not_found off;
access_log off;
}
#location = /backup { # NOT USED. This matters if you use drush prior to 5.x
# deny all; # After 5.x backups are stored outside the Drupal install.
#}
location ~* \.(txt|log)$ { # Block access to txt|log files. Very rarely should these ever be accessed outside of your local server
allow 127.0.0.1; # You could change to a range, for lan access, example: allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ { # Not certain. Looks to prevent access to php files in hidden folders/files
return 403; # Source: http://wiki.nginx.org/Drupal
}
location ~ ^/sites/.*/private/ { # Block access to private folder(s)
return 403;
}
location ~ (^|/)\. { # Block access to "hidden" files and directories whose names begin with a
return 403; # period. This includes directories used by version control systems such
} # as Subversion or Git to store control files.
location / {
try_files $uri @rewrite; # This is cool because no php is touched for static content
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_intercept_errors on; # You have options here based on distro. To config for
##__UBUNTU__##fastcgi_pass unix:/var/run/php5-fpm.sock; ## Ubuntu 14.04 socket setup default, Find and Erase ##__UBUNTU__## comments
##__ARCH__##fastcgi_pass unix:/run/php-fpm/php-fpm.sock;## Arch (since at least 2014-Dec), Find and Erase ##__ARCH__## comments
##__CENT__##fastcgi_pass unix:/tmp/php5-fpm.sock; ## CentOS 6, Find and Erase ##__CENTOS__## comments
##__FPM_TCP__##fastcgi_pass 127.0.0.1:9000; ## For a php-fpm setup with TCP, Find and Erase ##__FPM_TCP__## comments
}
# Fighting with Styles? This little gem is amazing.
# If you have a custom location, you may need to adjust the regex path.
##__D7_D8__##location ~ ^/sites/.*/files/styles/ { # For Drupal7 and D8, remove ##__D7_D8__## comment from the beginning of this line
##__D6_ONLY__##location ~ ^/sites/.*/files/imagecache/ { # For Drupal6, remove ##__D6_ONLY__## comment from the beginning of this line
try_files $uri @rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location @rewrite { # @rewrite function -- used in location stanza's above
# You have 2 options here
# For Drupal7 and Drupal8 (and likley D9?), Clean URLs are handled in drupal_environment_initialize().
##__D7_D8__##rewrite ^ /index.php; # Therefore remove ##__D7_D8__## comments
# For Drupal 6 and below, Some modules enforce no slash (/) at the end of the URL Else this rewrite block wouldn't be needed (GlobalRedirect)
##__D6_ONLY__##rewrite ^/(.*)$ /index.php?q=$1; # Therefore remove the ##__D6_ONLY__## comments
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment