Nginx

Nginx:將“p.test-prd.com.local”的請求重寫為“www.test.com”

  • October 6, 2014

由於各種原因,我們遵循內部 DNS 方案,其中“www.test.com”的內部測試站點將是“p.test-tst.com.local”。我不會詳細說明為什麼我們最終採用了我們正在使用的方案。

現在,我可以將 ‘p.test-tst.com.local’、‘p.test-tst.de.local’、‘p.test-tst.co.uk.local’ (等等)添加到 Nginx此特定測試站點的配置文件。但是,難道沒有更靈活的方式來做到這一點嗎?我能否以某種方式讓 Nginx 接受“p.test.tst.com.local”,並讓它在內部將其重寫為“www.test.com”,以便網站(在 Drupal 上執行)認為請求是針對“ www.test.com’?

這會很好的主要原因之一是,當涉及到語言處理(為“com.local”設置語言)時,我不必考慮向 Drupal 添加更多邏輯。

如果我正確理解你,你需要這樣的東西:

map $http_host $name { hostnames; p.test.tst.com.local test.com; ... } ... server { ... location / { fastcgi_pass unix:/var/run/php5-fpm.sock include fastcgi_params fastcgi_param HTTP_HOST $name; }

這將設置 HTTP_HOST 變數 $ SERVER to test.com if actual requested hostname was p.test.tst.com.local. Drupal looks at $ 伺服器

$$ ‘HTTP_HOST’ $$值來確定主機名。您可以添加其他映射,map {}因此它非常靈活。

如果您希望您的站點響應這些 Hosts 值,那麼您別無選擇,您必須將它們放在server_name指令中(您可以使用萬用字元或正則表達式來縮短 conf)。

server {

   server_name www.test.com;
   include /path/to/server_names;

   [ ... ]

   location / {
       [ ... ]
       fastcgi_param HTTP_HOST "www.test.com";
   }

}

文件 /path/to/server_names :

server_name p.test-tst.co.uk.local;
server_name p.test-tst.de.local;

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