浅谈Nginx(二)—http下server配置

浅谈Nginx(二)—http下server配置

此文介绍Nginx下的http模块,着重介绍http模块下的server服务

——–依据”马哥教育”主讲人马永亮导师的上课笔记整理——-

目录
 一. http相关的基本配置:
    1) listen           
    2) server_name      
    3) tcp_nodelay
    4) sendfile     
二. 定义路径相关的配置:
    1)root path
    2)location
    3)alias path
    4)error_page      
    5)try_files
三. 定义客户端请求的相关配置:
    1)keepalive_timeout
    2)keepalive_requests
    3)keepalive_disable
    4)send_timeout
    5)client_body_buffer_size
    6)client_body_temp_path
四. 对客户端进行限制的相关配置:
    1) limit_rate
    2) limit_except
五. 文件操作优化的配置:
    1)aio
    2)directio
    3)open_file_cache
    4)open_file_cache_valid
    5)open_file_cache_min_uses
    6)open_file_cache_errors
六. 一些常用的模块(非core模块)
    1)ngx_http_access_module模块
    2)ngx_http_auth_basic_module模块
    3)ngx_http_stub_status_module模块
    4)ngx_http_log_module模块
    5)ngx_http_gzip_module模块
    6)ngx_http_ssl_module模块
    7)ngx_http_rewrite_module模块
    8)ngx_http_referer_module模块
    9)ngx_http_proxy_module模块
    10)proxy_set_header field value模块
    11)ngx_http_headers_module模块
    12)ngx_http_fastcgi_module模块

一、http模块的基本配置格式

            http {
                ... ...
                server {
                    ...
                    server_name
                    root
                    location [OPERATOR] /uri/ {
                        ...
                    }
                }
                server {
                    ...
                }
            }

1.1 server{…….} :

http模块下可有多个虚拟主机,每个主机定义在单独的一个server配置段内;
1.1.1 server段的基本配置:
            server {
                        listen address[:PORT]|PORT;
                        server_name SERVER_NAME;
                        root /PATH/TO/DOCUMENT_ROOT;
                    }
        ----------------------------

    1) listen PORT | unix:/PATH/TO/SOCKET_FILE         #设置监听端口
    2) listen address[:port] [default_server] [ssl] [http2 | spdy] 
        [backlog=number] [rcvbuf=size] [sndbuf=size]   #设置监听地址

            解释:
                default_server:设定为默认虚拟主机;
                ssl:限制仅能够通过ssl连接提供服务;
                backlog=number:后援队列长度;
                rcvbuf=size:接收缓冲区大小;
                sndbuf=size:发送缓冲区大小;
            实例:
                server {
                        listen 80;
                        listen 172.16.33.117:8080;
                    }

    *3) server_name  设置主机名称;
            1)指明虚拟主机的主机名称;后可跟多个由空白字符分隔的字符串;
            2)支持*通配任意长度的任意字符;server_name   *.magedu.com  www.magedu.*
            3)支持~起始的字符做正则表达式模式匹配;server_name    ~^www\d+\.magedu\.com$
                匹配机制:
                    (1) 首先是字符串精确匹配;
                    (2) 左侧*通配符;
                    (3) 右侧*通配符;
                    (4) 正则表达式;
            实例:
                server {
                        user_name  www.magedu33.com
                    }

    4) tcp_nodelay on | off;
        在keepalived模式下的连接是否启用TCP_NODELAY选项;

    5) sendfile on | off;
        是否启用sendfile功能

二. 定义路径相关的配置

    *1) root path
        设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目
        录路径;可用的位置:http, server, location, if in location

    *2) location [ = | ~ | ~* | ^~ ] uri { ... }
                location @name { ... }
            解释:
                在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映
            射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而
            后应用其配置;
                如在网页中输入你想访问的地址:www.magedu.com/picture,这个uri是“picture”,
            nginx的http模块接收到此uri后,寻找与“/picture”最匹配的location,然后执行此location
            下内容,并返回给用户


            =:对URI做精确匹配;
            ~:对URI做正则表达式模式匹配,区分字符大小写;
            ~*:对URI做正则表达式模式匹配,不区分字符大小写;
            ^~:对URI的左半部分做匹配检查,不区分字符大小写,一般用来匹配目录;
            不带符号:匹配起始于此uri的所有的url;

            匹配优先级:=, ^~, ~/~*,不带符号;

            例:
                location  = / {
                    # 只匹配"/".
                [ configuration A ] 
                }

                location  / {
                # 匹配任何请求,因为所有请求都是以"/"开始
                # 但是更长字符匹配或者正则表达式匹配会优先匹配
                [ configuration B ] 
                }

                location ^~ /images/ {
                # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
                [ configuration C ] 
                }

                location ~* \.(gif|jpg|jpeg)$ {
                # 匹配以 gif, jpg, or jpeg结尾的请求,不区分大小写. 
                # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
                [ configuration D ] 
                }


    *3) alias path;
        定义路径别名,文档映射的另一种机制;仅能用于location上下文;

        注意:location中使用root指令和alias指令的意义不同;
                (a) root,给定的路径对应于location中的/uri/左侧的/;
                (b) alias,给定的路径对应于location中的/uri/右侧的/;
        例:
            server {
                    location /admin/{
                        alias /data/heml/;
                    }
            }

    *4) error_page code ... [=[response]] uri;     #定义错误页面

        例:
            server {
                    error_page  404   /404.html;  
                     #  error_page 404 =200 /404.html;此设置是错误项改为200,欺骗作用.
                    location = /404.html{
                        root "自定义错误页面存放路径"
                    }
            }

三. 定义客户端请求的相关配置

    1) keepalive_timeout timeout [header_timeout];
        设定保持连接的超时时长,0表示禁止长连接;默认为75s;

    2) eepalive_requests number;
        在一次长连接上所允许请求的资源的最大数量,默认为100;

    3) keepalive_disable none | browser ...;
        对哪种浏览器禁用长连接;

    4) send_timeout time;
        向客户端发送响应报文的超时时长,此处,是指两次写操作之间的间隔时长;

    5) client_body_buffer_size size;
        用于接收客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,
        其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置;

    6) client_body_temp_path path [level1 [level2 [level3]]];
        设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量;

            例:
            client_body_temp_path path  /var/tmp/client_body  1 2 2

四. 对客户端进行限制的相关配置

    1)limit_rate rate;
        限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;

    2)limit_except method ... { ... }
        限制对指定的请求方法之外的其它方法的使用客户端;

            例:除了192.168.1.0/24以外的主机禁止访问     
                limit_except GET {
                        allow 192.168.1.0/24;
                        deny  all;
                }

五. 文件操作优化的配置

    1) aio on | off | threads[=pool];        # aio 异步IO,内核把内存中的数据做缓存,过段时间后在把他们写到磁盘,这就叫异步IO
        是否启用异步IO功能;

    2) directio size | off;             # 直接IO, 内核直接写数据到磁盘,不过效率比较低
        在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;

    3) open_file_cache off;         #可加速访问速率,使用内存空间;可缓存否定答案,非权威答案
        open_file_cache max=N [inactive=time];     # 定义可定义缓存上限
        nginx可以缓存以下三种信息:
            (1) 文件的描述符、文件大小和最近一次的修改时间;
            (2) 打开的目录结构;检测目录存在与否
            (3) 没有找到的或者没有权限访问的文件的相关信息;

            max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;

            inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次 
            数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项;

    4) open_file_cache_valid time;
            缓存项有效性的检查频率;默认为60s; 每隔60s检测一次缓存项是否小于给定值

    5) open_file_cache_min_uses number;
            在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被
            归类为活动项;

    6) open_file_cache_errors on | off;      # 缓存否定答案的值
            是否缓存查找时发生错误的文件一类的信息;

六、一些常用模块(非core模块)

6.1 ngx_http_access_module模块

作用: 基于ip的访问控制功能

    1) allow address | CIDR | unix: | all;
    2) deny address | CIDR | unix: | all;
        例:
            location /admin{
                deny 192.168.0.1;       # 拒绝192.168.0.1访问
                allow 192.168.0.0/24;   # 允许192.168.0.0/24访问,除了上一行定义的192.168.0.1
                allow 10.1.1.0/16;      # 允许10.1.1.0/16这一网段中的主机访问
                deny all                # 除了已定义规则外,所有主机不可访问,默认策略
            }
        注:规则时自上而下检查,定义规则时,范围小的应该放在上面,范围大的放下面

6.2 ngx_http_auth_basic_module模块

作用:实现基于用户的访问控制,使用basic机制进行用户认证

    1)auth_basic string | off;          # 提供登录时的提示页面
    2)auth_basic_user_file file;        # 用户登录文件认证

        例:      
        location /admin/ {
                alias /webapps/app1/data/;
                auth_basic "Admin Area";
                auth_basic_user_file /etc/nginx/.ngxpasswd;
        }

        ps:htpasswd命令由httpd-tools所提供;htpasswd生成一个加密账户文件,存放至一个目录

6.3 ngx_http_stub_status_module模块

作用:用于输出nginx的基本状态信息

    访问网页后加uri “/basic_status”s时出现的内容:
        Active connections: 291                         #活动链接数
        server accepts handled requests
        16630948 16630948 31070465 
        Reading: 6 Writing: 179 Waiting: 106

        解释:     
            Active connections: 活动状态的连接数;
            accepts:已经接受的客户端请求的总数;
            handled:已经处理完成的客户端请求的总数;
            requests:客户端发来的总的请求数;
            Reading:处于读取客户端请求报文首部的连接的连接数;
            Writing:处于向客户端发送响应报文过程中的连接数;
            Waiting:处于等待客户端发出请求的空闲连接数;
        例:
            location   /basic_status {
                    stub_status;
            }

6.4 ngx_http_log_module模块 (重要)

作用:nginx的以指定的格式保存访问日志,利于分析用户习惯

    log_format name string ...;

    string可以使用nginx核心模块及其它模块内嵌的变量;
    1)access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
        access_log off;

        访问日志文件路径,格式及相关的缓冲的配置;
            buffer=size
            flush=time

    2)open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
        open_log_file_cache off;
        缓存各日志文件相关的元数据信息;

            max:缓存的最大文件描述符数量;
            min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项;
            inactive:非活动时长;
            valid:验正缓存中各缓存项是否为活动项的时间间隔;

            例:
                http {

                    log_format main  '$remote_addr - $remote_user [time_local] "request"';
                                     '$status $body_bytes_sent "$http_referer"';
                                     ' "$http_user_agent" "$http_x_forwarded_for" ';

                    access_log  /var/log/nginx/access.log   main;
                    open_log_file_cache max=100 inactive=120s min_uses=2 valid=120s;
                }

6.5 ngx_http_gzip_module模块

作用:压缩内容

    1)gzip on | off;
        启用或禁用“gzip”功能;

    2)gzip_comp_level level;
        压缩级别,范围从1-9;

    3)gzip_disable regex ...;
        对哪些浏览器禁用功能
        Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.

    4)gzip_min_length length;
        启用压缩功能的响应报文大小阈值; 如:文件至少10K在执行压缩

    5)gzip_buffers number size;
        支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小;

    6)gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
        nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的;
        off:对代理的请求不启用
        no-cache, no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能;

    7)gzip_types mime-type ...;
        压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能;

        示例:
            gzip  on;                   # 启用压缩功能
            gzip_comp_level 6;          # 定义压缩比,数值越大,压缩越高
            gzip_min_length 64;         # 至少64个字节才压缩
            gzip_proxied any;           # 任何代理请求都执行压缩功能
            gzip_types text/xml text/css  application/javascript;           # 仅对何种资源做压缩

6.6 ngx_http_ssl_module模块

作用:使得网址使用加密传输,即https

    1)ssl on | off;
        Enables the HTTPS protocol for the given virtual server.

    2)ssl_certificate file;
        当前虚拟主机使用PEM格式的证书文件;

    3)ssl_certificate_key file;
        当前虚拟主机上与其证书匹配的私钥文件;

    4)ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
        支持ssl协议版本,默认为后三个;

    5)ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
        builtin[:size]:使用OpenSSL内建的缓存,此缓存为每worker进程私有;

        [shared:name:size]:在各worker之间使用一个共享的缓存;

    6)ssl_session_timeout time;
        客户端一侧的连接可以复用ssl session cache中缓存 的ssl参数的有效时长;

        配置示例:
                server {
                    listen 443 ssl;
                    server_name www.magedu33.com;
                    root /vhosts/ssl/htdocs;
                    ssl on;         # 启用ssl功能
                    ssl_certificate /etc/nginx/ssl/nginx.crt;           # 证书路径
                    ssl_certificate_key /etc/nginx/ssl/nginx.key;       # 私钥路径
                    ssl_session_cache shared:sslcache:20m;      # 会话缓存时间
                }

6.7 ngx_http_rewrite_module

作用:网页重载,当你输入一个地址如www.magedu.com访问时,nginx服务可以通过此模块功能,将网址重载成www.magedu.com/bbs/,访问的内容也会指向www.magedu.com/bbs/

    1)rewrite regex replacement [flag]
        将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI;

        注意:     
            如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件
            规则替换完成后,会重新一轮的替换检查,因此,隐含有循环机制;[flag]所表示
            的标志位用于控制此循环机制;

            如果replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端;

        [flag]:
            last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环; 
            break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环;
            redirect:重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;不能以http://或https://开头;
            permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;

    2)return
        return code [text];
        return code URL;
        return URL;

        Stops processing and returns the specified code to a client. 
        中止此次请求,并返回一个错误页;

    3)rewrite_log on | off;
        是否开启重写日志;

    4)if (condition) { ... }
        引入一个新的配置上下文 ;条件满足时,执行配置块中的配置指令;server, location;

        condition:
            比较操作符:
                ==
                !=
                ~:模式匹配,区分字符大小写;
                ~*:模式匹配,不区分字符大小写;
                !~:模式不匹配,区分字符大小写;
                !~*:模式不匹配,不区分字符大小写;
            文件及目录存在性判断:
                -e, !-e
                -f, !-f
                -d, !-d
                -x, !-x

    5)set $variable value;
        用户自定义变量 ;

        例1:
            location /bbs/ {
                    rewrite  ^/bbs/(.*)$   /forum/$1 break;            #用户请求访问“bbs”时,此命令把它替换成/forum/开头,后面接任意字符(任意字符和原bbs后字符一致)
                    return 403;     #返回状态值200
            }

        例2:
            if ($http_user_agent ~ MSIE){
                rewite ^(.*)$ /msie/$1 break;       #判断模式是否是IE浏览器,匹配则把原地址改成以"/msie/"开头的字符
            }

6.8 ngx_http_referer_module模块

作用:限定引用参考功能,用户请求报文的值是一个非法引用,可以通过此模块禁用该请求。即可定义合法引用。

    1)valid_referers none | blocked | server_names | string ...;
        定义referer首部的合法可用值;

        none:请求报文首部没有referer首部;
        blocked:请求报文的referer首部没有值;
        server_names:参数,其可以有值作为主机名或主机名模式;
        arbitrary_string:直接字符串,但可使用*作通配符;
        regular expression:被指定的正则表达式模式匹配到的字符串;要使用~打头,例如 ~.*\.magedu\.com;

        配置示例:

            valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;               # 定义关键字

            if($invalid_referer) {          # 如果请求的值不在关键字内,就拒绝访问,并返回值403,并跳转至百度(可设成任意网址)
                return 403 www.baidu.com;
            }

6.9 ngx_http_proxy_module模块

作用:代理模块,可以让nginx具有代理功能,可以正向代理,也可反向代理;
官方定义:The ngx_http_proxy_module module allows passing requests to another server.用于实现把一个请求转发给其他服务器;

        1)proxy_pass URL;       # 完成URL映射
            Context:    location, if in location, limit_except

            注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机;

                例:              
                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://hos[:port];
                    }
                    ...
                }

                http://HOSTNAME/uri --> http://host/uri

       ======================================================================       
            proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri;

                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://host/new_uri/;
                    }
                    ...
                }

                http://HOSTNAME/uri/ --> http://host/new_uri/
       =====================================================================


    ===============================================================                 
            如果location定义其uri时使用了正则表达式的模式,则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后;

                server {
                    ...
                    server_name HOSTNAME;
                    location ~|~* /uri/ {
                        proxy http://host;
                    }
                    ...
                }

                http://HOSTNAME/uri/ --> http://host/uri/;
    ==================================================================

        2)proxy_set_header field value;
            设定发往后端主机的请求报文的请求首部的值;Context:   http, server, location

            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        3)proxy_cache_path
            定义可用于proxy功能的缓存;Context:    http

            proxy_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];

        4)proxy_cache zone | off;
            指明要调用的缓存,或关闭缓存机制;Context:   http, server, location

        5)  proxy_cache_key string;
            缓存中用于“键”的内容;

            默认值:proxy_cache_key $scheme$proxy_host$request_uri;

        6)proxy_cache_valid [code ...] time;
            定义对特定响应码的响应内容的缓存时长;

            定义在http{...}中;
            proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;

            定义在需要调用缓存功能的配置段,例如server{...};
            proxy_cache pxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;

        7)proxy_cache_use_stale
            定义用过期的内容响应客户端,例如:服务器错误或无响应,nginx可用缓存响应客户端
            proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;

            Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server.

        8)proxy_cache_methods GET | HEAD | POST ...;
            针对方法做代理
            If the client request method is listed in this directive then the response will be cached. “GET” and “HEAD” methods are always added to the list, though it is recommended to specify them explicitly.

        9)proxy_hide_header field;
            请求或响应中可以隐藏后端服务器报文内容
            By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-...” from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.
            例:
                location  / {
                    proxy_hide_header Server;
                }

        10)proxy_connect_timeout time;
            nginx代理服务器等待后端服务器响应时长;
            Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.

            默认为60s;

6.10 proxy_set_header field value模块

        设定发往后端主机的请求报文的请求首部的值;Context:   http, server, location
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

6.11 ngx_http_headers_module模块

作用:The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;

        1) add_header name value [always];
            添加自定义首部;

            add_header X-Via  $server_addr;
            add_header X-Accel $server_name;

        2) expires [modified] time;
            expires epoch | max | off;

            用于定义Expire或Cache-Control首部的值;

6.12 ngx_http_fastcgi_module模块

作用:The ngx_http_fastcgi_module module allows passing requests to a FastCGI server.

        1)fastcgi_pass address;
            address为fastcgi server的地址;  location, if in location;

        2)fastcgi_index name;
            fastcgi默认的主页资源;

        3)fastcgi_param parameter value [if_not_empty];     # 定义重载路径
            Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination.

        配置示例1:
            前提:配置好fpm server和mariadb-server服务;
                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;          #定义路径映射请求URL的须补在/usr/share/nginx/html后,构成新的URL,形成绝对路径传递给后端服务器
                    include        fastcgi_params;
                }

        配置示例2:通过/pm_status和/ping来获取fpm server状态信息;
            location ~* ^/(pm_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];

            定义fastcgi的缓存;缓存位置为磁盘上的文件系统,由path所指定路径来定义;

                levels=levels:缓存目录的层级数量,以及每一级的目录数量;levels=ONE:TWO:THREE
                    leves=1:2:2
                keys_zone=name:size
                    k/v映射的内存空间的名称及大小
                inactive=time
                    非活动时长
                max_size=size
                    磁盘上用于缓存数据的缓存空间上限

        5)fastcgi_cache zone | off;
            调用指定的缓存空间来缓存数据;http, server, location

        6)fastcgi_cache_key string;
            定义用作缓存项的key的字符串;

        7)fastcgi_cache_methods GET | HEAD | POST ...;
            为哪些请求方法使用缓存;

        8) fastcgi_cache_min_uses number;
            缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;

        9) fastcgi_cache_valid [code ...] time;
            不同的响应码各自的缓存时长;

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

            10) fastcgi_keep_conn on | off;
                By default, a FastCGI server will close a connection right after sending the response. However, when this directive is set to the value on, nginx will instruct a FastCGI server to keep connections open.

原创文章,作者:yaoqin.liang,如若转载,请注明出处:http://www.178linux.com/67800

(0)
上一篇 2017-02-07 19:16
下一篇 2017-02-07 22:23

相关推荐

  • Linux下的LVM管理命令

    一. 何为LVM?     Logical Volume Manager的缩写,它可以把多个分区、硬盘甚至RAID组合成一个存储设备来使用,并可以扩展或缩减空间。LVM有三层组成组成:底层的PV,中间的VG,上层的LV,如图所示         &n…

    Linux干货 2015-12-06
  • Linux ansible 服务

                      Linux ansible 服务 Ansible:    运维工具的分类: agent:基于专用的agent程序完成管理功能,puppet, func, zabbix, … agentless:基于ss…

    系统运维 2016-11-19
  • 建立yum源及yum命令的使用

    一、什么是YUM     YUM的全称为 Yellowdog Update Modifier,其主要目的是为了解决RPM包安装时的依赖关系的问题。YUM只是一个用于软件安装的前端工具,其主要的服务对象还是RPM软件包。     YUM采用C/S架构,即客户端与服务器的模…

    Linux干货 2015-05-11
  • 第一周的学习总结

       本人是Linux 小白,0基础。加入马帮开始Linux之旅。由于完全不懂Linux,所以在学习的过程中,每课都要看上2遍。接下来说说我第一周所学的内容。 首先是了解到了计算机基础知识,计算机的组成部分、CPU架构类型、其他外围设备。 操作系统基础知识进程管理、内存管理、网络管理、驱动管理、安全管理等。 Linux的起源、发行版以及构…

    Linux干货 2016-02-28
  • 计算机基础与linux入门

    计算机硬件组成:     运算器:主要完成算术运算,逻辑运算     控制器:控制指令的执行序列,根据指令的功能给出实现指令功能所需要的控制信号     存储器:存放程序以及一些数据     &nbs…

    Linux干货 2015-12-19
  • PageRank算法

    1. PageRank算法概述          PageRank,即网页排名,又称网页级别、Google左侧排名或佩奇排名。         是Google创始人拉里·佩奇和谢尔盖·布林于1997年构建早期的…

    Linux干货 2015-12-15