@dungan
2018-09-22T07:00:57.000000Z
字数 11108
阅读 97
Nginx
什么是 FastCGI
由于传统的 CGI 接口方式的主要缺点是性能差,因为每次 HTTP 服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给 HTTP 服务器。这在处理高并发访问时,几乎不可用,所以后来出现了 FastCGI!
FastCGI(Fast Common Gateway Interface,快速通用网关接口),是一个可伸缩的、高速地在HTTP server和动态脚本语言间通信的接口,被多数脚本支持;FastCGI 的主要优点是把 HTTP 服务器和脚本解析器分开,二者之间通过 FastCGI 通信,解决了 CGI 并发重复 Fork 的问题并实现了 php 脚本的平滑重启;FastCGI 接口方式采用 c/s 结构,分为客户端(HTTP 服务器)和服务器端(脚本语言解析器), HTTP 服务器通过(例如 fastcgi_pass) FastCGI 客户端和动态语言的 FastCGI 服务端通信(例如php-fpm)!
nginx + FastCGI 运行原理

Nginx不支持对外部程序的直接调用或者解析,所有的程序(包括PHP)必须通过FastCGI、uwsig接口调用。FastCGI接口调用在Linux下的socket(unix sockt或者ip:port 形式)。为了调用CGI程序,还需要一个FastCGI的wrapper(可以理解为用户启动另一个程序的程序),wrapper绑定在某个固定的socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket时,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本,并读取返回数据,接着wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx。最后Nginx将返回的
php-fpm 是 php 用于支持 FastCGI 接口的进程管理器,它是作为 php 的一个扩展存在的,因此在安装的时候需要和PHP源码一起编译!
第一步 :拷贝 php 配置文件
cp php-fpm.conf.default php-fpm.confcp www.conf.default www.conf
第二步:配置 php.ini 和 php-fpm
php.ini 去掉注释 :1. ;cgi.force_redirect = 1php-fpm.conf 去掉注释 :1. ;pid = run/php-fpm.pid2. ;error_log = log/php-fpm.log3. ;log_level = notice4. ;listen.owner = www5. ;listen.group = www6. ;listen.mode = 06607. 修改 user = www group = www
第三步:nginx.conf 中去掉注释
#user nobody; # 改为 user www www;#location ~ \.php$ {# root html; # 这行可以删掉,root 可以在 server 区块统一配置# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}
测试
[root@VM_0_17_centos default]# echo "hello php" > test.php[root@VM_0_17_centos default]# curl localhost/test.phphello php# 这样 nginx 就成功集成 php 了
也许你觉得这样配置比较繁琐,你想下载下来傻瓜式的直接用,那么已经有人提供了这种傻瓜式的安装脚本 oneinstck,它为你提供了已经优化过的 lnmp 环境!
一般一个简单的 FastCGI 的配置如下 :
http {...fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;fastcgi_intercept_errors on;...}
fastcgi_buffer_size语法:fastcgi_buffer_size the_size ;默认值:fastcgi_buffer_size 4k/8k ;放置区块:http, server, location ;这个参数指定将用多大的缓冲区来读取从FastCGI服务器到来应答的第一部分,通常来说在这个部分中包含一个小的应答头;默认的缓冲区大小为 fastcgi_buffers 指令中的每块大小,可以将这个值设置更小!
fastcgi_buffers语法:fastcgi_buffers the_number is_size;默认值:fastcgi_buffers 8 4k/8k;放置区块:http, server, location这个参数指定了从FastCGI服务器到来的应答,本地将用多少和多大的缓冲区读取,默认这个参数等于分页大小,根据环境的不同可能是4K, 8K或16K!
fastcgi_cache语法:fastcgi_cache zone|off;默认值:off;放置区块:http, server, location ;为缓存实际使用的共享内存指定一个区域,相同的区域可以用在不同的地方!
fastcgi_cache_key语法:fastcgi_cache_key line默认值:none放置区块:http, server, location设置缓存的关键字,如:fastcgi_cache_key localhost:9000$request_uri;
fastcgi_cache_path语法:fastcgi_cache_path path [levels=m:n] keys_zone=name:size [inactive=time] [max_size=size]默认值:none放置区块:http这个指令指定FastCGI缓存的路径以及其他的一些参数,所有的数据以文件的形式存储,缓存的关键字(key)和文件名为代理的url计算出的MD5值。Level参数设置缓存目录的目录分级以及子目录的数量,例如指令如果设置为:fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;那么数据文件将存储为:/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c缓存中的文件首先被写入一个临时文件并且随后被移动到缓存目录的最后位置,另外,所有活动的关键字及数据相关信息都存储于共享内存池,这个值的名称和大小通过key_zone参数指定,inactive参数指定了内存中的数据存储时间,默认为10分钟。max_size参数设置缓存的最大值,一个指定的cache manager进程将周期性的删除旧的缓存数据。
fastcgi_cache_methods语法:fastcgi_cache_methods [GET HEAD POST];默认值:fastcgi_cache_methods GET HEAD;放置区块:main,http,location;无法禁用GET/HEAD ,即使你只是这样设置:fastcgi_cache_methods POST;
fastcgi_cache_min_uses语法:fastcgi_cache_min_uses n;默认值:fastcgi_cache_min_uses 1;放置区块:http, server, location;指令指定了经过多少次请求的相同URL将被缓存;
fastcgi_cache_use_stale语法:fastcgi_cache_use_stale [updating|error|timeout|invalid_header|http_500]默认值:fastcgi_cache_use_stale off;放置区块:http, server, location ;用在某些网关错误、超时的情况下,nginx都将传送过期的缓存数据;
fastcgi_cache_valid语法:fastcgi_cache_valid [http_error_code|time]默认值:none;放置区块:http, server, location ;为指定的http返回代码指定缓存时间,例如:fastcgi_cache_valid 200 302 404 301 10m;默认情况下缓存只处理200,301,302的状态。同样也可以在指令中使用any表示任何一个:fastcgi_cache_valid 200 302 10m;fastcgi_cache_valid 301 1h;fastcgi_cache_valid any 1m;
fastcgi_connect_timeout语法:fastcgi_connect_timeout time默认值:fastcgi_connect_timeout 60;放置区块:http, server, location;指定同FastCGI服务器的连接超时时间,这个值不能超过75秒;
fastcgi_index语法:fastcgi_index file默认值:none;放置区块:http, server, location;如果URI以斜线结尾,文件名将追加到URI后面,这个值将存储在变量$fastcgi_script_name中。例如:fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;请求"/page.php"的参数SCRIPT_FILENAME将被设置为"/home/www/scripts/php/page.php",但是"/"为"/home/www/scripts/php/index.php";
fastcgi_hide_header语法:fastcgi_hide_header name放置区块:http, server, location;默认情况下nginx不会将来自FastCGI服务器的"Status"和"X-Accel-..."头传送到客户端,这个参数也可以隐藏某些其它的头。如果必须传递"Status"和"X-Accel-..."头,则必须使用fastcgi_pass_header强制其传送到客户端。
fastcgi_ignore_client_abort语法:fastcgi_ignore_client_abort on|off默认值:fastcgi_ignore_client_abort off;放置区块:http, server, location;如果当前连接请求FastCGI服务器失败,为防止其与nginx服务器断开连接,可以用这个指令;
fastcgi_ignore_headers语法:fastcgi_ignore_headers name [name...]放置区块:http, server, location;这个指令禁止处理一些FastCGI服务器应答的头部字段,比如可以指定像"X-Accel-Redirect", "X-Accel-Expires", "Expires"或"Cache-Control"等;
fastcgi_intercept_errors语法:fastcgi_intercept_errors on|off;默认值:fastcgi_intercept_errors off;放置区块:http, server, location;这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。你必须明确的在error_page中指定处理方法使这个参数有效,如果没有适当的处理方法,nginx不会拦截一个错误,这个错误不会显示自己的默认页面,这里允许通过某些方法拦截错误;
fastcgi_max_temp_file_size语法:fastcgi_max_temp_file_size 0默认值:fastcgi_max_temp_file_size 1024m;放置区块:http, server, location;当来自 FastCGI 服务器的响应数据无法全部存储在 buffer 中时,就会将一部分数据存在临时文件中,该指令用于设置临时文件的大小;
fastcgi_temp_path语法:fastcgi_temp_path path [level1 [level2 [level3]]]默认值:fastcgi_temp_path fastcgi_temp;放置区块:http, server, location;定义用于存储临时文件的目录,其中包含从FastCGI服务器接收的数据,在指定目录下最多可以使用三级子目录层次结构;例如,在下列配置中:fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;生成的临时文件目录层级类似如下:/spool/nginx/fastcgi_temp/7/45/00000123457
fastcgi_no_cache语法:fastcgi_no_cache variable [...]默认值:None;放置区块:http, server, location;设置哪些响应数据不被缓存,例如:fastcgi_no_cache $cookie_nocache $arg_nocache$arg_comment;fastcgi_no_cache $http_pragma $http_authorization;
fastcgi_next_upstream语法:fastcgi_next_upstream error|timeout|invalid_header|http_500|http_503|http_404|off默认值:fastcgi_next_upstream error timeout;放置区块:http, server, location;指定哪种情况请求将被转发到下一个FastCGI服务器:- error — 传送中的请求或者正在读取应答头的请求在连接服务器的时候发生错误。- timeout — 传送中的请求或者正在读取应答头的请求在连接服务器的时候超时。- invalid_header — 服务器返回空的或者无效的应答。- http_500 — 服务器返回500应答代码。- http_503 — 服务器返回503应答代码。- http_404 — 服务器返回404应答代码。- off — 禁止请求传送到下一个FastCGI服务器。注意传送请求在传送到下一个服务器之前可能已经将空的数据传送到了客户端,所以,如果在数据传送中有错误或者超时发生,这个指令可能无法修复一些传送错误;
fastcgi_pass语法:fastcgi_pass fastcgi-server默认值:none;放置区块:http, server, location;指定 FastCGI 服务器监听端口与地址,可以是本机或者其它:fastcgi_pass localhost:9000;还可以使用 Unix socket:fastcgi_pass unix:/tmp/fastcgi.socket;同样可以使用一个upstream字段名称:http {...upstream backend {server localhost:1234;}fastcgi_pass backend;...}
fastcgi_pass_header语法:fastcgi_pass_header name默认值:none放置区块:http, server, location;将FastCGI服务器上禁用的header头字段(Status 和 X-Accel-...)传递给客户端;。
fastcgi_read_timeout语法:fastcgi_read_timeout time默认值:fastcgi_read_timeout 60放置区块:http, server, location定义从FastCGI服务器读取响应的超时,该超时指的是仅在两个连续的读操作之间的超时,而不是为整个响应的传输,如果FastCGI服务器在此时间内未传输任何内容,则关闭连接;
fastcgi_send_timeout语法:fastcgi_send_timeout time;默认值:fastcgi_send_timeout 60;放置区块:http, server, location;设置将请求传输到FastCGI服务器的超时,该超时指的是两个连续的写操作之间设置超时,而不是为整个请求的传输,如果FastCGI服务器在此时间内未收到任何内容,则关闭连接;
fastcgi_split_path_info语法:fastcgi_split_path_info regex ;放置区块:location;用来定义一个捕获 $fastcgi_path_info 变量值的正则表达式,正则表达式应该有两个捕获:第一个为 $fastcgi_script_name 变量的值,第二个为 $fastcgi_path_info 变量的值;location ~ ^(.+\.php)(.*)$ {...fastcgi_split_path_info ^(.+\.php)(.*)$;fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;...}在 /show.php/article/0001 请求中, SCRIPT_FILENAME 参数将等于 /path/to/php/show.php, PATH_INFO 参数将等于 /article/0001 ;
fastcgi_store语法:fastcgi_store [on | off | path]默认值:fastcgi_store off;放置区块:http, server, location;用于保存客户端的文件,值为 on 将使用和 root 或 alias 指令相同的路径,值为 off 则禁止存储,还可以使用变量来设置路径名:fastcgi_store /data/www$original_uri;该指令可用于创建静态文件的本地副本,例如:location /images/ {root /data/www;error_page 404 = /fetch$uri;}location /fetch {internal;fastcgi_pass fastcgi://backend;fastcgi_store on;fastcgi_store_access user:rw group:rw all:r;fastcgi_temp_path /data/temp;alias /data/www;}fastcgi_store 并不是缓存,某些需求下它更像是一个镜像;
fastcgi_store_access语法:fastcgi_store_access users:permissions [users:permission ...]默认值:fastcgi_store_access user:rw;放置区块:http, server, location;这个参数指定创建文件或目录的权限,例如:fastcgi_store_access user:rw group:rw all:r;如果要指定一个组的人的相关权限,可以不写用户,如:fastcgi_store_access group:rw all:r;
fastcgi_param语法:fastcgi_param parameter value默认值:none;放置区块:http, server, location;指定一些传递到FastCGI服务器的参数,可以使用字符串,变量,或者其组合,这里的设置不会继承到其他的字段,设置在当前字段会清除掉任何之前的定义;这些参数通常以环境变量的形式取得,例如,"User-agent"头以HTTP_USER_AGENT参数传递,除此之外还有一些其他的http头,都可以用fastcgi_param指令自由传递;下面是一个PHP需要使用的最少参数:fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;PHP使用SCRIPT_FILENAME参数决定需要执行哪个脚本,QUERY_STRING包含请求中的某些参数;如果要处理POST请求,则需要另外增加三个参数:fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;如果PHP在编译时带有--enable-force-cgi-redirect,则必须传递值为200的REDIRECT_STATUS参数:fastcgi_param REDIRECT_STATUS 200;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123fastcgi_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; #客户端IPfastcgi_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_namefastcgi_param PATH_INFO $path_info; #可自定义变量
第一步:安装 php 依赖库
#安装make,已安装,可省略yum -y install gcc automake autoconf libtool make#安装gcc g++ glibc库yum -y install gcc gcc-c++ glibc#安装所需的包$yum -y install libmcrypt-devel mhash-devel libxslt-devellibjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-develzlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-develncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-develkrb5 krb5-devel libidn libidn-devel openssl openssl-devel
第二步:编译安装 php
#下载源码wget http://am1.php.net/distributions/php-7.2.2.tar.gz#解压tar -zxvf php-7.2.2.tar.gz && cd php-7.2.2# 生成 Makefile 文件./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-curl --with-freetype-dir --with-gd \--with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex \--with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 \--with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex \--enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm \--enable-xml --enable-zip --with-ldap# 编译并安装make && make install