2013-04-08 37 views
2

我有一個Apache前端服務器,直到今天代理子域api.myapp.net的流量到在Nginx上運行Rails應用程序的後端服務器。如何通過Apache代理服務第二個子域

現在我在我的域名投資組合中添加了第二個子域alpha.myapp.net,併爲它提供了相同的IP。流向該子域的流量不應該擊中Rails應用,而應該設置爲服務於靜態站點的Nginx服務器上的第二個VHost。

所以我有一個代理服務器配置爲api.myapp.net

NameVirtualHost *:80 

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName api.myapp.net 
    ServerAlias api.myapp.net 

    DirectoryIndex index.html 

    RewriteEngine On 

    RewriteLog /var/log/apache2/rewrite.log 
    RewriteLogLevel 9 

    RewriteCond %{HTTP_HOST} !^(api\.)?myapp\.net$ 
    RewriteRule ^(.*)$ http://myapp.net$1 [L,R=301] 

    ProxyPass/http://192.168.1.145/ 
    ProxyPassReverse/http://192.168.1.145/ 

    ErrorLog /var/log/apache2/error.log 

    LogLevel warn 

    CustomLog /var/log/apache2/access.log combined 
    ServerSignature Off 
</VirtualHost> 

我設置爲alpha.myapp.net第二配置:

NameVirtualHost *:80 

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName alpha.myapp.net 
    ServerAlias alpha.myapp.net 

    DirectoryIndex index.html 

    RewriteEngine On 

    RewriteLog /var/log/apache2/rewrite.log 
    RewriteLogLevel 9 

    RewriteCond %{HTTP_HOST} !^(alpha\.)?myapp\.net$ 
    RewriteRule ^(.*)$ http://myapp.net$1 [L,R=301] 

    ProxyPass/http://192.168.1.145/ 
    ProxyPassReverse/http://192.168.1.145/ 

    ErrorLog /var/log/apache2/error.log 

    LogLevel warn 

    CustomLog /var/log/apache2/access.log combined 
    ServerSignature Off 
</VirtualHost> 

現在發生了什麼,是,所有的流量alpha.myapp.net打我的Rails監聽api.myapp.net的請求的應用程序。

我整理了所有的Nginx配置問題,所以我認爲它必須是Apache錯誤配置。我在Apache的重寫日誌中看到的似乎解釋了這個問題:

xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (2) init rewrite engine with requested uri /index.html 
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (3) applying pattern '^(.*)$' to uri '/index.html' 
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (4) RewriteCond: input='alpha.myapp.net' pattern='!^(alpha\.)?myapp\.net$' => not-matched 
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (1) pass through /index.html 

最後一部分pass through /index.html似乎忽略子域和域部分。因此,Nginx後端服務器現在不需要請求哪個子域,並且服務於來自第一個可用服務器的請求,該服務器爲api

現在的問題似乎是:我如何代理從Apache前端到Nginx後端的流量並維護子域和域?

或者是否還有其他問題?

希望有人能幫忙。

問候菲利克斯

回答

2

有時候,答案會出現問題。問題恰恰是缺少的主機名。

我解決了這個問題,在我的Apache服務器上編輯/etc/hosts並添加了兩個條目。一個api.myapp.net,一個alpha.myapp.net都引用相同的IP。

然後我改變了bot的Apache代理配置,所以ProxyPassProxyPassReverse不再使用IP的,而是新的主機名。

而瞧,它的工作原理。

相關問題