# TCP形式传递
fastcgi_pass localhost:9000;
# Socket形式传递
fastcgi_pass unix:/tmp/fastcgi.socket;
# 传递给集群
upstream cloud {
server cgi_1.cloud.com;
server cgi_2.cloud.com;
}
fastcgi_pass cloud;
fastcgi_param
作用域:http, server, location
设置一个传递给FastCGI服务的参数,可以是文本或者是变量。
# 例如在接入层Nginx上面传递如下5个参数
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;
# 那么在FastCGI上面,例如PHP-CGI上面就可以通过$_SERVER这个超全局变量获取。
$_SERVER[‘REMOTE_ADDR‘]
$_SERVER[‘REMOTE_PORT‘]
$_SERVER[‘SERVER_ADDR‘]
$_SERVER[‘SERVER_PORT‘]
$_SERVER[‘SERVER_NAME‘]
fastcgi_param 文件详解
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径,也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白
fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。
fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
fastcgi_param GATEWAY_INTERFACE CGI/1.1; #cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; #nginx 版本号,可修改、隐藏
fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
fastcgi_param REMOTE_PORT $remote_port; #客户端端口
fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
fastcgi_param SERVER_PORT $server_port; #服务器端口
fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name
fastcgi_param PATH_INFO $path_info; #可自定义变量
-- PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_index
作用域:http, server, location
当请求以/结尾的时候,会将请求传递给所设置的index.php文件处理。
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_split_path_info
作用域:location
Nginx默认获取不到PATH_INFO的值,得通过fastcgi_split_path_info指定定义的正则表达式来给$fastcgi_path_info赋值。
目的:让php能够解析类似这样的url http://www.xxx.com/index.php/abc/def
在默认情况下我们打开这个url时会出现无法找到该页。这就需要在nginx做path_info设置了。
原理:把index.php做为php执行的脚本,把/abc/def做为参数传给php-cgi执行。
实现:nginx版本0.7.31以上支持fastcgi_split_path_info,这个指令可以设置SCRIPT_FILENAME和PATH_INFO的变量,用正则表达式将这两部分分开.
例子:我们在nginx配置文件"local"区块中加入以下代码。
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
重启nginx服务
/usr/local/nginx/sbin/nginx -s reload
当nginx处理http://www.xxx.com/index.php/abc/def请求时,将会把"index.php"做为php的脚本,/abc/def做为index.php脚本的参数提交给php-cgi执行。