自Drupal8开始简洁网址(clean urls)国内常称为伪静态是drupal运行必要需求之一,如果你使用的是Nginx服务器,你会发现安装drupal10后除了首页可以打开,进入后台提示“404页面没有找到”,这是由于Drupal的开发和部署大多数都是在Apache上完成的,因此对于使用apache的服务器,通常只需要启用apache的“mod_rewrite”扩展即可满足drupal伪静态需求。而nginx服务器则需要配置drupal的nginx静态规则,才能正常运行drupal10。
Drupal10的nginx伪静态规则。
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /usr/local/tengine/conf/ssl/atuwe.com.crt;
ssl_certificate_key /usr/local/tengine/conf/ssl/atuwe.com.key;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name www.atuwe.com www.atuwe.com;
access_log off;
index index.html index.htm index.php;
root /data/wwwroot/atuwe/web;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
if ($host != atuwe.com) { return 301 $scheme://atuwe.com$request_uri; }
#上面是网站的SSL和路径配置,非drupal10配置,不要复制
#drupal10的nginx伪静态规则从这里开始。
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to scripts in site files directory
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
}
# Allow "Well-Known URIs" as per RFC 5785
location ~* ^/.well-known/ {
allow all;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/)\. {
return 403;
}
location / {
# try_files $uri @rewrite; # For Drupal <= 6
try_files $uri /index.php?$query_string; # For Drupal >= 7
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
# Don't allow direct access to PHP files in the vendor directory.
location ~ /vendor/.*\.php$ {
deny all;
return 404;
}
# In Drupal 8, we must also match new paths where the '.php' appears in
# the middle, such as update.php/selection. The rule we use is strict,
# and only allows this pattern with the update.php front controller.
# This allows legacy path aliases in the form of
# blog/index.php/legacy-path to continue to route to Drupal nodes. If
# you do not have any paths like that, then you might prefer to use a
# laxer rule, such as:
# location ~ \.php(/|$) {
# The laxer rule will continue to work if Drupal uses this new URL
# pattern with front controllers other than update.php in a future
# release.
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
# Security note: If you're running a version of PHP older than the
# latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
# See http://serverfault.com/q/627903/94922 for details.
include fastcgi_params;
# Block httpoxy attacks. See 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 socket location.
#fastcgi_pass unix:/var/run/php5-fpm.sock;
# PHP 7 socket location.
#特别说明,要将下面的php-cgi.sock更改为自己服务器相应文件的路径,否则会报错。
fastcgi_pass unix:/dev/shm/php-cgi.sock;
}
# Fighting with Styles? This little gem is amazing.
# location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
try_files $uri @rewrite;
}
# Handle private files through Drupal. Private file's path can come
# with a language prefix.
location ~ ^(/[a-z\-]+)?/system/files/ { # For 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;
}
#for google adsense
location = /ads.txt {
allow all;
log_not_found off;
access_log off;
}
#以下这行代码修复执行/update.php升级站点,或通过后台安装主题/模块出现错误的bug
rewrite ^/core/authorize.php/core/authorize.php(.*)$ /core/authorize.php$1;
}
本站基于Drupal10构建,以上为本站完整的nginx配置规则,如果你使用的宝塔面板或其他面板,请仔细看配置文件中做的注释,php-cgi.sock文件路径必须替换为自己服务器的,如果你不知道这个文件的路径,linux系统服务器可以通过find / -name php-cgi.sock命令定位文件路径。
另外提醒一点,Drupal10的依赖组件和使用的技术比较新,建议使用最新稳定版Nginx。
国内有一些站长在使用nginx的替代品Tengine,由于Tengine是基于nginx之上开发且完美兼容nginx,因此drupal10的nginx配置同样适用于Tengine。
最后附上Nginx官方提供的Drupal配置文件:
https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/