Nginx

帶有子目錄的 Drupal 多站點的 Nginx 虛擬主機配置

  • August 12, 2019

我們使用單個 Drupal 8 程式碼庫和 Nginx 作為 Web 伺服器執行 Drupal 多站點(每個站點具有不同的域)。現在我們想為幾個網站設置帶有子目錄的 Drupal Milti-site,格式如下,並且“子目錄”應該不區分大小寫(即站點應該可以通過“子目錄”或“子目錄”訪問):

  1. http://site1.com(Drupal 多站點,從“sites/site1.com”提供服務)
  2. http://site1.com/subdirectory(Drupal多站點,從“sites/site1.com.subdirectory”提供服務)
  3. http://site2.com(Drupal 多站點,從“sites/site2.com”提供)
  4. http://site2.com/subdirectory(Drupal多站點,從“sites/site2.com.subdirectory”提供)

我們在 CentOS 上的 Nginx 上執行這些網站。我們已經能夠讓子站點在以下結構中工作(但不是上述結構):

  1. http://site1.com(靜態 HTML 頁面)
  2. 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;
 }
}

任何幫助是極大的讚賞。

以下是帶有子目錄的 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;
 }

}

筆記 :

  1. 以上配置適用於“site1.com”和“site1.com/subdirectory”到使用 Drupal 8 的伺服器頁面。
  2. 唯一懸而未決的問題是,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 多站點作為子目錄啟動並執行。

你為什麼不設置不同的東西?我對它可以為您工作的想法:

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 中擁有每個 Drupal 的程式碼.子域

但是當訪問site1時,它會進入/var/www/sites/site1,如果訪問site1.com/subdomain,它會進入/var/www/sites/site1/subdomain,在符號連結之後,它’將連接到 /var/www/sites/site1.com.subdomain。

這不是你需要的嗎?或者我確實讀錯了這個問題?

引用自:https://serverfault.com/questions/974031