【问题标题】:Apache cookie-based transparent proxy / redirect基于 Apache cookie 的透明代理/重定向
【发布时间】:2013-11-30 23:17:24
【问题描述】:

我有许多站点在同一台机器上运行,由 httpd 提供服务。每个站点都设置为不同子域上的 VirtualHost。此外,对于每个子域,在不同的端口上有两个 VirtualHost:一个用于“稳定”版本,一个用于“测试”版本。稳定版托管在 80 端口上。

登录(稳定版)网站后,如果后端确定用户应该使用测试版,则会设置一个 cookie。

我希望 Apache 在对稳定版本的后续请求中检测此 cookie,并(在用户不知情的情况下)将请求重定向到 beta 版本。

我怎样才能做到这一点?

我已经尝试在 httpd.conf 中使用 RewriteCond / RewriteRule,但它似乎没有任何效果 - 也许 Apache 忽略它而支持匹配的 VirtualHost,或者对排序很敏感(我认为 VirthalHost 定义是被首先包括在内)?也许我应该使用 mod_proxy,无论如何?

配置

我在下面的配置中包含(匿名)sn-ps

httpd.conf

Listen  80
NameVirtualHost *:80
Listen 81
NameVirtualHost *:81

Include "/path/to/checkout/config/[stage]/apache2/*.conf" # VirtualHosts of sites
Include "/path/to/checkout/config/common/apache2/*.conf" # My attempted redirect

config/[stage]/apache2/[site]/apache2/[app-stable].conf

<VirtualHost *:80>
  ServerName [app.hostname]
  ServerAlias [alternative-app-name.hostname]
  DocumentRoot "/var/www-application/release-base/current/app/public"
  <Directory "/var/www-application/release-base/current/app/public/">
     Options Indexes FollowSymLinks
     AllowOverride All
  </Directory>
  <!-- snip -->
</VirtualHost>

config/[stage]/apache2/[site]/apache2/[app-beta].conf

<VirtualHost *:81>
  ServerName [app.hostname]
  ServerAlias [alternative-app-name.hostname]
  DocumentRoot "/var/www-application/beta-release-base/current/app/public"
  <Directory "/var/www-application/beta-release-base/current/app/public/">
     Options Indexes FollowSymLinks
     AllowOverride All
  </Directory>
  <!-- snip -->
</VirtualHost>

config/common/apache2/common.conf

# !!! This has no effect !!!
RewriteEngine On
RewriteCond %{HTTP_COOKIE} cookiename
RewriteCond %{SERVER_PORT} !8081
RewriteRule ^ https://%{HTTP_HOST}:8081%{REQUEST_URI} [R,L] # (R for debugging)

App .htaccess(如果相关)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

【问题讨论】:

    标签: apache mod-rewrite virtualhost


    【解决方案1】:

    你有两个问题:

    1. 忽略全局范围内的重定向;将它们移动到虚拟主机范围
    2. %{HTTP_HOST}”变量包含端口号,因此您将重定向到 http://hostname:80:81/foo(因此您的“挂起”)

    为了使其透明,您需要在 RewriteRule 上使用 [P] 标志,并包含一个 ProxyPassReverse 指令。您的最终指令(在每个“稳定”的 conf 文件中)应如下所示:

    RewriteCond %{HTTP_COOKIE} absVersion
    RewriteRule ^/(.*) http://hostname:81/$1 [P]
    ProxyPassReverse / http://hostname:81/
    

    请注意,这需要启用 mod_proxy(以及任何必要的子模块,例如 mod_proxy_http)。

    【讨论】:

    • 我已经尝试将 cookie cond 和重写规则放在稳定的 vhost 中,但这似乎使请求挂起......
    猜你喜欢
    • 2017-06-21
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多