Drupal是使用PHP语言编写的开源内容管理框架(CMF),它由内容管理系统(CMS)和PHP开发框架(Framework)共同构成。连续多年荣获全球最佳CMS大奖,是基于PHP语言最著名的WEB应用程序。截止2011年底,共有13,802位WEB专家参加了Drupal的开发工作;228个国家使用181种语言的729,791位网站设计工作者使用Drupal。著名案例包括:联合国、美国白宫、美国商务部、纽约时报、华纳、迪斯尼、联邦快递、索尼、美国哈佛大学、Ubuntu等。
Drupal在Apache环境下使用是件很轻松的事,因为官方已经把写好的.htaccess文件附在源代码里,一般在配置里直接就可以打开了。但在Nginx下却没有那么简单,但不用担心,实事上并不怎么复杂。Nginx已经在其官方网站上做出了配置示例。
server { server_name example.com; root /var/www/drupal8; ## <-- Drupal唯一的安装路径. location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # 尽量避免这些在局域网外被访问 location ~* \.(txt|log)$ { allow 192.168.0.0/16; deny all; } location ~ \..*/.*\.php$ { return 403; } location ~ ^/sites/.*/private/ { return 403; } # 禁止访问站点文件目录中的脚本 location ~ ^/sites/[^/]+/files/.*\.php$ { deny all; } # 允许 "Well-Known URIs" 依据 RFC 5785 location ~* ^/.well-known/ { allow all; } # 禁止访问名称以". "开头的"隐藏"文件和目录。这包括版本控制系统(如Subversion或Git)等,用来存储控制文件的目录。 location ~ (^|/)\. { return 403; } location / { # try_files $uri @rewrite; # 如果 Drupal <= 6 try_files $uri /index.php?$query_string; # 如果 Drupal >= 7 } location @rewrite { rewrite ^/(.*)$ /index.php?q=$1; } # 禁止直接访问"vendor"目录中的PHP文件. location ~ /vendor/.*\.php$ { deny all; return 404; } # 在Drupal8中,我们还必须匹配中间出现".php"的新路径,例如update.php/selection。 # 我们使用的规则是严格的,并且只允许update.php前端控制器使用此模式。 # 这允许blog/index.php/legacy-path形式的传统路径别名继续路由到Drupal节点。 # 如果你没有这样的路径,那么你可能更喜欢使用相对宽松的规则,例如: # location ~ \.php(/|$) { # 如果Drupal在将来的版本中将这种新的URL模式与update.php以外的前端控制器一起使用,那么宽松规则将继续工作。 location ~ '\.php$|^/update.php' { # 确保PHP文件存在。缓解漏洞CVE-2019-11043 try_files $uri =404; fastcgi_split_path_info ^(.+?\.php)(|/.*)$; # 安全说明:如果运行的PHP版本低于5.3,则PHP.ini中应包含"cgi.fix_pathinfo=0;"。 # 详细信息,请参见 http://serverfault.com/q/627903/94922。 include fastcgi_params; # 阻止 httpoxy 漏洞攻击. 详细信息,请参见 https://httpoxy.org/. fastcgi_param HTTP_PROXY ""; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param QUERY_STRING $query_string; fastcgi_intercept_errors on; # PHP 5 套接字定位. #fastcgi_pass unix:/var/run/php5-fpm.sock; # PHP 7 套接字定位. fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; } # Fighting with Styles? This little gem is amazing.(处理Styles?) # location ~ ^/sites/.*/files/imagecache/ { # 如果 Drupal <= 6 location ~ ^/sites/.*/files/styles/ { # 如果 Drupal >= 7 try_files $uri @rewrite; } # 通过Drupal处理私有文件。私有文件的路径可以带有语言前缀。 location ~ ^(/[a-z\-]+)?/system/files/ { # 如果 Drupal >= 7 try_files $uri /index.php?$query_string; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { try_files $uri @rewrite; expires max; log_not_found off; } # 强制整理 URLs # 从 URLs 中移除 index.php , 像这样 www.example.com/index.php/my-page --> www.example.com/my-page # 对于永久性或其他重定向代码,可以使用301. if ($request_uri ~* "^(.*/)index\.php(.*)") { return 307 $1$2; } }
原文参考:https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/