用Nginx的try_files将所有对不存在文件的请求重写到index.php


Rewrite all requests for non existing files to index.php with try_files with Nginx

我正在尝试将琐碎的htaccess文件转换为Nginx,但无法使其工作。返回404错误。以下是htaccess的内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

这是我当前的nginx配置:

server {
listen 80;
server_name domain.biz;
root /var/www/domain.biz;
charset utf-8;
autoindex off;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ '.php$ {
include        fastcgi_params;
fastcgi_pass   unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}
  1. 你直接调用的其他php文件怎么样?例如,一个只有的info.php

    phpinfo();

    里面?

    我问这个问题是因为您的服务器conf似乎使用了try_files,但我不确定您提供的php脚本是否正确。

  2. ?你的fastcgi游泳池在听那只袜子吗??例如,你确定它没有在端口9000中侦听吗?无论如何,我更喜欢在http部分定义上游,然后在服务器部分中使用它

    http {
        upstream phpbackend {
            server unix:/var/run/php5-fpm.sock;
        }
        ...
    
        server {
            ...
            location ~ '.php$ {
                include       fastcgi_params;
                fastcgi_pass  phpbackend;
                fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
            }    
        }
    }
    
  3. 你确定php.ini的cgi.fix_pathinfo设置为false吗?