重写 nginx 的 try_files

重写 nginx 的 try_files

我正在尝试将一个非常简单的重写从以前的 nginx 版本转换为当前版本,如下所示:

location / {
   if ( !-f $request_filename ) {
     rewrite ^/([a-z]*)$ /index.php?action=$1;
     rewrite ^/([a-z]*)/(.*)$ /index.php?action=$1&item=$2;
   }
}

这就是我所取得的进展。索引页出现了,但任何应该像上面那样重写的页面都会抛出 404:

server {

    listen 80 default;
    root /var/www;
    index index.php;
    server_name _;

    location / {
        try_files $uri $uri/ /index.php?action=$uri&item=$args;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

我在这里做错了什么?

答案1

这应该有效:

location / { 
    try_files $uri $uri/ @rules; 
} 

location @rules { 
    rewrite ^/([a-z]*)$ /index.php?action=$1; 
    rewrite ^/([a-z]*)/(.*)$ /index.php?action=$1&item=$2; 
}

location /index.php { 
    fastcgi_pass 127.0.0.1:9000; 
    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
}

相关内容