美文网首页我爱编程
nginx模块之ngx_http_fastcgi_module

nginx模块之ngx_http_fastcgi_module

作者: 小尛酒窝 | 来源:发表于2018-05-22 22:05 被阅读0次

    前言

    在Apache中php页面内容通常是通过fastcgi代理传递到php-fpm服务器上进行处理的。同理在nginx服务中也也支持类似的做法。nginx服务支持FastCGI模式,能够快速高效地处理php动态请求的内容。而nginx的FastCGI模式对应的模块即为ngx_http_fastcgi_module。那么下面我们就来一起看下该模块的常用指令以及简单的示列。

    ngx_http_fastcgi_module

    - 1、fastcgi_pass address;
    配置段: location, if in location
    此指令用于指定一个fastcgi服务器的地址,可以服务器的主机名或IP+端口的方式指定。
    示例:

    location ~* \.php$ {
            ....
            fastcgi_pass 10.10.10.13:9000;
            ...
    }
    

    - 2、fastcgi_index name;
    配置段:http, server, location
    设置fastcgi的默认主页资源,如果URI以斜线结尾,文件名将追加到URI后面,这个值将存储在变量$fastcgi_script_name中。例如:

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
    

    请求uri/page.php的参数SCRIPT_FILENAME将被设置为/home/www/scripts/php/page.php,但是"/"为"/home/www/scripts/php/index.php"。

    - 3、fastcgi_param parameter value [if_not_empty];
    配置段:http, server, location
    此指令用于指定要传递给FastCGI服务器的参数,参数可以为文本、变量和两者之间的组合。
    示例1:

    #将匹配的php内容送到fastcgi服务器的/usr/share/nginx/html目录下进行处理,即指定fastcgi服务器上用于存放PHP内容的目录
    location ~* \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;  #调用nginx的变量定义;
    }
    

    示例2:

    #将状态页面status和检测页面ping送到fastcgi进行处理
    location ~* ^/(status|ping)$ {
        include        fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
    }
    

    - 4、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];
    配置段:http
    此指令用于定义fastcgi代理的缓存路径,缓存位置为nginx服务器上的文件路径。
    levels=levels:缓存目录的层级数量,以及每一级的目录数量;格式为:levels=ONE:TWO:THREE,如:leves=1:2:2。
    keys_zone=name:size:指定映射的内存空间的名称及大小
    inactive=time:指定缓存文件如果在指定的时间内都没有被访问,则文件会被cache manager进程删除掉。
    max_size=size:指定此缓存空间的大小上限。
    示例:

    fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
    

    - 5、fastcgi_cache zone | off;
    配置段: http, server, location
    调用使用fastcgi_cache_path指定设置的缓存空间来缓存数据。
    示例:

    fastcgi_cache one;
    

    - 6、fastcgi_cache_key string;
    配置段: http, server, location
    定义缓存的key字符串。

    - 7、fastcgi_cache_methods GET | HEAD | POST ...;
    配置段:http, server, location
    设置哪些请求方法能够使用fastcgi缓存。

    - 8、fastcgi_cache_min_uses number;
    配置段:http, server, location
    缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;

    - 9、fastcgi_cache_valid [code ...] time;
    配置段: http, server, location
    根据不同的页面响应码设置不同的缓存时间。
    示例:

    location ~* \.php$ {
        ...
        fastcgi_cache fcgi;
        fastcgi_cache_key $request_uri;
        fastcgi_cache_valid 200 302 10m;  #状态码为200和302的页面内容缓存10分钟
        fastcgi_cache_valid 301 1h;
        fastcgi_cache_valid any 1m; 
        ...
    }
    

    - 10、fastcgi_keep_conn on | off;
    配置段:http, server, location
    用于开启fastcgi长连接机制。

    综合示例
    server {
            listen 80;
            server_name www.ilinux.io;
            index index.php index.html;
            location / {
                    root /data/nginx/html;
                    proxy_pass http://10.10.10.12:80; 
            }
            location ~* \.php$ {
                    fastcgi_pass 10.10.10.11:9000;  #指定fastcgi服务器
                    fastcgi_index index.php;  #指定默认的fastcgi主页
                    include fastcgi_params;  #调用nginx的变量定义
                    fastcgi_param   SCRIPT_FILENAME /data/apps/$fastcgi_script_name;  #指定fastcgi服务器上的php目录
                    fastcgi_cache   fcache;  #调用fache缓存空间
                    fastcgi_cache_key $request_uri;  #设置缓存的key为$request_uri
                    fastcgi_cache_valid 200 302 10m;  #状态码为200和302的页面缓存10分钟
                    fastcgi_cache_valid 301         1h;  #状态码为301的页面缓存1小时
                    fastcgi_cache_valid any         1m;  #剩下的都缓存1分钟
                    fastcgi_keep_conn on;  #开启长连接
            }
            location ~* ^/(status|ping)$ {
                    fastcgi_pass 10.10.10.11:9000;
                    include fastcgi_params;
                    fastcgi_param   SCRIPT_FILENAME /data/apps/$fastcgi_script_name;
            }
    }
    

    相关文章

      网友评论

        本文标题:nginx模块之ngx_http_fastcgi_module

        本文链接:https://www.haomeiwen.com/subject/qfdtjftx.html