This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
network:nginx_usage_and_configuration [2019/11/16 05:39] zhwiki [模块配置] |
network:nginx_usage_and_configuration [2019/11/16 11:28] zhwiki [Other Modules] |
||
---|---|---|---|
Line 136: | Line 136: | ||
===== 模块配置 ===== | ===== 模块配置 ===== | ||
+ | ==== 核心功能模块 ==== | ||
+ | === events === | ||
+ | |||
+ | * Syntax: events { ... } | ||
+ | * Default: — | ||
+ | * Context: main | ||
+ | |||
+ | 为配置文件提供一个上下文环境,在其中可以指定影响连接处理的指令。 | ||
+ | |||
+ | === include === | ||
+ | |||
+ | * Syntax: include file | mask; | ||
+ | * Default: — | ||
+ | * Context: any | ||
+ | |||
+ | 在当前文件中包含另一个文件或一组,被包含的文件需包含语法正确的指令和块。 | ||
+ | |||
+ | === user === | ||
+ | |||
+ | * Syntax: user user [group]; | ||
+ | * Default: user nobody nobody; | ||
+ | * Context: main | ||
+ | |||
+ | 定义工作进程使用的用户和组凭据。 如果省略组,则使用名称与用户相同的组。 | ||
+ | |||
+ | === worker_processes === | ||
+ | |||
+ | * Syntax: worker_processes number | auto; | ||
+ | * Default: worker_processes 1; | ||
+ | * Context: main | ||
+ | |||
+ | 定义工作进程数。 | ||
+ | |||
+ | 最佳值取决于许多因素,包括(但不限于)CPU内核数,存储数据的硬盘驱动器数以及加载模式。 如有疑问,将其设置为可用的CPU内核数将是一个不错的开始(值“auto”将尝试自动检测可用的CPU内核数)。 | ||
+ | |||
+ | ==== ngx_http_core_module ==== | ||
+ | |||
+ | === 指令 === | ||
+ | |||
+ | == alias == | ||
+ | |||
+ | * Syntax: alias path; | ||
+ | * Default: — | ||
+ | * Context: location | ||
+ | |||
+ | 为''location''定义一个别名。 | ||
+ | |||
+ | 其中,''path''可以包含变量,但不能是%%$document_root%% 和 %%$realpath_root%%。 | ||
+ | |||
+ | 如果在使用正则表达式定义的''location''内使用别名,则该正则表达式应包含捕获,而别名应引用这些捕获。例如: | ||
+ | |||
+ | <code> | ||
+ | location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ { | ||
+ | alias /data/w3/images/$1; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | 如果 ''location''的路径与该指令路径的后部分匹配,则最好使用''root''指令。 | ||
+ | |||
+ | == HTTP == | ||
+ | |||
+ | * Syntax: http { ... } | ||
+ | * Default: — | ||
+ | * Context: main | ||
+ | |||
+ | 为配置文件提供一个上下文环境,在其中可以指定HTTP的 ''server'' 指令。 | ||
+ | |||
+ | == listen == | ||
+ | |||
+ | * Syntax: | ||
+ | * listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]]; | ||
+ | * listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]]; | ||
+ | * listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]]; | ||
+ | * Default: listen *:80 | *:8000; | ||
+ | * Context: server | ||
+ | |||
+ | 设置IP的地址和端口,或UNIX-domain套接字的路径,服务器将在其上接受请求。 既可以指定地址和端口,也可以仅指定地址或端口;地址可以是主机名;IPv6 地址要加方括号;UNIX-domain套接字要使用''unix:''前缀。例如: | ||
+ | |||
+ | <code> | ||
+ | listen 127.0.0.1:8000; | ||
+ | listen 127.0.0.1; | ||
+ | listen 8000; | ||
+ | listen *:8000; | ||
+ | listen localhost:8000; | ||
+ | listen [::]:8000; | ||
+ | listen [::1]; | ||
+ | listen unix:/var/run/nginx.sock; | ||
+ | </code> | ||
+ | |||
+ | 如果仅指定了地址,则使用端口80;如果没有该指令,nginx有超级用户权限,则使用 %%*:80%%,否则使用%%*:8000%%。 | ||
+ | |||
+ | == location == | ||
+ | |||
+ | * Syntax: | ||
+ | * location [ = | ~ | ~* | ^~ ] uri { ... } | ||
+ | * location @name { ... } | ||
+ | * Default: — | ||
+ | * Context: server, location | ||
+ | |||
+ | 设置依赖于请求URI的配置。 | ||
+ | |||
+ | ''location''可以通过前缀字符串或正则表达式定义。正则表达式由前面的%%〜*%%修饰符(不区分大小写)或%%〜%%修饰符(不区分大小写)指定。 | ||
+ | |||
+ | nginx匹配''location''块的过程是,首先检查使用前缀字符串定义的''location''块,选择并记住具有最长前缀的''location''块;然后按照在配置文件中出现的顺序检查正则表达式,正则表达式的搜索在第一个匹配项上终止,并使用相应的配置;如果未找到与正则表达式匹配的内容,则使用前面记住的前缀位置的配置。 | ||
+ | |||
+ | 除下述特殊情况外,''location''块可以嵌套使用。 | ||
+ | |||
+ | 正则表达式可以包含捕获,从而在其他指令中使用。 | ||
+ | |||
+ | 如果匹配的最长前缀''location''块有%%^~%%修饰符,则停止检查正则表达式。 | ||
+ | |||
+ | 使用%%=%%修饰符可以定义URI和''location''块的精确匹配。 如果找到完全匹配的内容,搜索将终止。这样的''location''块显然不能包含嵌套。 | ||
+ | |||
+ | %%@%%前缀定义命名''location''块。 这样的位置不用于常规请求处理,而是用于请求重定向。 它们不能嵌套,也不能包含嵌套位置。 | ||
+ | |||
+ | 如果''location''块由以斜杠字符结尾的前缀字符串定义,并且请求由''proxy_pass'',''fastcgi_pass'',''uwsgi_pass'',''scgi_pass'',''memcached_pass''或''grpc_pass''中的一个处理,则将执行一个特殊处理。 | ||
+ | |||
+ | == root == | ||
+ | |||
+ | * Syntax: root path; | ||
+ | * Default: root html; | ||
+ | * Context: http, server, location, if in location | ||
+ | |||
+ | 设置请求的根目录。 | ||
+ | |||
+ | 其中,''path''可以包含变量,但%%$document_root%% 和 %%$realpath_root%%除外。 | ||
+ | |||
+ | 只需通过将URI添加到root指令的值即可构造文件的路径。如果必须修改URI,则应使用alias指令。 | ||
+ | |||
+ | == server == | ||
+ | |||
+ | * Syntax: server { ... } | ||
+ | * Default: — | ||
+ | * Context: http | ||
+ | |||
+ | 设置虚拟服务器的配置。 | ||
+ | |||
+ | 基于IP的虚拟服务器(基于IP地址)和基于名称的虚拟服务器(基于“主机”请求标头字段)之间没有明确区分。然而,listen指令描述了应该接受连接的所有地址和端口,而server_name指令列出了所有服务器名称。 | ||
+ | |||
+ | == server_name == | ||
+ | |||
+ | * Syntax: server_name name ...; | ||
+ | * Default: server_name ""; | ||
+ | * Context: server | ||
+ | |||
+ | 设置虚拟服务器的名称。 | ||
+ | |||
+ | 服务器名称可以包含一个星号(%%*%%)来代替名称的第一部分或最后一部分,这样的名称称为通配符名称。 | ||
+ | |||
+ | 也可以在服务器名称中使用正则表达式,在名称前加上波浪号(“〜”)。正则表达式可以包含捕获,这些捕获可以在其他指令中使用;正则表达式中的命名捕获会创建变量,可在其他指令中使用该变量。 | ||
+ | |||
+ | 也可以指定一个空服务器名称。给于给定的地址:端口对,它允许该服务器处理请求标头不带主机域的请求。这是默认设置。 | ||
+ | |||
+ | 在按名称搜索虚拟服务器的过程中,如果名称匹配多个指定的变体(例如,通配符名称和正则表达式匹配),则将按照以下优先级顺序选择第一个匹配的变体: | ||
+ | |||
+ | * 确切的名字 | ||
+ | * 以星号开头的最长通配符名称 | ||
+ | * 最长的以星号结尾的通配符名称 | ||
+ | * 第一个匹配的正则表达式(按在配置文件中出现的顺序) | ||
+ | |||
+ | === 内嵌变量 === | ||
+ | |||
+ | ngx_http_core_module模块支持名称与Apache Server变量匹配的内嵌变量。 这些是代表客户端请求标头字段的变量,例如%%$http_user_agent%%,%%$http_cookie%%等,另外还有其他变量。 | ||
+ | |||
+ | ==== ngx_http_autoindex_module ==== | ||
+ | |||
+ | ngx_http_autoindex_module模块处理以斜杠(''/'')结尾的请求,并生成目录列表。 通常,当ngx_http_index_module模块找不到index文件时,会将请求传递给ngx_http_autoindex_module模块。 | ||
+ | |||
+ | === autoindex === | ||
+ | |||
+ | * Syntax: autoindex on | off; | ||
+ | * Default: autoindex off; | ||
+ | * Context: http, server, location | ||
+ | |||
+ | 启用或禁用目录列表输出。 | ||
+ | |||
+ | === autoindex_exact_size === | ||
+ | |||
+ | * Syntax: autoindex_exact_size on | off; | ||
+ | * Default: autoindex_exact_size on; | ||
+ | * Context: http, server, location | ||
+ | |||
+ | 对于HTML格式,指定是在目录列表中输出确切的文件大小,还是四舍五入为KB,MB和GB。 | ||
+ | |||
+ | === autoindex_format === | ||
+ | |||
+ | * Syntax: autoindex_format html | xml | json | jsonp; | ||
+ | * Default: autoindex_format html; | ||
+ | * Context: http, server, location | ||
+ | |||
+ | 设置目录列表的格式。 | ||
+ | |||
+ | 使用JSONP格式时,将使用回调请求参数设置回调函数的名称。 如果参数缺失或值为空,则使用JSON格式。 | ||
+ | |||
+ | 可以使用ngx_http_xslt_module模块转换XML输出。 | ||
+ | |||
+ | === autoindex_localtime === | ||
+ | |||
+ | * Syntax: autoindex_localtime on | off; | ||
+ | * Default: autoindex_localtime off; | ||
+ | * Context: http, server, location | ||
+ | |||
+ | 对于HTML格式,指定输出目录列表中的时间使用本地时区还是UTC。 | ||
+ | |||
+ | ==== ngx_http_index_module ==== | ||
+ | |||
+ | ngx_http_index_module模块处理以斜杠(''/'')结尾的请求。 此类请求也可以由ngx_http_autoindex_module和ngx_http_random_index_module模块处理。 | ||
+ | |||
+ | === index === | ||
+ | |||
+ | * Syntax: index file ...; | ||
+ | * Default: index index.html; | ||
+ | * Context: http, server, location | ||
+ | |||
+ | 定义将用作index的文件。 文件名可以包含变量。 文件以指定顺序检查。 列表的最后一项可以是具有绝对路径的文件。 例如: | ||
+ | |||
+ | index index.$geo.html index.0.html /index.html; | ||
+ | |||
+ | ==== Other Module ==== | ||