ngx_http_fastcgi_module模块

ngx_http_fastcgi_module模块 :ngx_http_fastcgi_module实现通过fastcgi协议(FastCGI服务器)将指定的客户端请求转发给php-fpm处理,不支持php模块方式

 

fastcgi_pass address; #转发请求到后端服务器,address为后端的fastcgi server的地址,(可用位置:location, if in location)
fastcgi_index index.php;; #fastcgi默认的主页资源,
include fastcgi_params;  #调用fastcgi配置文件,此文件存放的是变量。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param parameter value [if_not_empty]; #设置nginx变量传递给FastCGI服务器的参数值,可以是文本,变量或组合,可以于将Nginx的内置变量赋值给自定义key
    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 name


示例1: 
1)在后端服务器先配置fpm server和mariadb-server
2)在前端nginx服务上做以下配置:
location ~* \.php$ {
    root /data/php;         #$document_root 调用root目录
    fastcgi_pass             后端fpm服务器IP:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    #fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name;  #同上,如果SCRIPT_FILENAME是绝对路径,则可以省略"root /data/php";
    include fastcgi_params;  #调用fastcgi配置文件,此文件存放的是变量。
}
 
示例2:通过/pm_status和/ping来获取fpm server状态信息   http://hostname/pm_status
location ~* ^/(fpm_status|ping)$ {
    fastcgi_pass 后端fpm服务器IP:9000;
    fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; 
    include fastcgi_params; 
}

示例3

server {
  listen 80;
  server_name php.zjol.com.cn;
  charset utf-8;

location / {
  root /usr/local/nginx/html/;
  index index.php index.html;
}

location ~ \.php$ {
  root /usr/local/nginx/html/php/;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}
location ~* ^/(status|ping)$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  include fastcgi_params;
  }
}

php服务器,与nginx同台

yum install php php-fpm 

grep "^[a-Z]" /etc/php-fpm.conf
include=/etc/php-fpm.d/*.conf
pid = /run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
daemonize = yes #是否后台启动

cat /etc/php-fpm.d/www.conf 
[www]
listen = 127.0.0.1:9000 
;listen.allowed_clients = 127.0.0.1 
user = nginx 
group = nginx

测试页面

[root@localhost7B src]# cat /usr/local/nginx/html/php/index.php
<?php
phpinfo();
?>

测试结果
curl php.zjol.com.cn/index.php


fastcgi_cache zone | off; #调用指定的缓存空间来缓存fastcgi数据,缓存就是动态页面变成静态页面。名称为fastcgi_cache_path的keys_zone名。 可用位置:http, server, location
fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size[inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time]
[manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
  path            #缓存位置为磁盘上的文件系统路径
  max_size=size      #磁盘path路径中用于缓存数据的缓存空间上限
  levels=levels:    #十六进制的缓存目录的层级数量,以及每级的子录数量,levels=ONE:TWO:THREE,⽰ 例:leves=1:2:2
  keys_zone=name:size  #设置缓存名称及k/v映射的内存空间的名称及大小
  inactive=time     #缓存有效时间,默认10分钟,需要在指定时间满足fastcgi_cache_min_uses 次数被视为 活动缓存。
fastcgi_cache_key string; #定义用作缓存项的key的字符串,例:fastcgi_cache_key $request_uri;
fastcgi_cache_methods GET | HEAD | POST ...; #为哪些请求方法使用缓存
fastcgi_cache_min_uses number;    #缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
fastcgi_keep_conn on | off;       #收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
fastcgi_cache_valid [code ...] time; #不同的响应码各自的缓存时长
fastcgi_hide_header field;        #隐藏响应头指定信息
fastcgi_pass_header field;       #返回响应头指定信息,默认不会将Status、X-Accel-...返回



示例:
http {
fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s;
server {
location ~* \.php$ {
  fastcgi_cache fcgicache;
  fastcgi_cache_key $request_uri;
  fastcgi_cache_valid 200 302 10m;
  fastcgi_cache_valid 301 1h;
  fastcgi_cache_valid any 1m;
}
}

 

 

posted @ 2022-07-21 20:46  yuanbangchen  阅读(93)  评论(0编辑  收藏  举报