PHP5的CGI方式的一大优势是内置了FastCGI的支持,只需指明绑定的地址和端口参数便可以以FastCGI的方式运行,如下:
php-cgi -b 127.0.0.1:9000
配置Nginx的PHP FastCGI
请将以下内容保存为fastcgi_params文件,保存于/usr/local/nginx/conf下(Ubuntu可保存于/etc/nginx下),他为我们的FastCGI模块设置了基本的环境变量:
#fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;#PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
请特别注意”fastcgi_script_name”一行,PHP-CGI特别需要此行信息来确定PHP文件的位置。
另外需要在PHP-CGI的配置文件(Ubuntu 上此配置文件位于/etc/php5/cgi/php.ini)中,打开cgi.fix_pathinfo选项:
cgi.fix_pathinfo=1;
这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。
接下来在nginx的配置中针对php文件配置其利用FastCGI进程来执行:
server {
index index.php;
root/usr/local/nginx/html;
location~ .*.php$ {
include/usr/local/nginx/conf/fastcgi_params; #请根据自己保存的路径进行设置
fastcgi_index index.php;
fastcgi_pass127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置
}
}