Nginx

Apache 在動態主機上重寫為 nginx

  • August 11, 2017

我做了一些閱讀,但我找不到我的問題的答案,因為它有一個與大多數情況不同的關鍵組成部分。它與其他故事一樣開始:我需要將 .htaccess 遷移到 nginx 配置中,如果不是這樣,這將非常簡單:nginx 伺服器設置為使用動態主機:

server {
   listen 80;

   server_name ~^(www\.)?(?<sname>.+?).server.company.com$;
   root /var/www/$sname/current/public;
   index index.html index.htm index.php;

   location / {
       try_files $uri $uri/ /index.php$is_args$args;
   }

   location ~* \.(gif|png|bmp|ico|flv|swf|exe|html|htm|txt|css|js) {
       add_header        Cache-Control public;
       add_header        Cache-Control must-revalidate;
       expires           7d;
   }

   location ~ \.php$ {

       fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
       include fastcgi_params;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_index index.php;
   }

   location ~ /\.ht {
       deny all;
   }
}

這樣做是為了根據其目錄在單個子域上執行多個項目。問題是其中一個項目(我們稱之為 theproject.domain.company.com)是一個非常古老的龐然大物,它使用 .htaccess 進行大量重定向。我可以為這些重定向創建位置塊,但我不知道如何將它們僅應用於該項目(我對 nginx 不是很有經驗)。

我願意接受任何可能的解決方案,我的理論是:

  1. 目錄特定的 nginx 配置 - 有點像 htaccess 但不確定 nginx 是否甚至能夠動態載入配置

  2. 對特定伺服器名稱使用 if 塊,但不確定語法,因為我找不到任何 if 用於伺服器名稱的範例

3)為該子域單獨的虛擬主機,這將是一個可行的,雖然不是一個非常優雅的解決我的問題,問題是我不知道如何設置優先級,因為該子域將匹配動態虛擬主機的相同模式

非常感謝任何幫助、建議或連結

選項(3)降低了破壞每個子域的風險,只是為了修復一個流氓子域。一個完全匹配的serverserver_name總是優先於正則表達式server_name。有關詳細資訊,請參閱此文件

如果您想最小化重複配置,請將常用語句解除安裝到單獨的文件中,然後使用include語句將它們拉入。

例如:

server {
   listen 80;
   server_name www.theproject.server.company.com theproject.server.company.com;

   root /var/www/theproject/current/public;

   #
   # ... statements to fix "theproject"
   #

   include /path/to/common/config;
}

server {
   listen 80;
   server_name ~^(www\.)?(?<sname>.+?).server.company.com$;

   root /var/www/$sname/current/public;
   include /path/to/common/config;
}

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