我有一个 Apache 前端服务器,直到今天,它仍将子域 api.myapp.net
的流量代理到在 Nginx 上运行 Rails 应用程序的后端服务器。
现在我在我的域组合中添加了第二个子域 alpha.myapp.net
并为其提供了相同的 IP。到该子域的流量不应到达 Rails 应用程序,而是 Nginx 服务器上设置为服务于静态站点的第二个 VHost。
所以我有一个 api.myapp.net
的代理配置:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@myapp.net
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 webmaster@myapp.net
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
最后一部分通过/index.html
似乎省略了子域和域部分。因此,Nginx 后端服务器现在不会为请求的子域提供来自第一个可用服务器的请求,即 api
。
现在的问题似乎是:如何将Apache前端的流量代理到Nginx后端并维护子域和域?
还是有其他问题?
希望有人能帮忙。
问候费利克斯
最佳答案
有时答案伴随着问题而来。问题正是缺少主机名。
我通过在我的 Apache 服务器上编辑 /etc/hosts
并添加两个条目来解决这个问题。一个 api.myapp.net
,一个 alpha.myapp.net
都引用相同的 IP。
然后我更改了 bot Apache 代理配置,以便 ProxyPass
和 ProxyPassReverse
不再使用 IP,而是使用新的主机名。
瞧,它起作用了。
关于apache - 如何通过 Apache 代理服务第二个子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15873940/