Apache-2.4

Apache2 代理 fastcgi 條件重寫與代理通行證匹配和 cookie

  • July 3, 2018

我目前有兩個項目:

  1. /home/piotrek/Vhosts/sf.local/web/app_dev.php
  2. /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 時如何將其與另一個站點結合使用?

好的,所以我想通了。

虛擬主機應如下所示:

<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/時,

  1. 請求被重寫為sf.local/web/app_dev.php
  2. ProxyPassMatch處理對舊主題目錄的請求fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf.local/web/app_dev.php
  3. cookie 已設置
  4. OLD THEME被陳列

刷新後

  1. 請求被重寫為sf2.local/web/app_dev.php
  2. ProxyPassMatch處理對新主題目錄的請求fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf2.local/web/app_dev.php
  3. 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

引用自:https://serverfault.com/questions/918557