[关闭]
@dungan 2018-09-22T07:00:57.000000Z 字数 11108 阅读 88

Nginx

Nginx与Fast-CGI

相关概念

什么是 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将返回的

配置 ngixn 支持 php

php-fpm 是 php 用于支持 FastCGI 接口的进程管理器,它是作为 php 的一个扩展存在的,因此在安装的时候需要和PHP源码一起编译!

第一步 :拷贝 php 配置文件

  1. cp php-fpm.conf.default php-fpm.conf
  2. cp www.conf.default www.conf

第二步:配置 php.ini 和 php-fpm

  1. php.ini 去掉注释 :
  2. 1. ;cgi.force_redirect = 1
  3. php-fpm.conf 去掉注释 :
  4. 1. ;pid = run/php-fpm.pid
  5. 2. ;error_log = log/php-fpm.log
  6. 3. ;log_level = notice
  7. 4. ;listen.owner = www
  8. 5. ;listen.group = www
  9. 6. ;listen.mode = 0660
  10. 7. 修改 user = www group = www

第三步:nginx.conf 中去掉注释

  1. #user nobody; # 改为 user www www;
  2. #location ~ \.php$ {
  3. # root html; # 这行可以删掉,root 可以在 server 区块统一配置
  4. # fastcgi_pass 127.0.0.1:9000;
  5. # fastcgi_index index.php;
  6. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  7. # include fastcgi_params;
  8. #}

测试

  1. [root@VM_0_17_centos default]# echo "hello php" > test.php
  2. [root@VM_0_17_centos default]# curl localhost/test.php
  3. hello php
  4. # 这样 nginx 就成功集成 php 了

也许你觉得这样配置比较繁琐,你想下载下来傻瓜式的直接用,那么已经有人提供了这种傻瓜式的安装脚本 oneinstck,它为你提供了已经优化过的 lnmp 环境!

关于 nginx FastCGI 相关的配置参数说明

一般一个简单的 FastCGI 的配置如下 :

  1. http {
  2. ...
  3. fastcgi_connect_timeout 300;
  4. fastcgi_send_timeout 300;
  5. fastcgi_read_timeout 300;
  6. fastcgi_buffer_size 64k;
  7. fastcgi_buffers 4 64k;
  8. fastcgi_busy_buffers_size 128k;
  9. fastcgi_temp_file_write_size 128k;
  10. fastcgi_intercept_errors on;
  11. ...
  12. }

指令

  1. fastcgi_buffer_size
  2. 语法:fastcgi_buffer_size the_size ;
  3. 默认值:fastcgi_buffer_size 4k/8k ;
  4. 放置区块:http, server, location ;
  5. 这个参数指定将用多大的缓冲区来读取从FastCGI服务器到来应答的第一部分,通常来说在这个部分中包含一个小的应答头;
  6. 默认的缓冲区大小为 fastcgi_buffers 指令中的每块大小,可以将这个值设置更小!
  1. fastcgi_buffers
  2. 语法:fastcgi_buffers the_number is_size;
  3. 默认值:fastcgi_buffers 8 4k/8k;
  4. 放置区块:http, server, location
  5. 这个参数指定了从FastCGI服务器到来的应答,本地将用多少和多大的缓冲区读取,默认这个参数等于分页大小,根据环境的不同可能是4K, 8K16K!
  1. fastcgi_cache
  2. 语法:fastcgi_cache zone|off;
  3. 默认值:off;
  4. 放置区块:http, server, location ;
  5. 为缓存实际使用的共享内存指定一个区域,相同的区域可以用在不同的地方!
  1. fastcgi_cache_key
  2. 语法:fastcgi_cache_key line
  3. 默认值:none
  4. 放置区块:http, server, location
  5. 设置缓存的关键字,如:fastcgi_cache_key localhost:9000$request_uri;
  1. fastcgi_cache_path
  2. 语法:fastcgi_cache_path path [levels=m:n] keys_zone=name:size [inactive=time] [max_size=size]
  3. 默认值:none
  4. 放置区块:http
  5. 这个指令指定FastCGI缓存的路径以及其他的一些参数,所有的数据以文件的形式存储,缓存的关键字(key)和文件名为代理的url计算出的MD5值。
  6. Level参数设置缓存目录的目录分级以及子目录的数量,例如指令如果设置为:
  7. fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
  8. 那么数据文件将存储为:
  9. /data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
  10. 缓存中的文件首先被写入一个临时文件并且随后被移动到缓存目录的最后位置,另外,所有活动的关键字及数据相关信息都存储于共享内存池,这个值的名称和大小通过key_zone参数指定,inactive参数指定了内存中的数据存储时间,默认为10分钟。
  11. max_size参数设置缓存的最大值,一个指定的cache manager进程将周期性的删除旧的缓存数据。
  1. fastcgi_cache_methods
  2. 语法:fastcgi_cache_methods [GET HEAD POST];
  3. 默认值:fastcgi_cache_methods GET HEAD;
  4. 放置区块:main,http,location;
  5. 无法禁用GET/HEAD ,即使你只是这样设置:fastcgi_cache_methods POST;
  1. fastcgi_cache_min_uses
  2. 语法:fastcgi_cache_min_uses n;
  3. 默认值:fastcgi_cache_min_uses 1;
  4. 放置区块:http, server, location;
  5. 指令指定了经过多少次请求的相同URL将被缓存;
  1. fastcgi_cache_use_stale
  2. 语法:fastcgi_cache_use_stale [updating|error|timeout|invalid_header|http_500]
  3. 默认值:fastcgi_cache_use_stale off;
  4. 放置区块:http, server, location ;
  5. 用在某些网关错误、超时的情况下,nginx都将传送过期的缓存数据;
  1. fastcgi_cache_valid
  2. 语法:fastcgi_cache_valid [http_error_code|time]
  3. 默认值:none;
  4. 放置区块:http, server, location ;
  5. 为指定的http返回代码指定缓存时间,例如:
  6. fastcgi_cache_valid 200 302 404 301 10m;
  7. 默认情况下缓存只处理200301302的状态。同样也可以在指令中使用any表示任何一个:
  8. fastcgi_cache_valid 200 302 10m;
  9. fastcgi_cache_valid 301 1h;
  10. fastcgi_cache_valid any 1m;
  1. fastcgi_connect_timeout
  2. 语法:fastcgi_connect_timeout time
  3. 默认值:fastcgi_connect_timeout 60;
  4. 放置区块:http, server, location;
  5. 指定同FastCGI服务器的连接超时时间,这个值不能超过75秒;
  1. fastcgi_index
  2. 语法:fastcgi_index file
  3. 默认值:none;
  4. 放置区块:http, server, location;
  5. 如果URI以斜线结尾,文件名将追加到URI后面,这个值将存储在变量$fastcgi_script_name中。例如:
  6. fastcgi_index index.php;
  7. fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
  8. 请求"/page.php"的参数SCRIPT_FILENAME将被设置为"/home/www/scripts/php/page.php",但是"/""/home/www/scripts/php/index.php";
  1. fastcgi_hide_header
  2. 语法:fastcgi_hide_header name
  3. 放置区块:http, server, location;
  4. 默认情况下nginx不会将来自FastCGI服务器的"Status""X-Accel-..."头传送到客户端,这个参数也可以隐藏某些其它的头。
  5. 如果必须传递"Status""X-Accel-..."头,则必须使用fastcgi_pass_header强制其传送到客户端。
  1. fastcgi_ignore_client_abort
  2. 语法:fastcgi_ignore_client_abort on|off
  3. 默认值:fastcgi_ignore_client_abort off;
  4. 放置区块:http, server, location;
  5. 如果当前连接请求FastCGI服务器失败,为防止其与nginx服务器断开连接,可以用这个指令;
  1. fastcgi_ignore_headers
  2. 语法:fastcgi_ignore_headers name [name...]
  3. 放置区块:http, server, location;
  4. 这个指令禁止处理一些FastCGI服务器应答的头部字段,比如可以指定像"X-Accel-Redirect", "X-Accel-Expires", "Expires""Cache-Control"等;
  1. fastcgi_intercept_errors
  2. 语法:fastcgi_intercept_errors on|off;
  3. 默认值:fastcgi_intercept_errors off;
  4. 放置区块:http, server, location;
  5. 这个指令指定是否传递4xx5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。
  6. 你必须明确的在error_page中指定处理方法使这个参数有效,如果没有适当的处理方法,nginx不会拦截一个错误,这个错误不会显示自己的默认页面,这里允许通过某些方法拦截错误;
  1. fastcgi_max_temp_file_size
  2. 语法:fastcgi_max_temp_file_size 0
  3. 默认值:fastcgi_max_temp_file_size 1024m;
  4. 放置区块:http, server, location;
  5. 当来自 FastCGI 服务器的响应数据无法全部存储在 buffer 中时,就会将一部分数据存在临时文件中,该指令用于设置临时文件的大小;
  1. fastcgi_temp_path
  2. 语法:fastcgi_temp_path path [level1 [level2 [level3]]]
  3. 默认值:fastcgi_temp_path fastcgi_temp;
  4. 放置区块:http, server, location;
  5. 定义用于存储临时文件的目录,其中包含从FastCGI服务器接收的数据,在指定目录下最多可以使用三级子目录层次结构;
  6. 例如,在下列配置中:
  7. fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;
  8. 生成的临时文件目录层级类似如下:
  9. /spool/nginx/fastcgi_temp/7/45/00000123457
  1. fastcgi_no_cache
  2. 语法:fastcgi_no_cache variable [...]
  3. 默认值:None
  4. 放置区块:http, server, location;
  5. 设置哪些响应数据不被缓存,例如:
  6. fastcgi_no_cache $cookie_nocache $arg_nocache$arg_comment;
  7. fastcgi_no_cache $http_pragma $http_authorization;
  1. fastcgi_next_upstream
  2. 语法:fastcgi_next_upstream error|timeout|invalid_header|http_500|http_503|http_404|off
  3. 默认值:fastcgi_next_upstream error timeout;
  4. 放置区块:http, server, location;
  5. 指定哪种情况请求将被转发到下一个FastCGI服务器:
  6. - error 传送中的请求或者正在读取应答头的请求在连接服务器的时候发生错误。
  7. - timeout 传送中的请求或者正在读取应答头的请求在连接服务器的时候超时。
  8. - invalid_header 服务器返回空的或者无效的应答。
  9. - http_500 服务器返回500应答代码。
  10. - http_503 服务器返回503应答代码。
  11. - http_404 服务器返回404应答代码。
  12. - off 禁止请求传送到下一个FastCGI服务器。
  13. 注意传送请求在传送到下一个服务器之前可能已经将空的数据传送到了客户端,所以,如果在数据传送中有错误或者超时发生,这个指令可能无法修复一些传送错误;
  1. fastcgi_pass
  2. 语法:fastcgi_pass fastcgi-server
  3. 默认值:none;
  4. 放置区块:http, server, location;
  5. 指定 FastCGI 服务器监听端口与地址,可以是本机或者其它:
  6. fastcgi_pass localhost:9000;
  7. 还可以使用 Unix socket:
  8. fastcgi_pass unix:/tmp/fastcgi.socket;
  9. 同样可以使用一个upstream字段名称:
  10. http {
  11. ...
  12. upstream backend {
  13. server localhost:1234;
  14. }
  15. fastcgi_pass backend;
  16. ...
  17. }
  1. fastcgi_pass_header
  2. 语法:fastcgi_pass_header name
  3. 默认值:none
  4. 放置区块:http, server, location;
  5. FastCGI服务器上禁用的header头字段(Status X-Accel-...)传递给客户端;。
  1. fastcgi_read_timeout
  2. 语法:fastcgi_read_timeout time
  3. 默认值:fastcgi_read_timeout 60
  4. 放置区块:http, server, location
  5. 定义从FastCGI服务器读取响应的超时,该超时指的是仅在两个连续的读操作之间的超时,而不是为整个响应的传输,如果FastCGI服务器在此时间内未传输任何内容,则关闭连接;
  1. fastcgi_send_timeout
  2. 语法:fastcgi_send_timeout time
  3. 默认值:fastcgi_send_timeout 60
  4. 放置区块:http, server, location
  5. 设置将请求传输到FastCGI服务器的超时,该超时指的是两个连续的写操作之间设置超时,而不是为整个请求的传输,如果FastCGI服务器在此时间内未收到任何内容,则关闭连接;
  1. fastcgi_split_path_info
  2. 语法:fastcgi_split_path_info regex ;
  3. 放置区块:location;
  4. 用来定义一个捕获 $fastcgi_path_info 变量值的正则表达式,正则表达式应该有两个捕获:第一个为 $fastcgi_script_name 变量的值,第二个为 $fastcgi_path_info 变量的值;
  5. location ~ ^(.+\.php)(.*)$ {
  6. ...
  7. fastcgi_split_path_info ^(.+\.php)(.*)$;
  8. fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
  9. fastcgi_param PATH_INFO $fastcgi_path_info;
  10. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  11. ...
  12. }
  13. /show.php/article/0001 请求中, SCRIPT_FILENAME 参数将等于 /path/to/php/show.php, PATH_INFO 参数将等于 /article/0001 ;
  1. fastcgi_store
  2. 语法:fastcgi_store [on | off | path]
  3. 默认值:fastcgi_store off;
  4. 放置区块:http, server, location;
  5. 用于保存客户端的文件,值为 on 将使用和 root alias 指令相同的路径,值为 off 则禁止存储,还可以使用变量来设置路径名:
  6. fastcgi_store /data/www$original_uri;
  7. 该指令可用于创建静态文件的本地副本,例如:
  8. location /images/ {
  9. root /data/www;
  10. error_page 404 = /fetch$uri;
  11. }
  12. location /fetch {
  13. internal;
  14. fastcgi_pass fastcgi://backend;
  15. fastcgi_store on;
  16. fastcgi_store_access user:rw group:rw all:r;
  17. fastcgi_temp_path /data/temp;
  18. alias /data/www;
  19. }
  20. fastcgi_store 并不是缓存,某些需求下它更像是一个镜像;
  1. fastcgi_store_access
  2. 语法:fastcgi_store_access users:permissions [users:permission ...]
  3. 默认值:fastcgi_store_access user:rw;
  4. 放置区块:http, server, location;
  5. 这个参数指定创建文件或目录的权限,例如:
  6. fastcgi_store_access user:rw group:rw all:r;
  7. 如果要指定一个组的人的相关权限,可以不写用户,如:
  8. fastcgi_store_access group:rw all:r;
  1. fastcgi_param
  2. 语法:fastcgi_param parameter value
  3. 默认值:none;
  4. 放置区块:http, server, location;
  5. 指定一些传递到FastCGI服务器的参数,可以使用字符串,变量,或者其组合,这里的设置不会继承到其他的字段,设置在当前字段会清除掉任何之前的定义;
  6. 这些参数通常以环境变量的形式取得,例如,"User-agent"头以HTTP_USER_AGENT参数传递,除此之外还有一些其他的http头,都可以用fastcgi_param指令自由传递;
  7. 下面是一个PHP需要使用的最少参数:
  8. fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
  9. fastcgi_param QUERY_STRING $query_string;
  10. PHP使用SCRIPT_FILENAME参数决定需要执行哪个脚本,QUERY_STRING包含请求中的某些参数;
  11. 如果要处理POST请求,则需要另外增加三个参数:
  12. fastcgi_param REQUEST_METHOD $request_method;
  13. fastcgi_param CONTENT_TYPE $content_type;
  14. fastcgi_param CONTENT_LENGTH $content_length;
  15. 如果PHP在编译时带有--enable-force-cgi-redirect,则必须传递值为200REDIRECT_STATUS参数:
  16. fastcgi_param REDIRECT_STATUS 200;

fastcgi_param 指令中的参数说明

  1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径
  2. fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
  3. fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
  4. fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
  5. fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。
  6. fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
  7. fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
  8. fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
  9. fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
  10. fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  11. fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本
  12. fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本号,可修改、隐藏
  13. fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
  14. fastcgi_param REMOTE_PORT $remote_port; #客户端端口
  15. fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
  16. fastcgi_param SERVER_PORT $server_port; #服务器端口
  17. fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name
  18. fastcgi_param PATH_INFO $path_info; #可自定义变量

附 PHP 编译安装

第一步:安装 php 依赖库

  1. #安装make,已安装,可省略
  2. yum -y install gcc automake autoconf libtool make
  3. #安装gcc g++ glibc库
  4. yum -y install gcc gcc-c++ glibc
  5. #安装所需的包
  6. $yum -y install libmcrypt-devel mhash-devel libxslt-devel
  7. libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
  8. zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel
  9. ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel
  10. krb5 krb5-devel libidn libidn-devel openssl openssl-devel

第二步:编译安装 php

  1. #下载源码
  2. wget http://am1.php.net/distributions/php-7.2.2.tar.gz
  3. #解压
  4. tar -zxvf php-7.2.2.tar.gz && cd php-7.2.2
  5. # 生成 Makefile 文件
  6. ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-curl --with-freetype-dir --with-gd \
  7. --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex \
  8. --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 \
  9. --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex \
  10. --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm \
  11. --enable-xml --enable-zip --with-ldap
  12. # 编译并安装
  13. make && make install
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注