0

目前我有这段代码:

if (!-e $request_filename)
{
    rewrite ^/(.*)$ /index.php?/$1 last;
    break;
}

这适用于以下情况: example.com/foo重定向到index.php

然而example.com/foo?bar不起作用。你如何让它发挥作用?

FWIW:我在 Apache 的 mod_rewrite 等效项中没有遇到这个问题。基本上,我将一个可以工作的站点从 Apache 移到了 Nginx。现在我遇到了这个问题。

编辑:

要清楚这就是我要做的事情:

  • example.com/foo
  • example.com/foo/bar/etc
  • example.com/foo?bar
  • example.com/foo?bar=quz

应该全部index.php“静默”服务而不更改浏览器地址栏的 URL。

4

1 回答 1

1

我刚刚使用以下配置对其进行了测试,我相信这确实需要您想要的:

server {
#listen   80; ## listen for ipv4; this line is default and implied
#listen   [::]:80 default ipv6only=on; ## listen for ipv6

root /home/www/test;
index index.php index.html index.htm;

# Make site accessible from 
server_name test.myhost.nl;

location / {        
    # First attempt to serve request as file, then as directory, then fall back to index.php
    try_files $uri $uri/ /index.php?$args;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
    try_files $uri = 404;

    # Fix for server variables that behave differently under nginx/php-fpm than typically expected
    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;

    # Override the SCRIPT_FILENAME variable set by fastcgi_params
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    # Pass to upstream PHP-FPM; This must match whater you name your upstream connection
    #fastcgi_pass phpfpm;
    fastcgi_pass 127.0.0.1:9000;
}
}
于 2012-10-17T16:40:02.377 回答