【问题标题】:Creating a reverse proxy创建反向代理
【发布时间】:2011-05-24 23:10:56
【问题描述】:

如何创建反向代理?我想创建一个使用带有每个 url 标识符的 url 表的站点。如果您转到http://site.com/id,其中 id 是 url 的标识符,它将充当反向代理并从 url 中检索内容。

我研究过使用 apache 反向代理功能,但似乎没有一种方法可以在每次需要对新站点进行反向代理时不重新启动服务器来扩展它。

【问题讨论】:

  • 添加规则时是否有不想重启代理服务器的特殊原因?
  • 我计划他们会即时添加许多新规则,因此每次重新启动服务器似乎都不切实际。

标签: apache reverse-proxy


【解决方案1】:

使用 Apache 创建反向代理使用 mod_rewrite 在主 httpd.conf (如果适用,在相关下):

RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/id$
RewriteRule ^.*$ http://www.proxydomain.com/newUrl [P,L]

第一行检查www.mydomain.commydomain.com

第二行标识 URL 的 /id 部分

如果满足前两个条件,则 Apache 向www.proxydomain.com/newUrl 发出请求,然后返回给客户端。

您不必完全重新启动 Apache 即可进行这些更改。进行配置检查,然后正常重启,这不会使服务器脱机并重新加载配置:

/etc/init.d/httpd configtest && /etc/init.d/httpd graceful

或者,此代码可以进入.htaccess 文件,即less preferable

【讨论】:

  • @sarnold 是的,但它仍然会关闭服务器,因此如果您遇到错误,则在修复配置之前无法再次启动它 - 链接命令可以防止这种情况httpd.apache.org/docs/2.2/stopping.html#graceful
  • @Andy,很有趣,这与apachectl(8) manpage 直接矛盾:) This command automatically checks the configuration files via apache2ctl configtest before initiating the restart to to catch the most obvious errors. However, it is still possible for the daemon to die because of problems with the configuration. 我想知道哪个是正确的? :)
  • (哈,to to catch the most obvious errors,可惜没有类似的工具供手册页作者使用。:)
  • @sarnold 我怀疑这种行为多年来已经发生了变化,因为我以前被咬过,但是对 2.2 的粗略测试给出了apachectl: Configuration syntax error, will not run "graceful"
  • 但这假设我总是计划重定向到proxydomain.com 上的某个页面。我计划重定向到许多不同的页面。
猜你喜欢
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多