我们有 Drupal 多站点(每个站点有不同的域),使用单个 Drupal 8 代码库和 Nginx 作为 Web 服务器。现在我们想为几个网站设置 Drupal Milti-site 和子目录,格式如下,“子目录”不区分大小写(即,站点应该可以通过“子目录”或“SubDirectory”访问):
- http://site1.com(Drupal 多站点,由“sites/site1.com”提供服务)
- http://site1.com/subdirectory(Drupal 多站点,由“sites/site1.com.subdirectory”提供服务)
- http://site2.com(Drupal 多站点,由“sites/site2.com”提供服务)
- http://site2.com/subdirectory(Drupal 多站点,由“sites/site2.com.subdirectory”提供服务)
我们在 CentOS 上的 Nginx 上运行这些网站。我们能够让子网站在以下结构下运行(但不能在以上结构下运行):
- http://site1.com(静态 HTML 页面)
- http://site1.com/subdirectory(Drupal 多站点)
以下是我们正在使用的 nginx 虚拟主机配置。
注意:我已更新 nginx 配置以删除 SSL。
# VHOST Config.
server {
listen 80;
server_name site1.com;
root /var/www/html;
index index.php index.html index.htm;
# Prevent files from being accessed.
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\.htaccess*$ {
return 403;
}
location ~ (^|/)\. {
return 403;
}
location ~ .*\.config$ {
return 403;
}
location ~ /composer.*$ {
return 403;
}
location ~ \..*/.*\.yml$ {
return 403;
}
location / {
try_files $uri $uri/ =404;
}
location @rewrite {
try_files $uri /subdirectory/index.php?$query_string;
}
location /subdirectory/ {
try_files $uri $uri/ @rewrite;
}
location ~ '\.php$|^/update.php' {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
proxy_cache_bypass 1;
}
}
任何帮助是极大的赞赏。
答案1
你为什么不以不同的方式设置这些事情?我认为它可以为你做些什么:
server {
listen 80;
server_name site1.com;
root /var/www/sites/site1; #I suppose that in site1 it's the code for site1.com
index index.php index.html index.htm;
.
.
.#simple configuration here, don't care about site1.com/subdomain
.
}
server {
listen 80;
server_name site2.com;
root /var/www/sites/site2; #I suppose that in site2 it's the code for site2.com
index index.php index.html index.htm;
.
.
.#simple configuration here, don't care about site2.com/subdomain
.
}
然后,进入 /var/www/sites/site1 和 /var/www/sites/site2 ,并创建一个到 ../site1.com.subdomain ../site2.com.subdomain 的符号链接,如下所示:
cd /var/www/sites/site1
ln -s ../site1.com.subdomain subdomain
cd /var/www/sites/site2
ln -s ../site2.com.subdomain subdomain
像这样,您在 /var/www/sites/site1 /var/www/sites/site2 /var/www/sites/site1.com.subdomain 和 /var/www/sites/site2.com.subdomain 中拥有每个 Drupal 的代码
但是当访问 site1 时,它会进入 /var/www/sites/site1,如果访问 site1.com/subdomain,它会进入 /var/www/sites/site1/subdomain ,它会按照符号链接将连接带到 /var/www/sites/site1.com.subdomain。
这难道不是你需要的吗?还是我看错了问题?
答案2
以下是带有子目录的 Drupal 多站点的实际解决方案。
# VHOST Config.
server {
# Define server name and directory root.
server_name site1.com;
root /var/www/html/drupal;
index index.php;
# Listen to port
listen 80;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /subdirectory {
try_files $uri $uri/ @nested;
location ~ '\.php$|^/update.php' {
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
}
location @nested {
try_files $uri $uri/ /subdirectory/index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ '\.php$|^/update.php' {
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
proxy_cache_bypass 1;
}
}
笔记 :
- 上述配置适用于使用 Drupal 8 的服务器页面“site1.com”和“site1.com/subdirectory”。
唯一悬而未决的问题是,Drupal 更新不适用于上述配置的“site1.com/subdirectory”(site1.com/subdirectory/update.php 可以工作,但其后的页面不工作)。为了使其工作,我必须在 vhost 中进行以下更改
# Change try_files in 'location @nested' as below : location @nested { try_files $uri $uri/ /subdirectory/update.php?$query_string; }
非常感谢任何帮助,以解决上述问题并使一切正常运行,而无需进行更改location @nested
。
希望这可以帮助有需要的人启动并运行 Drupal 8 多站点子目录。