nginx_simple_include.conf 14.1 KB
Newer Older
Grace's avatar
Grace committed
1
#######################################################
2
###  nginx.conf site standard vhost include start
Grace's avatar
Grace committed
3 4
#######################################################

5 6
set $nocache_details "Cache";

7
###
8
### Deny crawlers.
9
###
10
if ($is_crawler) {
11
  return 403;
12 13 14
}

###
15
### Deny not compatible request methods without 405 response.
16
###
17
if ( $request_method !~ ^(?:GET|HEAD|POST|PUT|DELETE|OPTIONS)$ ) {
18
  return 403;
19 20 21
}

###
22
### Deny listed requests for security reasons.
23 24
###
if ($is_denied) {
25
  return 403;
26 27 28
}

###
29
### Deny listed requests for security reasons.
30 31
###
location ~* (?:delete.+from|insert.+into|select.+from|union.+select|onload|\.php.+src|system\(.+|document\.cookie|\;|\.\.) {
32
  return 403;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
}

###
### If favicon else return error 204.
###
location = /favicon.ico {
  access_log    off;
  log_not_found off;
  expires       30d;
  try_files     /sites/$host/files/favicon.ico $uri =204;
}

###
### Support for http://drupal.org/project/robotstxt module
### and static file in the sites/domain/files directory.
48 49
###
location = /robots.txt {
50 51
  access_log    off;
  log_not_found off;
52 53 54 55
  try_files /sites/$host/files/robots.txt $uri @cache;
}

###
56 57
### Allow local access to support wget method in Aegir settings
### for running sites cron.
58
###
59 60 61 62 63 64 65 66
location = /cron.php {
  access_log   off;
  allow        127.0.0.1;
  deny         all;
  try_files    $uri =404;
  fastcgi_pass 127.0.0.1:9000;
}

omega8cc's avatar
omega8cc committed
67 68 69 70 71 72 73 74 75 76 77 78
###
### Allow local access to support wget method in Aegir settings
### for running sites cron in Drupal 8.
###
location = /core/cron.php {
  access_log   off;
  allow        127.0.0.1;
  deny         all;
  try_files    $uri =404;
  fastcgi_pass 127.0.0.1:9000;
}

79 80
###
### Send search to php-fpm early so searching for node.js will work.
81
### Deny bots on search uri.
82
###
83 84 85
location ^~ /search {
  location ~* ^/search {
    if ($is_bot) {
86
      return 403;
87 88
    }
    try_files $uri @cache;
89
  }
90 91 92
}

###
93
### Deny crawlers and never cache known AJAX requests.
94
###
95
location ~* /(?:autocomplete|ajax|ahah)/ {
96
  if ($is_bot) {
97
    return 403;
98 99 100 101 102 103 104 105 106
  }
  access_log     off;
  log_not_found  off;
  set $nocache_details "Skip";
  try_files $uri @drupal;
}

###
### Fix for Aegir & .info .pl domain extensions.
107
###
108
location ^~ /hosting {
109
  access_log off;
110 111
  set $nocache_details "Skip";
  try_files $uri @drupal;
112 113 114
}

###
115 116
### Support for backup_migrate module download/restore/delete actions.
###
117
location ^~ /admin {
118
  if ($is_bot) {
119
    return 403;
120 121 122 123 124 125 126 127 128
  }
  access_log off;
  set $nocache_details "Skip";
  try_files $uri @drupal;
}

###
### Support for audio module.
###
129 130 131
location ^~ /audio/download {
  location ~* ^/audio/download/.*/.*\.(?:mp3|mp4|m4a|ogg)$ {
    if ($is_bot) {
132
      return 403;
133 134 135 136
    }
    access_log off;
    set $nocache_details "Skip";
    try_files $uri @drupal;
137 138 139 140
  }
}

###
141
### Deny listed requests for security reasons.
142 143
###
location ~* (/\..*|settings\.php$|\.(?:git|htaccess|engine|inc|info|install|module|profile|pl|po|sh|.*sql|theme|tpl(?:\.php)?|xtmpl)$|^(?:Entries.*|Repository|Root|Tag|Template))$ {
144
  return 403;
145 146 147
}

###
148 149 150 151
### Deny some not supported URI like cgi-bin on the Nginx level.
###
location ~* (?:cgi-bin|vti-bin|wp-content) {
  access_log off;
152
  return 403;
153 154 155
}

###
156
### Deny bots on some weak modules uri.
157
###
158
location ~* (?:calendar|event|validation|aggregator|vote_up_down|captcha) {
159
  if ($is_bot) {
160
    return 403;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  }
  try_files $uri @cache;
}

###
### Responsive Images support.
### http://drupal.org/project/responsive_images
###
location ~* \.r\.(?:jpe?g|png|gif) {
  if ( $http_cookie ~* "rwdimgsize=large" ) {
    rewrite ^/(.*)/mobile/(.*)\.r(\.(?:jpe?g|png|gif))$ /$1/desktop/$2$3 last;
  }
  rewrite ^/(.*)\.r(\.(?:jpe?g|png|gif))$ /$1$2 last;
  access_log off;
  add_header X-Header "RI Generator 1.0";
  set $nocache_details "Skip";
  try_files  $uri @drupal;
}

###
### Adaptive Image Styles support.
### http://drupal.org/project/ais
###
location ~* /(?:.+)/files/styles/adaptive/(?:.+)$ {
  if ( $http_cookie ~* "ais=(?<ais_cookie>[a-z0-9-_]+)" ) {
    rewrite ^/(.+)/files/styles/adaptive/(.+)$ /$1/files/styles/$ais_cookie/$2 last;
  }
  access_log off;
  add_header X-Header "AIS Generator 1.0";
  set $nocache_details "Skip";
  try_files  $uri @drupal;
}

###
### Imagecache and imagecache_external support.
196 197
###
location ~* /(?:external|system|files/imagecache|files/styles)/ {
198 199 200
  access_log off;
  log_not_found off;
  expires    30d;
201
  # fix common problems with old paths after import from standalone to Aegir multisite
202 203 204
  rewrite    ^/sites/(.*)/files/imagecache/(.*)/sites/default/files/(.*)$  /sites/$host/files/imagecache/$2/$3 last;
  rewrite    ^/files/imagecache/(.*)$                                      /sites/$host/files/imagecache/$1 last;
  rewrite    ^/files/styles/(.*)$                                          /sites/$host/files/styles/$1 last;
205
  add_header X-Header "IC Generator 1.0";
206 207
  set $nocache_details "Skip";
  try_files  $uri @drupal;
208 209 210
}

###
211
### Deny direct access to backups.
212 213 214 215 216 217
###
location ~* ^/sites/.*/files/backup_migrate/ {
  access_log off;
  deny all;
}

omega8cc's avatar
omega8cc committed
218 219 220 221 222 223 224 225
###
### Deny direct access to config files in Drupal 8.
###
location ~* ^/sites/.*/files/config_.* {
  access_log off;
  deny all;
}

226
###
227 228
### Private downloads are always sent to the drupal backend.
### Note: this location doesn't work with X-Accel-Redirect.
229
###
230
location ~* ^/sites/.*/files/private/ {
231
  access_log off;
232 233 234 235
  rewrite    ^/sites/.*/files/private/(.*)$ $scheme://$host/system/files/private/$1 permanent;
  add_header X-Header "Private Generator 1.0a";
  set $nocache_details "Skip";
  try_files  $uri @drupal;
236 237 238
}

###
239 240
### Deny direct access to private downloads in sites/domain/private.
### Note: this location works with X-Accel-Redirect.
241
###
242
location ~* ^/sites/.*/private/ {
243 244 245 246 247
  access_log off;
  internal;
}

###
248 249
### Deny direct access to private downloads also for short, rewritten URLs.
### Note: this location works with X-Accel-Redirect.
250
###
251
location ~* /files/private/ {
252
  access_log off;
253
  internal;
254 255 256
}

###
257
### Advagg_css and Advagg_js support.
258 259
###
location ~* files/advagg_(?:css|js)/ {
260 261
  access_log off;
  expires    max;
262
  limit_conn gulag 88;
263 264 265 266
  rewrite    ^/files/advagg_(.*)/(.*)$ /sites/$host/files/advagg_$1/$2 last;
  add_header ETag "";
  add_header Cache-Control "max-age=290304000, no-transform, public";
  add_header Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT";
267
  add_header X-Header "AdvAgg Generator 1.0";
268 269
  set $nocache_details "Skip";
  try_files  $uri @drupal;
270 271 272
}

###
273
### Make css files compatible with boost caching.
274 275
###
location ~* \.css$ {
276
  if ( $request_method = POST ) {
277 278
    return 405;
  }
279
  if ( $cache_uid ) {
280 281
    return 405;
  }
282 283 284 285 286 287
  error_page  405 = @uncached;
  access_log  off;
  tcp_nodelay off;
  expires     max; #if using aggregator
  add_header  X-Header "Boost Citrus 2.1";
  try_files   /cache/perm/$host${uri}_.css $uri =404;
288 289 290
}

###
291
### Make js files compatible with boost caching.
292
###
293 294
location ~* \.(?:js|htc)$ {
  if ( $request_method = POST ) {
295 296
    return 405;
  }
297
  if ( $cache_uid ) {
298 299
    return 405;
  }
300 301 302 303 304 305
  error_page  405 = @uncached;
  access_log  off;
  tcp_nodelay off;
  expires     max; # if using aggregator
  add_header  X-Header "Boost Citrus 2.2";
  try_files   /cache/perm/$host${uri}_.js $uri =404;
306 307 308
}

###
309
### Make json compatible with boost caching.
310 311
###
location ~* \.json$ {
312
  if ( $request_method = POST ) {
313 314
    return 405;
  }
315
  if ( $cache_uid ) {
316 317
    return 405;
  }
318 319 320 321 322 323
  error_page  405 = @uncached;
  access_log  off;
  tcp_nodelay off;
  expires     max; ### if using aggregator
  add_header  X-Header "Boost Citrus 2.3";
  try_files   /cache/$device/$host${uri}_.json $uri =404;
324 325 326
}

###
327
### Helper location to bypass boost static files cache for logged in users.
328 329 330
###
location @uncached {
  access_log off;
331
  expires max; # max if using aggregator, otherwise sane expire time
332 333 334
}

###
335 336
### Serve & no-log static files & images directly,
### without all standard drupal rewrites, php-fpm etc.
337
###
338 339 340 341
location ~* ^.+\.(?:jpe?g|gif|png|ico|bmp|svg|swf|pdf|docx?|xlsx?|pptx?|tiff?|txt|rtf|cgi|bat|pl|dll|aspx?|exe|class|otf|ttf|woff|eot)$ {
  access_log  off;
  tcp_nodelay off;
  expires     30d;
342
  add_header  Access-Control-Allow-Origin *;
343
  # allow files/images/downloads to be accessed without /sites/fqdn/
344 345 346 347 348
  rewrite     ^/files/(.*)$              /sites/$host/files/$1 last;
  rewrite     ^/images/(.*)$             /sites/$host/files/images/$1 last;
  rewrite     ^/downloads/(.*)$          /sites/$host/files/downloads/$1 last;
  rewrite     ^/.+/sites/.+/files/(.*)$  /sites/$host/files/$1 last;
  try_files   $uri =404;
349 350 351
}

###
352 353
### Serve & log bigger media/static/archive files directly,
### without all standard drupal rewrites, php-fpm etc.
354
###
355 356 357
location ~* ^.+\.(?:avi|mpe?g|mov|wmv|mp3|mp4|m4a|ogg|ogv|flv|wav|midi|zip|tar|t?gz|rar)$ {
  expires     30d;
  tcp_nodelay off;
358
  add_header  Access-Control-Allow-Origin *;
359
  # allow files/downloads to be accessed without /sites/fqdn/
360 361 362 363
  rewrite     ^/files/(.*)$              /sites/$host/files/$1 last;
  rewrite     ^/downloads/(.*)$          /sites/$host/files/downloads/$1 last;
  rewrite     ^/.+/sites/.+/files/(.*)$  /sites/$host/files/$1 last;
  try_files   $uri =404;
364 365 366
}

###
367
### Serve & no-log some static files as is, without forcing default_type.
368 369
###
location ~* /(?:cross-?domain)\.xml$ {
370 371 372 373 374
  access_log  off;
  tcp_nodelay off;
  expires     30d;
  add_header  X-Header "XML Generator 1.0";
  try_files   $uri =404;
375 376 377
}

###
378
### Allow some known php files (like serve.php in the ad module).
379
###
380
location ~* /(?:modules|libraries)/(?:contrib/)?(?:ad|tinybrowser|f?ckeditor|tinymce|wysiwyg_spellcheck|ecc|civicrm|fbconnect|radioactivity)/.*\.php$ {
381 382 383
  access_log   off;
  try_files    $uri =404;
  fastcgi_pass 127.0.0.1:9000;
384 385 386
}

###
387
### Serve & no-log static helper files used in some wysiwyg editors.
388
###
389
location ~* ^/sites/.*/(?:modules|libraries)/(?:tinybrowser|f?ckeditor|tinymce)/.*\.(?:html?|xml)$ {
390
  access_log      off;
391
  tcp_nodelay     off;
392 393 394 395 396
  expires         30d;
  try_files $uri =404;
}

###
397
### Serve & no-log any not specified above static files directly.
398
###
399 400 401 402 403
location ~* ^/sites/.*/files/ {
  access_log      off;
  tcp_nodelay     off;
  expires         30d;
  try_files $uri =404;
404 405 406
}

###
407
### Make feeds compatible with boost caching and set correct mime type.
408 409
###
location ~* \.xml$ {
410
  if ( $request_method = POST ) {
411 412
    return 405;
  }
413
  if ( $cache_uid ) {
414 415 416 417 418 419 420
    return 405;
  }
  error_page 405 = @drupal;
  access_log off;
  add_header Expires "Tue, 24 Jan 1984 08:00:00 GMT";
  add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
  add_header X-Header "Boost Citrus 2.4";
421
  charset    utf-8;
422 423
  types { }
  default_type application/rss+xml;
424
  try_files /cache/$device/$host${uri}_.xml /cache/$device/$host${uri}_.html $uri @drupal;
425 426 427
}

###
428
### Deny bots on never cached uri.
429
###
430
location ~* ^/(?:.*/)?(?:user|cart|checkout|logout|flag) {
431
  if ($is_bot) {
432
    return 403;
433
  }
434
  access_log off;
435 436
  set $nocache_details "Skip";
  try_files $uri @drupal;
437 438 439
}

###
440
### Deny bots on never cached uri - og compatible.
441
###
442 443
location ~* /(?:node/[0-9]+/edit|node/add|comment/reply|approve|users) {
  if ($is_bot) {
444
    return 403;
445 446
  }
  access_log off;
447 448
  set $nocache_details "Skip";
  try_files $uri @drupal;
449 450 451
}

###
452
### Catch all unspecified requests.
453 454
###
location / {
455
  if ( $http_user_agent ~* wget ) {
456
    return 403;
457
  }
458 459 460 461
  try_files $uri @cache;
}

###
462
### Boost compatible cache check.
463 464
###
location @cache {
465 466
  if ( $request_method = POST ) {
    set $nocache_details "Method";
467 468
    return 405;
  }
469 470 471 472 473 474 475 476 477 478
  if ( $args ~* "nocache=1" ) {
    set $nocache_details "Args";
    return 405;
  }
  if ( $http_cookie ~* "OctopusNoCacheID" ) {
    set $nocache_details "OctopusNCookie";
    return 405;
  }
  if ( $cache_uid ) {
    set $nocache_details "DrupalCookie";
479 480 481 482 483 484
    return 405;
  }
  error_page 405 = @drupal;
  add_header Expires "Tue, 24 Jan 1984 08:00:00 GMT";
  add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
  add_header X-Header "Boost Citrus 1.9";
485 486
  charset    utf-8;
  try_files  /cache/$device/$host${uri}_$args.html @drupal;
487 488 489
}

###
490
### Send all not cached requests to drupal with clean URLs support.
491 492
###
location @drupal {
493 494 495 496 497 498 499 500 501 502 503 504 505 506
  error_page 418 = @nobots;
  if ($args) {
    return 418;
  }
  rewrite ^/(.*)$  /index.php?q=$1 last;
}

###
### Send all known bots to $args free URLs.
###
location @nobots {
  if ($is_bot) {
    rewrite ^ $scheme://$host$uri? permanent;
  }
507 508 509 510
  rewrite ^/(.*)$  /index.php?q=$1 last;
}

###
511
### Send all non-static requests to php-fpm, restricted to known php file.
512
###
513
location = /index.php {
514
  add_header    X-Engine "Aegir";
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
  add_header    X-Device "$device";
  add_header    X-Speed-Cache "$upstream_cache_status";
  add_header    X-Speed-Cache-UID "$cache_uid";
  add_header    X-Speed-Cache-Key "$key_uri";
  add_header    X-NoCache "$nocache_details";
  add_header    X-This-Proto "$http_x_forwarded_proto";
  ###
  ### Use Nginx cache for all visitors.
  ###
  set $nocache "";
  if ( $nocache_details ~ (?:OctopusNCookie|Args|Skip) ) {
    set $nocache "NoCache";
  }
  fastcgi_cache speed;
  fastcgi_cache_methods GET HEAD; ### Nginx default, but added for clarity
  fastcgi_cache_min_uses 1;
531
  fastcgi_cache_key "$is_bot$device$host$request_method$key_uri$cache_uid$http_x_forwarded_proto$cookie_respimg";
532 533
  fastcgi_cache_valid 200 10s;
  fastcgi_cache_valid 302 1m;
534
  fastcgi_cache_valid 301 403 404 5s;
535
  fastcgi_cache_valid 500 502 503 504 1s;
536 537 538 539 540 541 542 543
  fastcgi_ignore_headers Cache-Control Expires;
  fastcgi_pass_header Set-Cookie;
  fastcgi_pass_header X-Accel-Expires;
  fastcgi_pass_header X-Accel-Redirect;
  fastcgi_no_cache $cookie_OctopusNoCacheID;
  fastcgi_no_cache $nocache;
  fastcgi_cache_bypass $cookie_OctopusNoCacheID;
  fastcgi_cache_bypass $nocache;
544
  fastcgi_cache_use_stale error updating timeout invalid_header;
545 546 547 548 549 550 551 552
  try_files     $uri =404; ### check for existence of php file first
  fastcgi_pass  127.0.0.1:9000;
  expires epoch;
}

###
### Send other known php requests/files to php-fpm without any caching.
###
omega8cc's avatar
omega8cc committed
553
location ~* ^/(?:core/)?(?:boost_stats|update|authorize|rtoc|xmlrpc)\.php$ {
554 555 556 557 558 559 560
  access_log   off;
  try_files    $uri =404; ### check for existence of php file first
  fastcgi_pass 127.0.0.1:9000;
}

###
### Deny access to any not listed above php files.
561 562 563 564
###
location ~* ^.+\.php$ {
  deny all;
}
565

Grace's avatar
Grace committed
566
#######################################################
567
###  nginx.conf site standard vhost include end
Grace's avatar
Grace committed
568 569
#######################################################