如何使用Nginx通过FastCGI发送所有请求

时间:2014-07-11 18:58:57

标签: php nginx fastcgi

目前,我的Nginx服务器已设置为接受www.example.com/filewww.example.com/file.php加载file.php。但是,因为Nginx设置如下所示:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    // More setup stuff
}

通过FastCGI将文件作为PHP处理的唯一方法是,如果URI以.php结尾,否则,它只会下载文件。我如何设置它以便所有PHP文件都通过FastCGI处理,无论URI如何? (无论如何,我确信这样更安全,所以人们无法查看我的PHP代码。)

1 个答案:

答案 0 :(得分:1)

虽然我认为通过单个php文件(即index.php)处理不以.php结尾的请求路径会更好,但你可以尝试这样的事情:

location / {
    root            /path/to/htdocs;
    index           index.php;
    if (-e $request_filename.php) {
          rewrite   ^(.*)?$ /$1.php last;
          break;
    }
    # rewrite requests without matching file index.php (request in: $_GET['req'])
    if (!-e $request_filename) {
          rewrite   ^(.*)?$ /index.php?req=$1 last;
          break;
    }
}

location ~ \.php$ {
   root             /path/to/htdocs;
   fastcgi_pass     unix:/var/www/php-fpm/nginx.sock;
   fastcgi_index    index.php;
   include          fastcgi_params;
   fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
   ..
}