Nginx 配置文件简介-2(Nginx 常用模块)


ngx_http_access_module

作用: ngx_http_access_module模块允许限制对某些客户端地址的访问

指令:

     allow:

        语法:allow address | CIDR | unix: | all;

        默认值:空

        应用位置:http, server, location, limit_except

    deny:

        语法:allow address | CIDR | unix: | all;

        默认值:空

        应用位置:http, server, location, limit_except

案例:

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

ngx_http_api_module 

作用:

   ngx_http_api_module模块(1.13.3)提供REST API,用于访问各种状态信息,即时配置上游服务器组以及管理键值对,而无需重新配置nginx。

   该模块取代了ngx_http_status_module和ngx_http_upstream_conf_module模块。商业订阅才有,付费的nginx.

ngx_http_auth_basic_module

作用:

   ngx_http_auth_basic_module模块允许通过使用“HTTP基本身份验证”协议验证用户名和密码来限制对资源的访问。

案例:

location / {
    auth_basic           "closed site";  # 提示短语
    auth_basic_user_file conf/htpasswd; # 密码文件,需要用 htpasswd 生成.
} 

ngx_http_auth_jwt_module

作用:

   ngx_http_auth_jwt_module模块(1.11.3)通过使用指定的密钥验证提供的JSON Web令牌(JWT)来实现客户端授权。 JWT声明必须以JSON Web签名(JWS)结构进行编码。 该模块可用于OpenID Connect身份验证。

案例:

location / {
    auth_jwt          "closed site";
    auth_jwt_key_file conf/keys.json;
}

ngx_http_autoindex_module

作用:

   ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。 当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。

案例:

location /test/ {
       root   /usr/share/nginx/html;
       autoindex on;   # 打开索引
       autoindex_exact_size on; # 显示文件尺寸
       autoindex_format json;   # 以 json 的格式展示
       autoindex_localtime on;  # 显示创建时间(UTC时间)
    }

ngx_http_fastcgi_module

作用:

   ngx_http_fastcgi_module模块允许将请求传递给FastCGI服务器。

指令详解:

   fastcgi_bind

     语法:fastcgi_bind address [transparent] | off;

     默认值:无

     应用位置:http, server, location

     作用:指定 nginx 与 fastcgi 通信的 IP 地址,一般不做设置。 与回环 IP 地址通信,需要设置路由表。

  fastcgi_buffer_size

    语法:fastcgi_buffer_size size;

    默认值:fastcgi_buffer_size 4k|8k;

    应用位置:http, server, location

    作用:  设置用于读取从FastCGI服务器接收的响应的第一部分的缓冲区的大小。 这部分通常包含一个小的响应头。 默认情况下,缓冲区大小等于一个内存页面。 这是4K或8K,具体取决于平台。 然而,它可以做得更小。

  fastcgi_buffers

    语法:fastcgi_buffers number size;

    默认值:fastcgi_buffers 8 4k|8k;

    应用位置:http, server, location

    作用:设置用于从FastCGI服务器读取响应的缓冲区的数量和大小,用于单个连接。 默认情况下,缓冲区大小等于一个内存页面。 这是4K或8K,具体取决于平台。

  fastcgi_buffering

    语法:fastcgi_buffering on | off;

    默认值:fastcgi_buffering on;

    应用位置:http, server, location

    作用:启用或禁用缓冲来自FastCGI服务器的响应。
              启用缓冲后,nginx会尽快从FastCGI服务器接收响应,并将其保存到fastcgi_buffer_size和fastcgi_buffers指令设置的缓冲区中。

              如果整个响应不适合内存,则可以将其中的一部分保存到磁盘上的临时文件中。 写入临时文件由fastcgi_max_temp_file_size和      fastcgi_temp_file_write_size指令控制。
              禁用缓冲时,响应会在收到响应时立即同步传递给客户端。 nginx不会尝试从FastCGI服务器读取整个响应。 nginx一次可以从服务器接收的数据的最大大小由fastcgi_buffer_size指令设置。
              也可以通过在“X-Accel-Buffering”响应头字段中传递“是”或“否”来启用或禁用缓冲。 可以使用fastcgi_ignore_headers指令禁用此功能。

   fastcgi_busy_buffers_size

     语法:fastcgi_busy_buffers_size size;

     默认值:fastcgi_busy_buffers_size 8k|16k;

     应用位置:http, server, location

     作用: 通常设置为 fastcgi_buffer_size 的两倍,高负荷下内存缓冲区的总大小.

  fastcgi_cache

    语法:fastcgi_cache zone | off;

    默认值:fastcgi_cache off;

    应用位置:http, server, location

    作用:该指令用于定义一个共享内存zone,用于缓存 。off 参数关闭从上层继承.

  fastcgi_cache_background_update

    语法:fastcgi_cache_background_update on | off;

    默认值:fastcgi_cache_background_update off;

    应用位置:http, server, location

    作用:允许启动后台子请求来更新过期的缓存项。

  fastcgi_cache_bypass

    语法:fastcgi_cache_bypass string ...;

    默认值:无

    应用位置:http, server, location

    作用:该指令定义一个或多个字符串变量,当非空或非零时,将导致从 FastCGI 服务器获取而不是从缓存中获取响应

    案例:

fastcgi_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
fastcgi_cache_bypass $http_pragma    $http_authorization;

  fastcgi_cache_key

    语法fastcgi_cache_key string;

    默认值:无

    应用位置:http, server, location

    作用:该指令指定一个字符串,作为存储和获取缓存值的 key.

    案例:

fastcgi_cache_key localhost:9000$request_uri;

  fastcgi_cache_lock

    语法:fastcgi_cache_lock on | off;

    默认值:fastcgi_cache_lock off;

    应用位置:http, server, location

    作用:启用这个指令将会阻止多个请求对同一缓存 key  进行操作.

  fastcgi_cache_lock_timeout

    语法:fastcgi_cache_lock_timeout time;

    默认值:fastcgi_cache_lock_timeout 5s;

    应用位置:http, server, location

    作用:设置fastcgi_cache_lock的超时。当时间过期时,请求将被传递给FastCGI服务器,然而,响应将不会被缓存。

  fastcgi_cache_lock_age

    语法:fastcgi_cache_lock_age time;

    默认值:fastcgi_cache_lock_age 5s;

    应用位置:http, server, location

    作用:如果传递给FastCGI服务器的用于填充新缓存元素的最后一个请求在指定的时间内没有完成,那么可以再向FastCGI服务器传递一个请求。不太确定,最好不用.

  fastcgi_cache_min_uses

    语法:fastcgi_cache_min_uses number;

    默认值:fastcgi_cache_min_uses 1;

    应用位置:http, server, location

    作用:该指令指定在一个响应被缓存之前最少的请求数。

  fastcgi_cache_path

    语法: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_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;  # key_zone 存储的内存区域为 one, 分配内存 10M

fastcgi_cache_path /data/nginx/cache keys_zone=cache_zone:10m;

map $request_method $purge_method {
    PURGE   1;
    default 0;
}

server {
    ...
    location / {
        fastcgi_pass        backend;
        fastcgi_cache       cache_zone;
        fastcgi_cache_key   $uri;
        fastcgi_cache_purge $purge_method;
    }
}

  fastcgi_cache_purge

   语法:fastcgi_cache_purge string ...;

   默认值:无

   应用位置:http, server, location

   作用:待定,目前看不明白.

  fastcgi_cache_use_stale

   语法:fastcgi_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_503 | http_403 | http_404 | http_429 | off ...;

   默认值:fastcgi_cache_use_stale off;

   应用位置:http, server, location

   作用:确定在与FastCGI服务器通信期间发生错误时,在哪些情况下可以使用过时的缓存响应。指令的参数与fastcgi_next_upstream指令的参数匹配。

  fastcgi_cache_valid

    语法: fastcgi_cache_valid [code ...] time;

    默认值:无

    应用位置:http, server, location

    作用: 为不同的响应代码设置缓存时间。

    案例:

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 60s;

    应用位置:http, server, location

    作用:nginx 向 fastcgi 服务器生成一个请求后,该指令指定等待 nginx 接收连接的最长时间。

  fastcgi_hide_header

     语法:fastcgi_hide_header field;

     默认值:无

     应用位置:http, server, location

     作用:默认情况下,nginx不会传递头字段“Status”和“X-Accel-…”从FastCGI服务器到客户端的响应。fastcgi_hide_header指令设置不会传递的其他字段。相反,如果需要允许字段的传递,则可以使用fastcgi_pass_header指令。

  fastcgi_ignore_client_abort

    语法:fastcgi_ignore_client_abort on | off;

    默认值:fastcgi_ignore_client_abort off;

    应用位置:http, server, location

    作用:确定当客户端不等待响应而关闭连接时,是否应该关闭与FastCGI服务器的连接。如果设置为 on,客户端放弃连接后,nginx 将不会放弃同 FastCGI 服务器的连接。

  fastcgi_intercept_errors

    语法:fastcgi_intercept_errors on | off;

    默认值:fastcgi_intercept_errors off;

    应用位置:http, server, location

    作用:如果启用该指令,nginx 将会显示 error_page 指令配置信息,而不是直接来自 FastCGI 服务器的响应

  fastcgi_keep_conn

   语法:fastcgi_keep_conn on | off;

   默认值:fastcgi_keep_conn off;

   应用位置:http, server, location

   作用:默认情况下,FastCGI服务器将在发送响应后立即关闭连接。 但是,当此伪指令设置为on时,nginx将指示FastCGI服务器保持连接打开。 特别是,这对于FastCGI服务器的keepalive连接起作用是必要的。

  fastcgi_max_temp_file_size

   语法:fastcgi_max_temp_file_size size;

   默认值:fastcgi_max_temp_file_size 1024m;

   应用位置:http, server, location

   作用:当启用来自FastCGI服务器的响应缓冲,并且整个响应不适合fastcgi_buffer_size和fastcgi_buffers指令设置的缓冲区时,响应的一部分可以保存到临时文件中。 该指令设置临时文件的最大大小。 一次写入临时文件的数据大小由fastcgi_temp_file_write_size指令设置。零值禁用缓冲对临时文件的响应。

  fastcgi_temp_path

    语法:fastcgi_temp_path path [level1 [level2 [level3]]];

    默认值:fastcgi_temp_path fastcgi_temp;

    应用位置:http, server, location

    作用:该指令指定缓存临时文件的目录,作为从 FastCGI 代理而来文件的缓存,可选多级目录深度.

  fastcgi_temp_file_write_size

   语法:fastcgi_temp_file_write_size size;

   默认值:fastcgi_temp_file_write_size 8k|16k;

   应用位置:http, server, location

   作用:当启用从FastCGI服务器到临时文件的响应缓冲时,限制一次写入临时文件的数据大小。 默认情况下,size由fastcgi_buffer_size和fastcgi_buffers指令设置的两个缓冲区限制。 临时文件的最大大小由fastcgi_max_temp_file_size directi设置

  fastcgi_next_upstream

    语法:fastcgi_next_upstream error | timeout | invalid_header | http_500 | http_503 | http_403 | http_404 | http_429 | non_idempotent | off ...;

   默认值:fastcgi_next_upstream error timeout;

   应用位置:http, server, location

   作用:该指令指示下一个 FastCGI 服务器被选中接收提供相应的条件,如果nginx 客户端已经被发送了某些信息,那么这种条件将不会被使用。

     error
        与服务器建立连接,向其传递请求或读取响应头时发生错误;

    timeout
        在与服务器建立连接,向其传递请求或读取响应头时发生超时;
    invalid_header
         服务器返回空或无效响应;
    HTTP_500
        服务器返回代码为500的响应;
    http_503
        服务器返回代码为503的响应;
    HTTP_403
        服务器返回代码为403的响应;
    http_404
       服务器返回代码为404的响应;
    http_429
       服务器返回代码为429的响应(1.11.13);

    off
       禁用将请求传递给下一个server

  fastcgi_next_upstream_timeout

    语法:fastcgi_next_upstream_timeout time;

    默认值:fastcgi_next_upstream_timeout 0;

    应用位置:http, server, location

    作用:限制请求可以传递到下一个服务器的时间。 0值关闭此限制。

   fastcgi_next_upstream_tries

     语法:fastcgi_next_upstream_tries number;

     默认值:fastcgi_next_upstream_tries 0;

     应用位置:http, server, location

     作用:限制将请求传递到下一个服务器的可能尝试次数。 0值关闭此限制。

   fastcgi_no_cache

     语法:fastcgi_no_cache string ...;

     默认值:无

     应用位置:http, server, location

     作用:该指令指定一个或者多个字符串变量,当变量非空或非零,将指导nginx 不会将来自FastCGI 服务器的响应保存到缓存中。

    案例:

fastcgi_no_cache $cookie_nocache $arg_nocache$arg_comment;
fastcgi_no_cache $http_pragma    $http_authorization;

   fastcgi_param

     语法:fastcgi_param parameter value [if_not_empty];

     默认值:无

     应用位置:http, server, location

     作用:该指令设置传递到 FastCGI 服务器的参数和它的值,在值为非空的时候,如果仅传送参数,那么  if_not_empty 应该设置额外的参数.

     案例:

fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING    $query_string;

fastcgi_param HTTPS   $https if_not_empty;

#如果PHP是使用--enable-force-cgi-redirect配置参数构建的,则还应使用值“200”传递REDIRECT_STATUS参数:
fastcgi_param REDIRECT_STATUS 200;

  fastcgi_pass

    语法:fastcgi_pass address;

    默认值:无

    应用位置:location, if in location

    作用:该指令指定 FastCGI 服务器如何传递请求,可以是 address:port 组合的 TCP 套接字,也可以是 unix:path UNIX 域套接字.

   fastcgi_pass_header

     语法:fastcgi_pass_header field;

     默认值:无

     应用位置:http, server, location

     作用:该指令覆盖在 Fastcgi_hide_header 中设置的禁用头,允许他们发送到客户端.

   fastcgi_pass_request_body

     语法:fastcgi_pass_request_body on | off;

     默认值:fastcgi_pass_request_body on;

     应用位置:http, server, location

     作用:指示是否将原始请求主体传递给FastCGI服务器。

   fastcgi_pass_request_headers

     语法:fastcgi_pass_request_headers on | off;

     默认值:fastcgi_pass_request_headers on;

     应用位置:http, server, location

     作用:指示原始请求的标头字段是否传递给FastCGI服务器。

   fastcgi_read_timeout

     语法:fastcgi_read_timeout time;

     默认值:fastcgi_read_timeout 60s;

     应用位置:http, server, location

     作用:该指令指定在连接关闭之前从FastCGI服务器两次成功读操作之间的时间长度.

               定义从FastCGI服务器读取响应的超时。 仅在两个连续的读操作之间设置超时,而不是为整个响应的传输。 如果FastCGI服务器在此时间内未传输任何内容,则关闭连接。

   fastcgi_send_timeout

     语法:fastcgi_send_timeout time;

     默认值:fastcgi_send_timeout 60s;

     应用位置:http, server, location

     作用:设置将请求传输到FastCGI服务器的超时。 仅在两个连续的写操作之间设置超时,而不是为整个请求的传输。 如果FastCGI服务器在此时间内未收到任何内容,则关闭连接。

   fastcgi_split_path_info

     语法:fastcgi_split_path_info regex;

     默认值:无

     应用位置:location

     作用:定义捕获$ fastcgi_path_info变量值的正则表达式。 正则表达式应该有两个捕获:第一个变为$ fastcgi_script_name变量的值,第二个变为$ fastcgi_path_info变量的值。只有依靠 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;

    and the “/show.php/article/0001” request, the SCRIPT_FILENAME parameter will be equal to “/path/to/php/show.php”, and the PATH_INFO parameter will be equal to “/article/0001”.

   fastcgi_store

     语法:fastcgi_store on | off | string;

     默认值:fastcgi_store off;

     应用位置:http, server, location

     作用:该指令启用将从 FastCGI 服务器获取来的响应作为文件存储在磁盘上。设置为 on,那么将会使用 alias 或者 root 指令的值作为存储文件的基础路径,指定一个字符串可以取而代之成为指示文件的存储位置的选择.

     案例:

location /fetch/ {
    internal;

    fastcgi_pass         backend:9000;
    ...

    fastcgi_store        on;
    fastcgi_store_access user:rw group:rw all:r;
    fastcgi_temp_path    /data/temp;

    alias                /data/www/;
}

   fastcgi_store_access

     语法:fastcgi_store_access users:permissions ...;

     默认值:fastcgi_store_access user:rw;

     应用位置:http, server, location

     作用:该指令为新创建的,存储在 FastCGI_store 中的文件设置访问权限.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM