2012-07-17 108 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 
}