kankacan一、FastCGI特點:
1、HTTP服務器和動態腳本語言間通信的接口或工具
2、可把動態語言解析和HTTP服務器分離I
3、Nginx、Apache、Lighttpd,以及多數動態語言 都支持FastCGI
4、FastCGI接口方式採用 C/S結構,分爲客戶端(HTTP服務器)和服務器端(動態語言解析服務器)
5、PHP動態語言服務器可以啓動多個FastCGI的守護進程(例如php-fpm(fcgi process mangement))
6、HTTP服務器通過 FastCGI客戶端(Nginx fastcgi_pass)和 動態語言FastCGI服務器(php-fpm) 通信
二、FastCGI原理:
FastCGI接口在Linux下是socket。wrapper 用於啓動另一個程序,綁定在某個固定的socket上,如端口或文件socket。調用CGI程序
1、Nginx將CGI請求發送給綁定有wrapper的socket時,通過FastCGI接口,wrapper接收到請求,然後派生出一個新的線程,這個線程調用解釋器 或者 外部程序 處理腳本 來讀取返回的數據。
2、wrapper再將返回的數據通過FastCGI接口,沿着固定的socket傳給Nginx
3、Nginx將返回的數據發送給客戶端n
三、nginx與php-fpm兩種通信方式
unix socket方式要比tcp的方式快,而且消耗資源少,因爲socket之間在nginx和php-fpm的進程之間通信,而tcp需要經過本地迴環驅動,還要申請臨時端口和tcp相關資源。
unix socket會顯得不是那麼穩定,當併發連接數爆發時,會產生大量的長時緩存,在沒有面向連接協議支撐的情況下,大數據包很有可能就直接出錯並不會返回異常。而TCP這樣的面向連接的協議,多少可以保證通信的正確性和完整性。
1、tcp方式
#PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
tips:diff fastcgi.conf fastcgi_params
fastcgi.conf多了一行fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
2、unix socket方式
location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/nginx-default$fastcgi_script_name;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
tips:修改nginx配置文件location ~ \.php$ 區段之後,記得修改/etc/php5/fpm/pool.d/www.conf 的 listen = 127.0.0.1:9000 或者 listen = unix:/var/run/php5-fpm.sock
參考文檔:
oldboy blog
http://xieminis.me/?p=216