【问题标题】:Apache: multiple virtual hosts and reverse proxyApache:多个虚拟主机和反向代理
【发布时间】:2021-08-16 15:59:04
【问题描述】:

我正在尝试配置一个 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”时,我仍然连接到 localhost 服务器. 但是这个配置不起作用。我可以在本地连接到 localhost 和“http://temp:8080”,但无法通过“http://my.domain.com/”连接。

如果我注释第一个 VirtualHost,并且我配置第二个监听端口 80,我可以使用“http://my.domain.com/”连接(但是这样反向代理是没用的:如果我省略了那部分,它仍然有效),但在本地我无法连接到“localhost”(显然)。

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

【问题讨论】:

    标签: apache reverse-proxy virtualhost


    【解决方案1】:

    现在,如果您连接到http://localhost/,它将使用您的第一个 VirtualHost 进行响应。已经可以了。

    但如果您连接到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>
    

    详情:

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

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

    http://localhost/
    http://my.domain.com/ which proxies to http://temp:8080/
    http://temp:8080/
    

    【讨论】:

    • 谢谢您,但此配置仍然不起作用:我收到错误。 In the httpd-vhosts.conf file: The number of DocumentRoot is not equal to the number of ServerName They should be identical. 当我尝试访问 my.domain.com 时,我在日志(本地主机日志)“客户端被服务器配置拒绝”中收到 403 错误
    • 我只是把那个确切的配置放在我的 apache 上,它就可以工作了。我确实添加了我在第一个答案中忘记的Listen 8080 行。关于您提到的有关拥有 VirtualHost 的数量 == DocumentRoot 的数量的消息,我不知道它来自哪里,但这是错误的。在大多数情况下,它应该,但并非总是如此。就像这里一样,我们有一个只做代理的 VirtualHost,所以不需要像服务文件一样配置目录。在my.domain.com 中,我添加了LogLevel trace8 以查看完整的调试消息。
    猜你喜欢
    • 2016-10-28
    • 2018-04-05
    • 1970-01-01
    • 2021-01-03
    • 2017-09-25
    • 2021-08-30
    • 2019-01-21
    • 2013-05-22
    • 2016-10-16
    相关资源
    最近更新 更多