2012-11-18 214 views
0

我有一个htaccess文件我想转换成nignx配置文件。在Nginx的htaccess重写规则:设置重写路径

这是我的htaccess文件。

RewriteEngine on 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule !\.(jpg|css|js|gif|png)$ public/ [L] 
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1 

和我有规则,我的nginx的配置文件:

location/{ 
if ($request_uri !~ "-f"){ 
     rewrite !\.(jpg|css|js|gif|png)$ public/ break; 
} 
rewrite !\.(jpg|css|js|gif|png)$ public/index.php?url=$1; 
} 

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
location ~ \.php$ { 
     # Move to the @missing part when the file doesn't exist 
     try_files $uri @missing; 

     # Fix for server variables that behave differently under nginx/$ 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     # Include the standard fastcgi_params file included with ngingx 
     include fastcgi_params; 
     fastcgi_param PATH_INFO $fastcgi_path_info; 
     fastcgi_index index.php; 


     # Pass to upstream PHP-FPM; This must match whater you name you$ 
     #fastcgi_pass phpfpm; 
     fastcgi_pass 127.0.0.1:9000; 
} 

location @missing { 
     rewrite ^(.*)$ public/index.php?url=$1 break; 
} 

然而,当我打/,我得到一个禁止的403,但是我能到/public/index.php,因此重写不起作用。

关于我在做什么错的任何想法?

回答

0

if ($request_uri !~ "-f") -statement没有解析你想在nginx中使用的方式。你并没有检查文件的存在,而是针对否定正则表达式-f。要在检查文件是否存在,如果在使用nginx if (-f $request_filename)

看到http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if为Nginx的全部细节if声明

一般要取代普通的.htaccess节:

RewriteEngine on 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule <regex> <target> 

与nginx相当于:

location ~* <regex> { try_files $uri $uri/ <target>;} 

对于你说的位你会得到一个嵌套的位置,如下所示:

location/{ 
    location ~* \.(jpg|css|js|gif|png)$ { try_files $uri $uri/ /public/; } 
    location !~* (.*)\.(jpg|css|js|gif|png)$ { 
    try_files $uri $uri/ /public/index.php?url=$1; 
    } 
}