![Apache2 代理 fastcgi 条件重写,带有代理传递匹配和 cookie](https://linux22.com/image/723743/Apache2%20%E4%BB%A3%E7%90%86%20fastcgi%20%E6%9D%A1%E4%BB%B6%E9%87%8D%E5%86%99%EF%BC%8C%E5%B8%A6%E6%9C%89%E4%BB%A3%E7%90%86%E4%BC%A0%E9%80%92%E5%8C%B9%E9%85%8D%E5%92%8C%20cookie.png)
我目前有两个项目:
- /home/piotrek/Vhosts/sf.local/web/app_dev.php
- /home/piotrek/Vhosts/sf2.local/web/app_dev.php
两者都有相同的 repo 但设置为两个不同的分支。
我有第一个站点的虚拟主机:
<VirtualHost *:80>
ServerName sf.local
ServerAlias www.sf.local
DocumentRoot /home/piotrek/Vhosts/sf.local/web
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf.local/web/$1
DirectoryIndex app_dev.php
<Directory /home/piotrek/Vhosts/sf.local/web>
AllowOverride All
Require all granted
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /home/piotrek/Vhosts/logs/sf.local-error.log
CustomLog /home/piotrek/Vhosts/logs/sf.local-access.log combined
</VirtualHost>
当我请求http://sf.local/一切正常。但现在我想在设置了名称的 cookie 时将人们发送到 /sf2.local/ THEME
。类似这样的事情:
RewriteCond %{HTTP_COOKIE} THEME=new [NC]
RewriteRule ^(.*)$ sf2project
当没有 cookie 时,如何将 mod rewrite 与 fastcgi 代理结合起来为一个站点提供服务,而当设置了 cookie 时,如何为另一个站点提供服务?
答案1
好的,我明白了。
Vhost 看起来应该是这样的:
<VirtualHost *:80>
ServerName sf.local
ServerAlias www.sf.local
DocumentRoot /home/piotrek/Vhosts
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9071/home/piotrek/Vhosts/$1
<Directory /home/piotrek/Vhosts>
AllowOverride None
Require all granted
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
# rewrite if cookie is set to "new"
RewriteCond %{HTTP_COOKIE} THEME=new [NC]
RewriteRule ^(.*)$ sf2.local/web/$1 [QSA,L]
# rewrite to old version
RewriteRule ^(.*)$ sf.local/web/$1 [QSA,L]
</IfModule>
</Directory>
<Directory /home/piotrek/Vhosts/sf.local/web>
<IfModule mod_rewrite.c>
# local rewrite to app_dev.php if file does not exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
</Directory>
<Directory /home/piotrek/Vhosts/sf2.local/web>
<IfModule mod_rewrite.c>
# local rewrite to app_dev.php if file does not exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /home/piotrek/Vhosts/logs/sf.local-error.log
CustomLog /home/piotrek/Vhosts/logs/sf.local-access.log combined
</VirtualHost>
/home/piotrek/Vhosts/sf.local/web/app_dev.php
<?php
setcookie("THEME", "new", time() + 3600);
die('OLD THEME');
/home/piotrek/Vhosts/sf2.local/web/app_dev.php
<?php
die('NEW THEME');
现在,当我进入http://sf.local/第一次,
- 请求重写为
sf.local/web/app_dev.php
ProxyPassMatch
处理对旧主题目录的请求fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf.local/web/app_dev.php
- cookie 已设置
OLD THEME
被展示
刷新后
- 请求重写为
sf2.local/web/app_dev.php
ProxyPassMatch
处理对新主题目录的请求fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf2.local/web/app_dev.php
NEW THEME
被展示
重写也适用于其他文件,例如http://sf.local/robots.txt。没有cookie时,会重写为,/home/piotrek/Vhosts/sf.local/web/robots.txt
有cookie时则重写为/home/piotrek/Vhosts/sf2.local/web/robots.txt