|
| 1 | +### Nginx configuration for Drupal 7 and 8. This configuration makes use of |
| 2 | +### drush (http:///drupal.org/project/drush) for site maintenance |
| 3 | +### and like tasks: |
| 4 | +### |
| 5 | +### 1. Run the cronjobs. |
| 6 | +### 2. Run the DB and code updates: drush up or drush upc followed by |
| 7 | +### drush updb to run any DB updates required by the code upgrades |
| 8 | +### that were performed. |
| 9 | +### 3. Disabling of xmlrpc.xml and update.php: all updates are now |
| 10 | +### handled through drush. |
| 11 | + |
| 12 | +## If ssl terminator had ssl, we're passing that on |
| 13 | +if ($http_x_forwarded_proto = 'https') { |
| 14 | + set $fastcgi_https "on"; |
| 15 | +} |
| 16 | + |
| 17 | +## rewriting /index.php to / because after https://www.drupal.org/node/2599326 |
| 18 | +## autocomplete URLs are forced to go to index.php |
| 19 | +rewrite ^/index.php / last; |
| 20 | + |
| 21 | +## The 'default' location. |
| 22 | +location / { |
| 23 | + |
| 24 | + |
| 25 | + if ($request_method = 'OPTIONS') { |
| 26 | + add_header 'Access-Control-Allow-Origin' "$http_origin" always; |
| 27 | + add_header 'Access-Control-Allow-Credentials' 'true' always; |
| 28 | + add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS' always; |
| 29 | + add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Cache-Hash' always; |
| 30 | + |
| 31 | + ### Tell client that this pre-flight info is valid for 20 days |
| 32 | + add_header 'Access-Control-Max-Age' 1728000; |
| 33 | + add_header 'Content-Type' 'text/plain charset=UTF-8'; |
| 34 | + add_header 'Content-Length' 0; |
| 35 | + return 204; |
| 36 | + } |
| 37 | + |
| 38 | + ## Disallow access to any dot files |
| 39 | + location ~ /\. { |
| 40 | + deny all; |
| 41 | + access_log off; |
| 42 | + log_not_found off; |
| 43 | + } |
| 44 | + |
| 45 | + ## Replicate the Apache <FilesMatch> directive of Drupal standard |
| 46 | + ## .htaccess. Disable access to any code files. Return a 404 to curtail |
| 47 | + ## information disclosure. Hide also the text files. |
| 48 | + location ~* ^(?:.+\.(?:htaccess|make|txt|yml|md||engine|inc|info|install|module|profile|po|pot|sh|.*sql|test|theme|tpl(?:\.php)?|xtmpl)|code-style\.pl)$ { |
| 49 | + deny all; |
| 50 | + access_log off; |
| 51 | + log_not_found off; |
| 52 | + } |
| 53 | + |
| 54 | + ## Expiring per default for two weeks, Drupal will overwrite that if necessary |
| 55 | + expires 2w; |
| 56 | + |
| 57 | + ## First we try the URI and relay to the /index.php?q=$escaped_uri&$args if not found. |
| 58 | + try_files $uri @drupal; |
| 59 | +} |
| 60 | + |
| 61 | +########### Security measures ########## |
| 62 | + |
| 63 | +## Restrict access to the strictly necessary PHP files. Reducing the |
| 64 | +## scope for exploits. Handling of PHP code and the Drupal event loop. |
| 65 | +location @drupal { |
| 66 | + ## Include the FastCGI config. |
| 67 | + include generic-configs.d/fastcgi_drupal.conf; |
| 68 | + |
| 69 | + add_header 'Access-Control-Allow-Origin' "$http_origin" always; |
| 70 | + add_header 'Access-Control-Allow-Credentials' 'true' always; |
| 71 | + add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS' always; |
| 72 | + add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Cache-Hash' always; |
| 73 | + |
| 74 | + |
| 75 | + # Adding args to query string. |
| 76 | + fastcgi_param QUERY_STRING $args; |
| 77 | + |
| 78 | + ## If a special xdebug php-fpm is exiting for this site, we check if the cookie or the parameters |
| 79 | + ## contain the string 'xdebug'. |
| 80 | + |
| 81 | + ## Requests with a cookie that contains "xdebug" are sent to the |
| 82 | + ## php-fpm that runs xdebug. |
| 83 | + if ($http_cookie ~* "xdebug") { |
| 84 | + fastcgi_pass unix:/var/run/php-fpm-drupal-xdebug.sock; |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + ## Requests with a parameter that contains "xdebug" are sent to the |
| 89 | + ## php-fpm that runs xdebug. |
| 90 | + if ($args ~* "xdebug") { |
| 91 | + fastcgi_pass unix:/var/run/php-fpm-drupal-xdebug.sock; |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + fastcgi_pass unix:/var/run/php-fpm-drupal.sock; |
| 97 | + |
| 98 | + ## @TODO Phase2: Learn more about MicroCache with Auth, as we have |
| 99 | + ## varnish most probably not necessary |
| 100 | + ## FCGI microcache for authenticated users also. |
| 101 | + #include apps/drupal/microcache_fcgi_auth.conf; |
| 102 | + |
| 103 | + ## @TODO need to test that more indept. |
| 104 | + ## Filefield Upload progress |
| 105 | + ## http://drupal.org/project/filefield_nginx_progress support |
| 106 | + ## through the NginxUploadProgress modules. |
| 107 | + ##track_uploads uploads 60s; |
| 108 | +} |
| 109 | + |
| 110 | +## Trying to access private files directly returns a 404. |
| 111 | +location /sites/default/files/private/ { |
| 112 | + internal; |
| 113 | +} |
| 114 | + |
| 115 | +## Disallow access to patches directory. |
| 116 | +location ^~ /patches { |
| 117 | + deny all; |
| 118 | + access_log off; |
| 119 | + log_not_found off; |
| 120 | +} |
| 121 | + |
| 122 | +## Disallow access to backup directory. |
| 123 | +location ^~ /backup { |
| 124 | + deny all; |
| 125 | + access_log off; |
| 126 | + log_not_found off; |
| 127 | +} |
| 128 | + |
| 129 | +## Disallow access to vagrant directory. |
| 130 | +location ^~ /vagrant { |
| 131 | + deny all; |
| 132 | + access_log off; |
| 133 | + log_not_found off; |
| 134 | +} |
| 135 | + |
| 136 | +## Disallow access to /core/vendor. |
| 137 | +location ^~ /core/vendor { |
| 138 | + deny all; |
| 139 | + access_log off; |
| 140 | + log_not_found off; |
| 141 | +} |
| 142 | + |
| 143 | +## Disallow access to /vendor. |
| 144 | +location ^~ /vendor { |
| 145 | + deny all; |
| 146 | + access_log off; |
| 147 | + log_not_found off; |
| 148 | +} |
| 149 | + |
| 150 | +## Disable access logs for robots.txt. |
| 151 | +location = /robots.txt { |
| 152 | + access_log off; |
| 153 | + ## Add support for the robotstxt module |
| 154 | + ## http://drupal.org/project/robotstxt. |
| 155 | + try_files $uri @drupal; |
| 156 | +} |
| 157 | + |
| 158 | +location = /humans.txt { |
| 159 | + access_log off; |
| 160 | + ## Add support for the humanstxt module |
| 161 | + ## http://drupal.org/project/humanstxt. |
| 162 | + try_files $uri @drupal; |
| 163 | +} |
| 164 | + |
| 165 | +## Support for favicon. Return an 1x1 transparent GIF if it doesn't |
| 166 | +## exist. |
| 167 | +location = /favicon.ico { |
| 168 | + expires 30d; |
| 169 | + try_files /favicon.ico @empty; |
| 170 | +} |
| 171 | + |
| 172 | +## Return an in memory 1x1 transparent GIF. |
| 173 | +location @empty { |
| 174 | + expires 30d; |
| 175 | + empty_gif; |
| 176 | +} |
| 177 | + |
| 178 | +# Any other attempt to access PHP files does not have access. |
| 179 | +location ~* ^.+\.php$ { |
| 180 | + deny all; |
| 181 | +} |
| 182 | + |
| 183 | +### Directives for installing drupal. |
| 184 | +location = /install.php { |
| 185 | + ## If a special xdebug php-fpm is exiting for this site, we check if the cookie or the parameters |
| 186 | + ## contain the string 'xdebug'. |
| 187 | + |
| 188 | + ## Requests with a cookie that contains "xdebug" are sent to the |
| 189 | + ## php-fpm that runs xdebug. |
| 190 | + if ($http_cookie ~* "xdebug") { |
| 191 | + fastcgi_pass unix:/var/run/php-fpm-drupal-xdebug.sock; |
| 192 | + break; |
| 193 | + } |
| 194 | + ## Requests with a parameter that contains "xdebug" are sent to the |
| 195 | + ## php-fpm that runs xdebug. |
| 196 | + if ($args ~* "xdebug") { |
| 197 | + fastcgi_pass unix:/var/run/php-fpm-drupal-xdebug.sock; |
| 198 | + break; |
| 199 | + } |
| 200 | + fastcgi_pass unix:/var/run/php-fpm-drupal.sock; |
| 201 | +} |
| 202 | +### Directives for installing drupal. |
| 203 | +location = /core/install.php { |
| 204 | + ## If a special xdebug php-fpm is exiting for this site, we check if the cookie or the parameters |
| 205 | + ## contain the string 'xdebug'. |
| 206 | + |
| 207 | + ## Requests with a cookie that contains "xdebug" are sent to the |
| 208 | + ## php-fpm that runs xdebug. |
| 209 | + if ($http_cookie ~* "xdebug") { |
| 210 | + fastcgi_pass unix:/var/run/php-fpm-drupal-xdebug.sock; |
| 211 | + break; |
| 212 | + } |
| 213 | + ## Requests with a parameter that contains "xdebug" are sent to the |
| 214 | + ## php-fpm that runs xdebug. |
| 215 | + if ($args ~* "xdebug") { |
| 216 | + fastcgi_pass unix:/var/run/php-fpm-drupal-xdebug.sock; |
| 217 | + break; |
| 218 | + } |
| 219 | + fastcgi_pass unix:/var/run/php-fpm-drupal.sock; |
| 220 | +} |
| 221 | + |
| 222 | +## PHP FPM status |
| 223 | +### The configuration for the status pages of php-fpm. As described in |
| 224 | +### http://www.php.net/manual/en/install.fpm.configuration.php. |
| 225 | + |
| 226 | +### php-fpm provides a status and a heartbeat page that is served through the web server. |
| 227 | +### Here's an example configuration for them. |
| 228 | + |
| 229 | +## The status page is at /fpm-status. Only local access is |
| 230 | +## allowed. Non authorized access returns a 404 through the error_page |
| 231 | +## directive. |
| 232 | +location = /fpm-status { |
| 233 | + if ($dont_show_fpm_status) { |
| 234 | + return 404; |
| 235 | + } |
| 236 | + fastcgi_pass unix:/var/run/php-fpm-drupal.sock; |
| 237 | +} |
| 238 | + |
| 239 | +## The ping page is at /ping and returns the string configured at the php-fpm level. |
| 240 | +## Also only local network connections (loopback and LAN) are permitted. |
| 241 | +location = /ping { |
| 242 | + if ($dont_show_fpm_status) { |
| 243 | + return 404; |
| 244 | + } |
| 245 | + fastcgi_pass unix:/var/run/php-fpm-drupal.sock; |
| 246 | +} |
| 247 | + |
| 248 | +## Nginx status |
| 249 | +### The configuration for Nginx status page. As described in |
| 250 | +### http://wiki.nginx.org/HttpStubStatusModule. |
| 251 | + |
| 252 | +### php-fpm provides a status and a heartbeat page that is served through the web server. |
| 253 | +### Here's an example configuration for them. |
| 254 | + |
| 255 | +## Get the nginx status. |
| 256 | +location /nginx_status { |
| 257 | + if ($dont_show_nginx_status) { |
| 258 | + return 404; |
| 259 | + } |
| 260 | + stub_status on; |
| 261 | + access_log off; |
| 262 | +} |
| 263 | + |
| 264 | +## Access to some admin scripts |
| 265 | +location ^~ /amazeeio/admin { |
| 266 | + alias /usr/local/lib/amazeeadmin; |
| 267 | + try_files $uri /index.php =404; |
| 268 | + include fastcgi_params; |
| 269 | + fastcgi_param SCRIPT_FILENAME $request_filename; |
| 270 | + fastcgi_pass unix:/var/run/php-fpm-drupal.sock; |
| 271 | +} |
0 commit comments