Nginx config for Drupal 8

Nginx config for Drupal 8

Posted on Sat, 10/29/2016 - 01:25 by zhilevan

Just in case anyone is wondering what's the correct way to configure Nginx for running Drupal 8 installation, or having trouble with Clean URLs or Image styles, I'll post my config as a Gist here.

The Gist will be revised and improved over time, so please come and check for updates! Suggestions or thoughts are welcomed in comments.

Embedded code below:

server {

  server_name example.com;
  root /var/www/example;
  index index.php;

  error_log /var/www/log/example_error.log;

  location ~ \..*/.*\.php$ {
    return 403;
  }

  # Block access to hidden directories
  location ~ (^|/)\. {
    return 403;
  }

  location ~ ^/sites/.*/private/ {
    return 403;
  }

  # No php is touched for static content
  location / {
    try_files $uri @rewrite;
  }

  # pass the PHP scripts to FastCGI server
  location ~ \.php$ {
    fastcgi_index index.php;
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # The address or socket on which FastCGI requests are accepted. Set yours in www.conf
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  # Clean URLs
  location @rewrite {
    rewrite ^ /index.php;
  }

  # Image styles
  location ~ ^/sites/.*/files/styles/ {
    try_files $uri @rewrite;
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
  }

  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }

}