2012-07-17 145 views
0

嗨 - 我一直在努力這個好幾天。這看起來很簡單,但我無法完成。需要幫助翻譯這個htaccess重寫規則到Nginx

我有一個在CakePHP開發的網站。有一個腳本可以響應/css/profiles/g/whatever.css(「無論是什麼」,它實際上是一個傳遞給動作的參數),它響應生成的CSS並將其保存到/css/profiles/whatever.css

我在Apache的一個規則,需要請求/css/profiles/whatever.css,如果它不存在,重寫請求/css/profiles/g/whatever.css不會重新導向,讓客戶永遠不會注意到它是由一個腳本,該文件並沒有迴應存在。

這是我在阿帕奇:

# Profile CSS rules 
RewriteCond %{REQUEST_URI} ^/css/profiles/ 
RewriteCond %{REQUEST_URI} !/css/profiles/g/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^css/profiles/(.*)$ /css/profiles/g/$1 [L] 

# CakePHP's default rules 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php/$1 [QSA,L] 

現在我的網站移動到nginx的服務器,到目前爲止,我有這樣的:

# Profile CSS rules 
location ~ ^/css/profiles/(?!g/)(.*)$ { 
   if (!-f $request_filename) { 
     rewrite ^/css/profiles/(.*)$ /css/profiles/g/$1 last; 
     break; 
   } 

}

# CakePHP's default rules 
location/{ 

try_files $ uri $ uri//index.php?$uri & $ args; }

的條件似乎是工作,因爲如果我去/css/profiles/whatever.css並打印出PHP的$_SERVER變量它給了我

[QUERY_STRING] => /css/profiles/g/whatever.css& 

通知的&。這意味着它到達try_files部分,並將$uri添加到查詢字符串中,並且它具有正確的$uri

但是......

[REQUEST_URI] => /css/profiles/whatever.css 

這就是毛刺。看起來這並沒有真正改變$request_uri這是CakePHP需要控制什麼控制器。

更新:該REQUEST_URI值是正確的......這裏的問題是,蛋糕看起來不同的服務器變量的值來決定哪個控制器將作出迴應。按此順序:$_SERVER['PATH_INFO'],$_SERVER['REQUEST_URI']$_SERVER['PHP_SELF']$_SERVER['SCRIPT_NAME']的組合,最後是$_SERVER['HTTP_X_REWRITE_URL']。這就是失敗的原因。

任何幫助將不勝感激。

謝謝。

注意:我昨天發佈this question on Serverfult因爲我認爲它適合那裏但是沒有得到答案,這就是爲什麼我也在這裏發佈它。

回答

1

所以我終於得到它的工作:

location ~ ^/css/profiles/(?!g/)(.*)$ { 
  set $new_uri /css/profiles/g/$1; 
  if (!-f $request_filename) { 
    rewrite ^/css/profiles/(.*)$ /css/profiles/g/$1 last; 
    } 
} 

...並在最後:

location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php; 
    include fastcgi_params; 

    ... some other stuff were here related to fastcgi 
    fastcgi_param PATH_INFO $new_uri; # <--- I added this 
}