nginx sample conf for drupal
mouse 1969 · person cloud · link
Last update
2019-02-01
2019
02-01
« — »
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/

server {
  listen      80;
  listen      localhost:8000;
  server_name www.mysite.net;

  root        /path/to/folder;  # path for static files

  access_log  /path/to/log/nginx.access.log main;
  error_log   /path/to/log/nginx.error.log  error;

  add_header  X-Frame-Options SAMEORIGIN;

  # https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
  location ~ \..*/.*\.php$       { return 403; }          # don't exit from root
  location ~ ^/sites/.*/private/ { return 403; }          # drupal private folders
  location ~ (^|/)\.             { return 403; }          # hidden files
  location ~ /vendor/.*\.php$ { deny all; return 404; }   # exclude vendor folder
  location / { try_files $uri /index.php?$query_string; } # drupal default route

  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$; # set $fastcgi_script_name and $fastcgi_path_info

    try_files $fastcgi_script_name =404;
    fastcgi_pass unix:/var/run/phpX-fpm.sock;

    include fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    # fix empty PATH_INFO  =>  https://trac.nginx.org/nginx/ticket/321
    set             $path_info      $fastcgi_path_info;
    fastcgi_param   PATH_INFO       $path_info;

    fastcgi_index index.php;
  }

  # fighting with styles? this little gem is amazing:
  # location ~ ^/sites/.*/files/styles/ { try_files $uri @rewrite;         }
  # location @rewrite                   { rewrite ^/(.*)$ /index.php?q=$1; }

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