繁体   English   中英

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

[英]Apache: multiple virtual hosts and reverse proxy

我正在尝试配置 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”(显然)。

我怎样才能解决这个问题? 谢谢

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

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

根据您的问题,您需要 3 个 VirtualHost:

  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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM