繁体   English   中英

使用 Apache 2.2 mod_rewrite 合并子域

[英]Using Apache 2.2 mod_rewrite to consolidate subdomains

我在使用mod_rewrite时遇到了麻烦,需要帮助。

我在 DMZ 中有一个反向代理,它接受来自外部客户端的请求,请求子域sub1.example.comsub2.example.com并将它们(透明地)转发到内部公司网络内的单个机器internal.example.com 具体来说:

  1. http://sub1.example.comhttp://internal.example.com
  2. https://sub1.example.comhttps://internal.example.com
  3. http://sub2.example.comhttp://internal.example.com
  4. https://sub2.example.comhttps://internal.example.com

虽然我无法控制 DMZ 中执行重定向的代理,但我可以完全控制internal.example.com ,它承载 Apache 2.2 并在加载mod_rewrite的情况下监听80443

I need to configure this Apache instance to perform a redirect of any of the four above subdomain addresses ( sub1 or sub2 on either HTTP or HTTPS) to the fourth address https://sub2.example.com (4). 为此,我目前在httpd.conf中使用以下内容:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://sub2.example.com/$1 [R=301,L]

这适用于将请求地址 (1) 和 (3)(即,任一子域的 HTTP 地址)的客户端重定向到正确的目标 (4),但对重写对地址 (2) 的访问没有影响。 为了将 (2) 重定向到 (4),我将以下内容添加到配置 SSL 环境的VirtualHost元素中:

RewriteEngine On
RewriteCond %{SERVER_NAME} =sub1.example.com
RewriteRule ^/?(.*) https://sub2.example.com/$1 [R=301,L]

现在,如果客户端通过 HTTPS 请求sub1.example.com (通过 mod_rewrite 日志记录确认),则会触发此事件。 但是,虽然重定向现在在 DMZ后面的机器(内部和与internal.example.com相同的网络上)进行测试时可以正常工作,但它们无法在其外部的任何网络上工作,其中:

  • 任一子域(1 和 3)的 HTTP 地址无法完全加载
  • 任一子域(2 和 4)的 HTTPS 地址在客户端浏览器中产生错误,报告已执行过多重定向。

谁能建议我哪里出错了,或者可能是更适合我的情况的配置? 提前致谢!

虽然我没有使用我的解决方案来解决与您完全一样的问题,但我怀疑有一种比使用重写更简单、更干净的方法。 (注意:以下假设内部 DNS 将您的一台服务器识别为 IP 以将所有子域解析为。如果不是这种情况,那么可能应该进行此更改...我不知道如果它会发生什么没有,但我也从未设置过反向代理......)

请尝试以下操作: -在 httpd.conf @ 末尾验证是否出现以下行:

NameVirtualHost *:80

- 最后为每个子域添加一个 VirtualHost,如下所示:

<VirtualHost *:80>
ServerName sub1.example.com
ServerAlias sub1
DocumentRoot "X:/path/to/website/for/internal.example.com"
</VirtualHost>

*重要提示:您可能只能使用一个虚拟主机条目。 为此,请尝试以下操作:

<VirtualHost *:80>
ServerName internal.example.com
ServerAlias sub1.example.com
ServerAlias sub2.example.com
ServerAlias sub3.example.com
DocumentRoot "X:/path/to/website/for/internal.example.com"
</VirtualHost>

非常重要的注意事项:这可能与 SSL(端口 443)的工作方式完全相同。 我不知道,因为我还没有对虚拟主机和 SSL 做太多事情。 In order to properly setup SSL using this method read the following: http://httpd.apache.org/docs/2.2/ssl/ssl_faq.html#vhosts2 ( summary , sometimes doing the above, [that is doing everything the same but使用端口 443 而不是 80],将起作用,但根据某些因素,您可能还只想进行 NamedVirtualHost 192.168.1.1:443 以及可能的其他配置更改,如文章中所述)。

希望这可以帮助!

问题是重定向会触发来自客户端浏览器的新请求。 所以他要求sub2.example.com和 DMZ 反向代理不明白这一点。

也许它可以在没有[R=...]的情况下工作,但我什至不确定,因为它仍然可能触发请求。 当然,它不再是重定向。

由于反向代理是你的前端接口,你需要他理解sub2.xxx否则它不起作用。

暂无
暂无

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

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