Apache:多个虚拟主机和反向代理

问题描述 投票:0回答:1

我正在尝试配置 Apache 服务器来处理多个虚拟主机并使用反向代理公开它们。

在我写的httpd-vhosts文件中:

<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "${INSTALL_DIR}/www"
 <Directory "${INSTALL_DIR}/www/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local
  </Directory>
</VirtualHost>

<VirtualHost *:8080>
    ServerName temp
    DocumentRoot "c:/users/test/sites/testsite"
    <Directory  "c:/users/test/sites/testsite">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ProxyPreserveHost On
    <Proxy>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass "temp:8080" "http://my.domain.com/"
    ProxyPassReverse "temp:8080" "http://my.domain.com/"
</VirtualHost>

我希望当我通过“http://my.domain.com/”连接到我的服务器时,客户端连接到临时虚拟服务器,而当连接到“localhost”时,我仍然连接到本地主机服务器。 但这个配置不起作用。我可以在本地连接到本地主机和“http://temp:8080”,但无法通过“http://my.domain.com/”连接。

如果我注释第一个 VirtualHost,并将第二个 VirtualHost 配置为侦听端口 80,我可以使用“http://my.domain.com/”进行连接(但这样,反向代理就没用了:如果我省略那部分,无论如何它都可以工作),但在本地我无法连接到“localhost”(显然)。

我该如何解决这个问题? 谢谢你

apache reverse-proxy virtualhost
1个回答
0
投票

现在,如果您连接到

http://localhost/
,它将使用您的第一个虚拟主机进行响应。已经可以了。

但是如果您连接到

http://temp:8080/
,它会代理到
http://my.domain.com/
。并且该站点未在任何地方配置。

根据您的问题,您想要 3 个虚拟主机:

  1. http://my.domain.com/
    --> 代理 -->
    http://temp:8080/
  2. http://localhost/
  3. http://temp:8008/

确保加载所需的代理模块。至少:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

然后像这样修改你的配置:

Listen 80
Listen 8080

<VirtualHost *:80>
    ServerName localhost
    ServerAlias localhost
    ErrorLog "logs/localhost_error.log"
    CustomLog "logs/localhost_access.log" combined

    DocumentRoot "${INSTALL_DIR}/www"
    <Directory "${INSTALL_DIR}/www/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName my.domain.com
    ErrorLog "logs/mydomain_error.log"
    CustomLog "logs/mydomain_access.log" combined
    LogLevel trace8

    ProxyPreserveHost On
    ProxyPass        "/" "http://temp:8080/"
    ProxyPassReverse "/" "http://temp:8080/"
</VirtualHost>

<VirtualHost *:8080>
    ServerName temp
    ErrorLog "logs/temp_error.log"
    CustomLog "logs/temp_access.log" combined

    DocumentRoot "c:/users/test/sites/testsite"
    <Directory  "c:/users/test/sites/testsite">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

详情:

  • 拆分日志文件总是更好,这样在不混合日志条目时更容易调试。
  • 无需为第二个虚拟主机设置单独的端口。由于您没有使用 https,Apache 根据请求的域名知道要使用哪个 VirtualHost。
  • 没有 DocumentRoot,没有代理配置的目录。

您现在拥有这些有效的站点,只要在本地主机文件中定义了名称(或者如果您有本地 DNS):

http://localhost/
http://my.domain.com/ which proxies to http://temp:8080/
http://temp:8080/
© www.soinside.com 2019 - 2024. All rights reserved.