![后端服务器上的 Apache 代理虚拟主机](https://linux22.com/image/798707/%E5%90%8E%E7%AB%AF%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84%20Apache%20%E4%BB%A3%E7%90%86%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA.png)
我有 2 个 Web Debian 服务器,一个从端口 80 接受请求的前端和一个通过前端的代理接受请求的后端。
我想要做的是让许多不同的 URL 连接到带有虚拟主机的后端服务器。
这是前端 Apache 设置
<VirtualHost *:80>
ServerName dev.example.com
ProxyPass / http://192.168.144.100:80/
ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>
<VirtualHost *:80>
ServerName sandbox.example.com
ProxyPass / http://192.168.144.100:80/
ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>
这是后端 Apache 设置
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.example.com
DocumentRoot /var/www/example.com/dev/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/example.com/dev/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName sandbox.example.com
DocumentRoot /var/www/example.com/sandbox/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/example.com/sandbox/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
当我访问 dev.example.com 或 sandbox.example.com 时,它会带我到默认的 /var/www 文件。
基本上,我希望能够在后端服务器上运行多个虚拟主机。
感谢您的帮助。
答案1
像这样修改前端设置怎么样?而且您不需要在后端进行虚拟主机设置。
<VirtualHost *:80>
ServerName dev.example.com
ProxyPass / http://192.168.144.100:80/
ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>
<VirtualHost *:80>
ServerName sandbox.example.com
ProxyPass / http://192.168.144.100:80/example.com/dev/
ProxyPassReverse / http://192.168.144.100:80/example.com/dev/
</VirtualHost>
答案2
看这里同时使用 Virtual_host 和 mod_proxy举个例子
<VirtualHost *:80>
ProxyPreserveHost On
ServerName dev.example.com
ProxyPass / http://192.168.144.100:80/
ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ServerName sandbox.example.com
ProxyPass / http://192.168.144.100:80/
ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>
启用后,此选项([ProxyPreserveHost][1]
)将把传入请求中的 Host: 行传递到代理主机,而不是该ProxyPass
行中指定的主机名。
我认为(但不确定),在这种情况下,你甚至可以不用 来做到这一点ProxyPreserveHost
,如下所示:
把这个放到你的前端hosts
文件中
192.168.144.100 dev.example.com
192.168.144.100 sandbox.example.com
然后执行以下操作:
<VirtualHost *:80>
ServerName dev.example.com
ProxyPass / http://dev.example.com/
ProxyPassReverse / http://dev.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName sandbox.example.com
ProxyPass / http://sandbox.example.com/
ProxyPassReverse / http://sandbox.example.com/
</VirtualHost>