针对多个域的Apache反向代理configuration

我是新手。 我有1个LAMP CentOS服务器,它使用以下Apache httpd.confconfiguration托pipe3个网站:

 NameVirtualHost *:80 NameVirtualHost *:443 <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /home/www/html/domaina.com ServerName www.domaina.com ServerAlias *.domaina.com ScriptAlias /cgi-bin/ "/home/www/html/domaina.com/cgi-bin/" RewriteEngine On RewriteOptions Inherit ErrorLog /home/log/domaina.com-error_log CustomLog /home/log/domaina.com-access_log common </VirtualHost> <VirtualHost *:443> SSLEngine on SSLCertificateFile /etc/pki/tls/certs/ca.crt SSLCertificateKeyFile /etc/pki/tls/private/ca.key ServerAdmin [email protected] DocumentRoot /home/www/html/domaina.com ServerName www.domaina.com ServerAlias *.domaina.com ScriptAlias /cgi-bin/ "/home/www/html/domaina.com/cgi-bin/" RewriteEngine On RewriteOptions Inherit ErrorLog /home/log/domaina.com-error_log CustomLog /home/log/domaina.com-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /home/www/html/domainb.com ServerName www.domainb.com ServerAlias *.domainb.com ScriptAlias /cgi-bin/ "/home/www/html/domainb.com/cgi-bin/" RewriteEngine On RewriteOptions Inherit ErrorLog /home/log/domainb.com-error_log CustomLog /home/log/domainb.com-access_log common </VirtualHost> <VirtualHost *:443> SSLEngine on SSLCertificateFile /etc/pki/tls/certs/ca.crt SSLCertificateKeyFile /etc/pki/tls/private/ca.key ServerAdmin [email protected] DocumentRoot /home/www/html/domainb.com ServerName www.domainb.com ServerAlias *.domainb.com ScriptAlias /cgi-bin/ "/home/www/html/domainb.com/cgi-bin/" RewriteEngine On RewriteOptions Inherit ErrorLog /home/log/domainb.com-error_log CustomLog /home/log/domainb.com-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /home/www/html/domainc.com ServerName www.domainc.com ServerAlias *.domainc.com ScriptAlias /cgi-bin/ "/home/www/html/domainc.com/cgi-bin/" RewriteEngine On RewriteOptions Inherit ErrorLog /home/log/domainc.com-error_log CustomLog /home/log/domainc.com-access_log common </VirtualHost> <VirtualHost *:443> SSLEngine on SSLCertificateFile /etc/pki/tls/certs/ca.crt SSLCertificateKeyFile /etc/pki/tls/private/ca.key ServerAdmin [email protected] DocumentRoot /home/www/html/domainc.com ServerName www.domainc.com ServerAlias *.domainc.com ScriptAlias /cgi-bin/ "/home/www/html/domainc.com/cgi-bin/" RewriteEngine On RewriteOptions Inherit ErrorLog /home/log/domainc.com-error_log CustomLog /home/log/domainc.com-access_log common </VirtualHost> 

当域直接指向它时,所有的东西都可以在这个服务器上运行 但是我想使用另一台服务器作为domainc.com的反向代理。 所以我在另一台CentOS服务器上安装了apache,并指向domainc.com。 我把下面的configuration放到/etc/httpd/conf.d/proxy.conf

 <IfModule mod_proxy.c> #turning ProxyRequests on and allowing proxying from all may allow #spammers to use your proxy to send email. ProxyRequests Off <Proxy *> AddDefaultCharset off Order deny,allow Allow from all </Proxy> # Enable/disable the handling of HTTP/1.1 "Via:" headers. # ("Full" adds the server version; "Block" removes all outgoing Via: headers) # Set to one of: Off | On | Full | Block ProxyVia On </IfModule> 

而这个configuration到httpd.conf中:

 NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin [email protected] ServerName www.domainc.com ErrorLog logs/domainc.com-error_log CustomLog logs/domainc.com-access_log common ProxyRequests Off ProxyPreserveHost On ProxyPass / http://[IP of server 1]:80/ ProxyPassReverse / http://[IP of server 1]:80/ </VirtualHost> 

但是现在当我尝试浏览domainc.com时,我得到了domaina.com的内容。 我一直试图找出几个小时,尝试不同的configuration,我在网上find,但我仍然得到相同的结果。 有人可以帮忙吗? 是否有可能做到这一点?

当你在这个configuration下使用mod_proxy的时候,原来的Host: -header会被你在ProxyPass编写的任何内容replace。 所以当一个客户端连接到你的domainc服务器时,客户端会发送头文件Host: www.domainc.com 。 你的反向代理将把这个头去掉,而是发送Host: [IP of server] 。 而且由于你没有在任何虚拟主机中列出的IP,所以Apache将简单地select虚拟主机列表中的第一个,即domaina.com

解决这个问题的最好方法是更改​​代理configuration,添加该行

 ProxyPreserveHost On 

这将使得Apache在连接到后端服务器时重新使用原来的Host:-header。

(您也可以将IP地址添加到domainc.com的虚拟主机configuration,但是如果您想要代理服务器上的其他域,则会出现完全相同的问题,所以这不是我所推荐的)。

 ssh host2 sed -i~ ' /Proxy/s/[IP of server 1]/[hostname of server 1]/ ' /etc/httpd/conf/httpd.conf 

请仔细阅读这个机制,尤其是如果更改标记为RPM的configuration文件,如果它们被修改,将不会收到任何[安全]更新。 这是最近(-ish)一个安全更新的问题,表面上是传递给/ etc / sudoers的,但是基于RPM的系统的新手一直都不知道要注意什么。 在你的情况下,习惯/etc/httpd/conf.s/v-057-virtual-serverc.conf和类似命名的文件。 (或者不要,现在,你的select)