Nginx 配置详细文件

<div class="article-body kancloud-markdown-body"><h3><a id="_0"></a>概述</h3>
<p>Nginx 是使用一个 master 进程来管理多个 worker 进程提供服务。master 负责管理 worker 进程,而 worker 进程则提供真正的客户服务,worker 进程的数量一般跟服务器上 CPU 的核心数相同,worker 之间通过一些进程间通信机制实现负载均衡等功能。Nginx 进程之间的关系可由下图表示:</p>
<p><img src="https://box.kancloud.cn/2016-09-01_57c7edce687dc.jpg" alt=""></p>
<p>Nginx 服务启动时会读入配置文件,后续的行为则按照配置文件中的指令进行。Nginx 的配置文件是纯文本文件,默认安装 Nginx 后,其配置文件均在 /usr/local/nginx/conf/ 目录下。其中,nginx.conf  为主配置文件。配置文件中以 # 开始的行,或者是前面有若干空格或者 TAB 键,然后再跟 # 的行,都被认为是注释。这里只是了解主配置文件的结构。</p>
<p>Nginx 配置文件是以 block(块)形式组织,每个 block 都是以一个块名字和一对大括号 “{}” 表示组成,block 分为几个层级,整个配置文件为 main 层级,即最大的层级;在 main 层级下可以有 event、http 、mail 等层级,而 http 中又会有 server block,server block中可以包含 location block。即块之间是可以嵌套的,内层块继承外层块。最基本的配置项语法格式是“配置项名  配置项值1  配置项值2  配置项值3  ... ”;</p>
<p>每个层级可以有自己的指令(Directive),例如 worker_processes 是一个main层级指令,它指定 Nginx 服务的 Worker 进程数量。有的指令只能在一个层级中配置,如worker_processes 只能存在于 main 中,而有的指令可以存在于多个层级,在这种情况下,子 block 会继承 父 block 的配置,同时如果子block配置了与父block不同的指令,则会覆盖掉父 block 的配置。指令的格式是“指令名 参数1 参数2 … 参数N;”,注意参数间可用任意数量空格分隔,最后要加分号。</p>
<p>下图是 Nginx 配置文件通常结构图示。</p>
<p><img src="https://box.kancloud.cn/2016-09-01_57c7edce82018.jpg" alt=""></p>
<h3><a id="Nginx__18"></a>Nginx 服务的基本配置项</h3>
<p>Nginx 服务运行时,需要加载几个核心模块和一个事件模块,这些模块运行时所支持的配置项称为基本配置;基本配置项大概可分为以下四类:</p>
<ul><li>用于调试、定位的配置项;</li><li>正常运行的必备配置项;</li><li>优化性能的配置项;</li><li>事件类配置项;</li></ul>
<p>各个配置项的具体实现如下:</p>
<pre><code class="ಠhighlight-container"><span class="token comment">/* Nginx 服务基本配置项 */</span>

<span class="token comment">/* 用于调试、定位的配置项 */</span>

以守护进程 Nginx 运行方式

语法:daemon off <span class="token operator">|</span> on<span class="token punctuation">;</span>

默认:daemon on<span class="token punctuation">;</span>

master <span class="token operator">/</span> worker 工作方式

语法:master_process on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:master_process on<span class="token punctuation">;</span>

error 日志设置

路径 错误级别

语法:error_log <span class="token operator">/</span>path<span class="token operator">/</span>file level<span class="token punctuation">;</span>

默认:error_log logs<span class="token operator">/</span>error<span class="token punctuation">.</span>log error<span class="token punctuation">;</span>

其中<span class="token operator">/</span>path<span class="token operator">/</span>file是一个具体文件;level是日志的输出级别,其取值如下:

debug info notice warn error crit alert emerg

从左至右级别增大;若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;

处理特殊调试点

语法:debug_points <span class="token punctuation">[</span>stop <span class="token operator">|</span> abort<span class="token punctuation">]</span>

这个设置是来跟踪调试 Nginx 的;

仅对指定的客户端输出 debug 级别的日志

语法:debug_connection <span class="token punctuation">[</span>IP <span class="token operator">|</span> DIR<span class="token punctuation">]</span>

限制 coredump 核心转储文件的大小

语法:worker_rlimit_core size<span class="token punctuation">;</span>

指定 coredump 文件的生成目录

语法:working_directory path<span class="token punctuation">;</span>

<span class="token comment">/* 正常运行的配置项 */</span>

定义环境变量

语法:env VAR <span class="token operator">|</span> VAR<span class="token operator">=</span>VALUE<span class="token punctuation">;</span>

VAR 是变量名,VALUE 是目录;

嵌入其他配置文件

语法:include <span class="token operator">/</span>path<span class="token operator">/</span>file<span class="token punctuation">;</span>

include 配置项可以将其他配置文件嵌入到 Nginx 的 nginx<span class="token punctuation">.</span>conf 文件中;

pid 的文件路径

语法:pid path<span class="token operator">/</span>file<span class="token punctuation">;</span>

默认:pid logs<span class="token operator">/</span>nginx<span class="token punctuation">.</span>pid<span class="token punctuation">;</span>

保存 master 进程 ID 的 pid 文件存放路径;

Nginx worker 运行的用户及用户组

语法:user username <span class="token punctuation">[</span>groupname<span class="token punctuation">]</span><span class="token punctuation">;</span>

默认:user nobody nobody<span class="token punctuation">;</span>

指定 Nginx worker进程可打开最大句柄个数

语法:worker_rlimit_nofile limit<span class="token punctuation">;</span>

限制信号队列

语法:worker_rlimit_sigpending limit<span class="token punctuation">;</span>

设置每个用户发给 Nginx 的信号队列大小,超出则丢弃;

<span class="token comment">/* 优化性能配置项 */</span>

Nginx worker 进程的个数

语法:worker_process number<span class="token punctuation">;</span>

默认:worker_process <span class="token number">1</span><span class="token punctuation">;</span>

绑定 Nginx worker 进程到指定的 CPU 内核

语法:worker_cpu_affinity cpumask <span class="token punctuation">[</span>cpumask<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>

SSL 硬件加速

语法:ssl_engine device<span class="token punctuation">;</span>

系统调用 gettimeofday 的执行频率

语法:timer_resolution t<span class="token punctuation">;</span>

Nginx worker 进程优先级设置

语法:worker_priority nice<span class="token punctuation">;</span>

默认:worker_priority <span class="token number">0</span><span class="token punctuation">;</span>

<span class="token comment">/* 事件类配置项 */</span>

一般有以下几种配置:

<span class="token number">1</span>、是否打开accept锁

语法格式:accept_mutex <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">2</span>、lock文件的路径

语法格式:lock_file path<span class="token operator">/</span>file<span class="token punctuation">;</span>

<span class="token number">3</span>、使用accept锁后到真正建立连接之间的延迟时间

语法格式:accept_mutex_delay Nms<span class="token punctuation">;</span>

<span class="token number">4</span>、批量建立新连接

语法格式:multi_accept <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">5</span>、选择事件模型

语法格式:use <span class="token punctuation">[</span>kqueue <span class="token operator">|</span> rtisg <span class="token operator">|</span> epoll <span class="token operator">|</span> <span class="token operator">/</span>dev<span class="token operator">/</span>poll <span class="token operator">|</span> select <span class="token operator">|</span> poll <span class="token operator">|</span> eventport<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">6</span>、每个worker进行的最大连接数

语法格式:worker_connections number<span class="token punctuation">;</span>

</code></pre>
<p>HTTP 核心模块的配置</p>
<p>具体可以参看《<a href="http://nginx.org/en/docs/http/ngx_http_core_module.html" target="_blank">Nginx 中 HTTP 核心模块配置</a>》</p>
<pre><code class="ಠhighlight-container"><span class="token comment">/* HTTP 核心模块配置的功能 */</span>

<span class="token comment">/* 虚拟主机与请求分发 */</span>

监听端口

语法:listen address<span class="token punctuation">:</span>port<span class="token punctuation">[</span>default <span class="token operator">|</span> default_server <span class="token operator">|</span> <span class="token punctuation">[</span>backlong<span class="token operator">=</span>num <span class="token operator">|</span> rcvbuf<span class="token operator">=</span>size <span class="token operator">|</span> sndbuf<span class="token operator">=</span>size <span class="token operator">|</span>

accept_filter <span class="token operator">|</span> deferred <span class="token operator">|</span> bind <span class="token operator">|</span> ipv6only<span class="token operator">=</span><span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span> <span class="token operator">|</span> ssl<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

默认:listen<span class="token punctuation">:</span><span class="token number">80</span><span class="token punctuation">;</span>

说明:

default或default_server:将所在的server块作为web服务的默认server块;当请求无法匹配配置文件中的所有主机名时,就会选择默认的虚拟主机;

backlog<span class="token operator">=</span>num:表示 TCP 中backlog队列存放TCP新连接请求的大小,默认是<span class="token operator">-</span><span class="token number">1</span>,表示不予设置;

rcvbuf<span class="token operator">=</span>size:设置监听句柄SO_RCVBUF的参数;

sndbuf<span class="token operator">=</span>size:设置监听句柄SO_SNDBUF的参数;

accept_filter:设置accept过滤器,只对FreeBSD操作系统有用;

deferred:设置该参数后,若用户发起TCP连接请求,并且完成TCP三次握手,但是若用户没有发送数据,则不会唤醒worker进程,直到发送数据;

bind:绑定当前端口 <span class="token operator">/</span> 地址对,只有同时对一个端口监听多个地址时才会生效;

ssl:在当前端口建立的连接必须基于ssl协议;

配置块范围:server

主机名称

语法:server_name name<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

默认:server_name <span class="token string">""</span><span class="token punctuation">;</span>

配置块范围:server

server name 是使用散列表存储的

每个散列桶占用内存大小

语法:server_names_hash_bucket_size size<span class="token punctuation">;</span>

默认:server_names_hash_bucker_size <span class="token number">32</span><span class="token operator">|</span><span class="token number">64</span><span class="token operator">|</span><span class="token number">128</span><span class="token punctuation">;</span>

散列表最大bucket数量

语法:server_names_hash_max_size size<span class="token punctuation">;</span>

默认:server_names_hash_max_size <span class="token number">512</span><span class="token punctuation">;</span>

默认:server_name_in_redirect on<span class="token punctuation">;</span>

配置块范围:server、http、location

处理重定向主机名

语法:server_name_in_redirect on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:server_name_in_redirect on<span class="token punctuation">;</span>

配置块范围:server、http、location

location语法:location<span class="token punctuation">[</span><span class="token operator">=</span> <span class="token operator">|</span> <span class="token operator">~</span> <span class="token operator">|</span> <span class="token operator">~</span><span class="token operator">*</span> <span class="token operator">|</span> <span class="token operator">^</span><span class="token operator">~</span> <span class="token operator">|</span> @<span class="token punctuation">]</span> <span class="token operator">/</span>uri<span class="token operator">/</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

配置块范围:server

    #location尝试根据用户请求中的URI来匹配 <span class="token operator">/</span>uri表达式,若匹配成功,则执行<span class="token punctuation">{</span><span class="token punctuation">}</span>里面的配置来处理用户请求  

以下是location的一般配置项

<span class="token number">1</span>、以root方式设置资源路径

语法格式:root path<span class="token punctuation">;</span>

<span class="token number">2</span>、以alias方式设置资源路径

语法格式:alias path<span class="token punctuation">;</span>

<span class="token number">3</span>、访问首页

语法格式:index file<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>

<span class="token number">4</span>、根据HTTP返回码重定向页面

语法格式:error_page code <span class="token punctuation">[</span>code<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token operator">=</span> <span class="token operator">|</span> <span class="token operator">=</span>answer<span class="token operator">-</span>code<span class="token punctuation">]</span> uri <span class="token operator">|</span> @named_location<span class="token punctuation">;</span>

<span class="token number">5</span>、是否允许递归使用error_page

语法格式:recursive_error_pages <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">6</span>、try_files

语法格式:try_files path1 <span class="token punctuation">[</span>path2<span class="token punctuation">]</span> uri<span class="token punctuation">;</span>

<span class="token comment">/* 文件路径的定义 */</span>

root方式设置资源路径

语法:root path<span class="token punctuation">;</span>

默认:root html<span class="token punctuation">;</span>

配置块范围:server、http、location、<span class="token keyword">if</span>

以alias方式设置资源路径

语法:alias path<span class="token punctuation">;</span>

配置块范围:location

访问主页

语法:index file<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>

默认:index index<span class="token punctuation">.</span>html<span class="token punctuation">;</span>

配置块范围:http、server、location

根据HTTP返回码重定向页面

语法:error_page code <span class="token punctuation">[</span>code<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token operator">=</span> <span class="token operator">|</span> <span class="token operator">=</span>answer<span class="token operator">-</span>code<span class="token punctuation">]</span> uri <span class="token operator">|</span> @named_location<span class="token punctuation">;</span>

配置块范围:server、http、location、<span class="token keyword">if</span>

是否允许递归使用error_page

语法:recursive_error_pages <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

配置块范围:http、server、location

try_files

语法:try_files path1 <span class="token punctuation">[</span>path2<span class="token punctuation">]</span> uri<span class="token punctuation">;</span>

配置块范围:server、location

<span class="token comment">/* 内存及磁盘资源分配 */</span>

HTTP 包体只存储在磁盘文件中

语法:client_body_in_file_only on <span class="token operator">|</span> clean <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:client_body_in_file_only off<span class="token punctuation">;</span>

配置块范围:http、server、location

HTTP 包体尽量写入到一个内存buffer中

语法:client_body_single_buffer on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:client_body_single_buffer off<span class="token punctuation">;</span>

配置块范围:http、server、location

存储 HTTP 头部的内存buffer大小

语法:client_header_buffer_size size<span class="token punctuation">;</span>

默认:client_header_buffer_size <span class="token number">1</span>k<span class="token punctuation">;</span>

配置块范围:http、server

存储超大 HTTP 头部的内存buffer大小

语法:large_client_header_buffer_size number size<span class="token punctuation">;</span>

默认:large_client_header_buffer_size <span class="token number">4</span> <span class="token number">8</span>k<span class="token punctuation">;</span>

配置块范围:http、server

存储 HTTP 包体的内存buffer大小

语法:client_body_buffer_size size<span class="token punctuation">;</span>

默认:client_body_buffer_size <span class="token number">8</span>k<span class="token operator">/</span><span class="token number">16</span>k<span class="token punctuation">;</span>

配置块范围:http、server、location

HTTP 包体的临时存放目录

语法:client_body_temp_path dir<span class="token operator">-</span>path <span class="token punctuation">[</span>level1 <span class="token punctuation">[</span>level2 <span class="token punctuation">[</span>level3<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

默认:client_body_temp_path client_body_temp<span class="token punctuation">;</span>

配置块范围:http、server、location

存储 TCP 成功建立连接的内存池大小

语法:connection_pool_size size<span class="token punctuation">;</span>

默认:connection_pool_size <span class="token number">256</span><span class="token punctuation">;</span>

配置块范围:http、server

存储 TCP 请求连接的内存池大小

语法:request_pool_size size<span class="token punctuation">;</span>

默认:request_pool_size <span class="token number">4</span>k<span class="token punctuation">;</span>

配置块范围:http、server

<span class="token comment">/* 网络连接设置 */</span>

读取 HTTP 头部的超时时间

语法:client_header_timeout time<span class="token punctuation">;</span>

默认:client_header_timeout <span class="token number">60</span><span class="token punctuation">;</span>

配置块范围:http、server、location

读取 HTTP 包体的超时时间

语法:client_body_timeout time<span class="token punctuation">;</span>

默认:client_body_timeout <span class="token number">60</span><span class="token punctuation">;</span>

配置块范围:http、server、location

发送响应的超时时间

语法:send_timeout time<span class="token punctuation">;</span>

默认:send_timeout <span class="token number">60</span><span class="token punctuation">;</span>

配置块范围:http、server、location

TCP 连接的超时重置

语法:reset_timeout_connection on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:reset_timeout_connection off<span class="token punctuation">;</span>

配置块范围:http、server、location

控制关闭 TCP 连接的方式

语法:lingering_close off <span class="token operator">|</span> on <span class="token operator">|</span> always<span class="token punctuation">;</span>

默认:lingering_close on<span class="token punctuation">;</span>

配置块范围:http、server、location

always 表示关闭连接之前无条件处理连接上所有用户数据;

off 表示不处理;on 一般会处理;

lingering_time

语法:lingering_time time<span class="token punctuation">;</span>

默认:lingering_time <span class="token number">30</span>s<span class="token punctuation">;</span>

配置块范围:http、server、location

lingering_timeout

语法:lingering_timeout time<span class="token punctuation">;</span>

默认:lingering_time <span class="token number">5</span>s<span class="token punctuation">;</span>

配置块范围:http、server、location

对某些浏览器禁止keepalive功能

语法:keepalive_disable <span class="token punctuation">[</span>mise6 <span class="token operator">|</span> safari <span class="token operator">|</span> none<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

默认:keepalive_disable mise6 safari<span class="token punctuation">;</span>

配置块范围:http、server、location

keepalive超时时间

语法:keepalive_timeout time<span class="token punctuation">;</span>

默认:keepalive_timeout <span class="token number">75</span><span class="token punctuation">;</span>

配置块范围:http、server、location

keepalive长连接上允许最大请求数

语法:keepalive_requests n<span class="token punctuation">;</span>

默认:keepalive_requests <span class="token number">100</span><span class="token punctuation">;</span>

配置块范围:http、server、location

tcp_nodelay

语法:tcp_nodelay on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:tcp_nodelay on<span class="token punctuation">;</span>

配置块范围:http、server、location

tcp_nopush

语法:tcp_nopush on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:tcp_nopush off<span class="token punctuation">;</span>

配置块范围:http、server、location

<span class="token comment">/* MIME 类型设置 */</span>

MIME type 与文件扩展的映射

语法:type<span class="token punctuation">{</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">}</span>

配置块范围:http、server、location

多个扩展名可映射到同一个 MIME type

默认 MIME type

语法:default_type MIME<span class="token operator">-</span>type<span class="token punctuation">;</span>

默认:default_type text<span class="token operator">/</span>plain<span class="token punctuation">;</span>

配置块范围:http、server、location

type_hash_bucket_size

语法:type_hash_bucket_size size<span class="token punctuation">;</span>

默认:type_hash_bucket_size <span class="token number">32</span> <span class="token operator">|</span> <span class="token number">64</span> <span class="token operator">|</span> <span class="token number">128</span><span class="token punctuation">;</span>

配置块范围:http、server、location

type_hash_max_size

语法:type_hash_max_size size<span class="token punctuation">;</span>

默认:type_hash_max_size <span class="token number">1024</span><span class="token punctuation">;</span>

配置块范围:http、server、location

<span class="token comment">/* 限制客户端请求 */</span>

按 HTTP 方法名限制用户请求

语法:limit_except method<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">{</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">}</span>

配置块:location

method 的取值如下:

GET、HEAD、POST、PUT、DELETE、MKCOL、COPY、MOVE、OPTIONS、

PROPFIND、PROPPATCH、LOCK、UNLOCK、PATCH

HTTP 请求包体的最大值

语法:client_max_body_size size<span class="token punctuation">;</span>

默认:client_max_body_size <span class="token number">1</span>m<span class="token punctuation">;</span>

配置块范围:http、server、location

对请求限制速度

语法:limit_rate speed<span class="token punctuation">;</span>

默认:limit_rate <span class="token number">0</span><span class="token punctuation">;</span>

配置块范围:http、server、location、<span class="token keyword">if</span>

<span class="token number">0</span> 表示不限速

limit_rate_after规定时间后限速

语法:limit_rate_after time<span class="token punctuation">;</span>

默认:limit_rate_after <span class="token number">1</span>m<span class="token punctuation">;</span>

配置块范围:http、server、location、<span class="token keyword">if</span>

<span class="token comment">/* 文件操作的优化 */</span>

sendfile系统调用

语法:sendfile on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:sendfile off<span class="token punctuation">;</span>

配置块:http、server、location

AIO 系统调用

语法:aio on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:aio off<span class="token punctuation">;</span>

配置块:http、server、location

directio

语法:directio size <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:directio off<span class="token punctuation">;</span>

配置块:http、server、location

directio_alignment

语法:directio_alignment size<span class="token punctuation">;</span>

默认:directio_alignment <span class="token number">512</span><span class="token punctuation">;</span>

配置块:http、server、location

打开文件缓存

语法:open_file_cache max<span class="token operator">=</span>N <span class="token punctuation">[</span>inactive<span class="token operator">=</span>time<span class="token punctuation">]</span> <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:open_file_cache off<span class="token punctuation">;</span>

配置块:http、server、location

是否缓存打开文件的错误信息

语法:open_file_cache_errors on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:open_file_cache_errors off<span class="token punctuation">;</span>

配置块:http、server、location

不被淘汰的最小访问次数

语法:open_file_cache_min_user number<span class="token punctuation">;</span>

默认:open_file_cache_min_user <span class="token number">1</span><span class="token punctuation">;</span>

配置块:http、server、location

检验缓存中元素有效性的频率

语法:open_file_cache_valid time<span class="token punctuation">;</span>

默认:open_file_cache_valid <span class="token number">60</span>s<span class="token punctuation">;</span>

配置块:http、server、location

<span class="token comment">/* 客户请求的特殊处理 */</span>

忽略不合法的 HTTP 头部

语法:ignore_invalid_headers on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:ignore_invalid_headers on<span class="token punctuation">;</span>

配置块:http、server

HTTP 头部是否允许下划线

语法:underscores_in_headers on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:underscores_in_headers off<span class="token punctuation">;</span>

配置块:http、server

If_Modified_Since 头部的处理策略

语法:if_modified_since <span class="token punctuation">[</span>off <span class="token operator">|</span> exact <span class="token operator">|</span> before<span class="token punctuation">]</span>

默认:if_modified_since exact<span class="token punctuation">;</span>

配置块:http、server、location

文件未找到时是否记录到error日志

语法:log_not_found on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:log_not_found on<span class="token punctuation">;</span>

配置块:http、server、location

是否合并相邻的“<span class="token operator">/</span>”

语法:merge_slashes on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:merge_slashes on<span class="token punctuation">;</span>

配置块:http、server、location

DNS解析地址

语法:resolver address<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>

配置块:http、server、location

DNS解析的超时时间

语法:resolver_timeout time<span class="token punctuation">;</span>

默认:resolver_timeout <span class="token number">30</span>s<span class="token punctuation">;</span>

配置块:http、server、location

返回错误页面是否在server中注明Nginx版本

语法:server_tokens on <span class="token operator">|</span> off<span class="token punctuation">;</span>

默认:server_tokens on<span class="token punctuation">;</span>

配置块:http、server、location

</code></pre>
<p>以下是在 Ubuntu 12.04 系统成功安装 Nginx 之后的主配置文件:</p>
<pre><code class="ಠhighlight-container">#Nginx服务器正常启动时会读取该配置文件,以下的值都是默认的,若需要可自行修改;

以下是配置选项

Nginx worker进程运行的用户以及用户组

语法格式:user username<span class="token punctuation">[</span>groupname<span class="token punctuation">]</span>

user nobody<span class="token punctuation">;</span>

Nginx worker 进程个数

worker_processes <span class="token number">1</span><span class="token punctuation">;</span>

error 日志设置

语法格式:error <span class="token operator">/</span>path<span class="token operator">/</span>file level

其中<span class="token operator">/</span>path<span class="token operator">/</span>file是一个具体文件;level是日志的输出级别,其取值如下:

debug info notice warn error crit alert emerg<span class="token punctuation">,</span>从左至右级别增大;

若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;

error_log logs<span class="token operator">/</span>error<span class="token punctuation">.</span>log<span class="token punctuation">;</span>

error_log logs<span class="token operator">/</span>error<span class="token punctuation">.</span>log notice<span class="token punctuation">;</span>

error_log logs<span class="token operator">/</span>error<span class="token punctuation">.</span>log info<span class="token punctuation">;</span>

保存master进程ID的pid文件存放路径

语法格式:pid path<span class="token operator">/</span>file

pid logs<span class="token operator">/</span>nginx<span class="token punctuation">.</span>pid<span class="token punctuation">;</span>

事件类配置项

一般有以下几种配置:

<span class="token number">1</span>、是否打开accept锁

语法格式:accept_mutex <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">2</span>、lock文件的路径

语法格式:lock_file path<span class="token operator">/</span>file<span class="token punctuation">;</span>

<span class="token number">3</span>、使用accept锁后到真正建立连接之间的延迟时间

语法格式:accept_mutex_delay Nms<span class="token punctuation">;</span>

<span class="token number">4</span>、批量建立新连接

语法格式:multi_accept <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">5</span>、选择事件模型

语法格式:use <span class="token punctuation">[</span>kqueue <span class="token operator">|</span> rtisg <span class="token operator">|</span> epoll <span class="token operator">|</span> <span class="token operator">/</span>dev<span class="token operator">/</span>poll <span class="token operator">|</span> select <span class="token operator">|</span> poll <span class="token operator">|</span> eventport<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">6</span>、每个worker进行的最大连接数

语法格式:worker_connections number<span class="token punctuation">;</span>

events <span class="token punctuation">{</span>
worker_connections <span class="token number">1024</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

以下是http模块

http <span class="token punctuation">{</span>
include mime<span class="token punctuation">.</span>types<span class="token punctuation">;</span>
default_type application<span class="token operator">/</span>octet<span class="token operator">-</span>stream<span class="token punctuation">;</span>

#log_format  main  <span class="token string">'$remote_addr - $remote_user [$time_local] "$request" '</span>
#                  <span class="token string">'$status $body_bytes_sent "$http_referer" '</span>
#                  <span class="token string">'"$http_user_agent" "$http_x_forwarded_for"'</span><span class="token punctuation">;</span>

#access_log  logs<span class="token operator">/</span>access<span class="token punctuation">.</span>log  main<span class="token punctuation">;</span>

sendfile        on<span class="token punctuation">;</span>
#tcp_nopush     on<span class="token punctuation">;</span>

#keepalive_timeout  <span class="token number">0</span><span class="token punctuation">;</span>
keepalive_timeout  <span class="token number">65</span><span class="token punctuation">;</span>

#gzip  on<span class="token punctuation">;</span>

server块

每个server块就是一个虚拟主机,按照server_name来区分

server <span class="token punctuation">{</span>

监听端口

    listen       <span class="token number">80</span><span class="token punctuation">;</span>

主机名称

    server_name  localhost<span class="token punctuation">;</span>

    #charset koi8<span class="token operator">-</span>r<span class="token punctuation">;</span>

    #access_log  logs<span class="token operator">/</span>host<span class="token punctuation">.</span>access<span class="token punctuation">.</span>log  main<span class="token punctuation">;</span>

location语法:location<span class="token punctuation">[</span><span class="token operator">=</span> <span class="token operator">|</span> <span class="token operator">~</span> <span class="token operator">|</span> <span class="token operator">~</span><span class="token operator">*</span> <span class="token operator">|</span> <span class="token operator">^</span><span class="token operator">~</span> <span class="token operator">|</span> @<span class="token punctuation">]</span> <span class="token operator">/</span>uri<span class="token operator">/</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    #location尝试根据用户请求中的URI来匹配 <span class="token operator">/</span>uri表达式,若匹配成功,则执行<span class="token punctuation">{</span><span class="token punctuation">}</span>里面的配置来处理用户请求

以下是location的一般配置项

<span class="token number">1</span>、以root方式设置资源路径

语法格式:root path<span class="token punctuation">;</span>

<span class="token number">2</span>、以alias方式设置资源路径

语法格式:alias path<span class="token punctuation">;</span>

<span class="token number">3</span>、访问首页

语法格式:index file<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>

<span class="token number">4</span>、根据HTTP返回码重定向页面

语法格式:error_page code <span class="token punctuation">[</span>code<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token operator">=</span> <span class="token operator">|</span> <span class="token operator">=</span>answer<span class="token operator">-</span>code<span class="token punctuation">]</span> uri <span class="token operator">|</span> @named_location<span class="token punctuation">;</span>

<span class="token number">5</span>、是否允许递归使用error_page

语法格式:recursive_error_pages <span class="token punctuation">[</span>on <span class="token operator">|</span> off<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token number">6</span>、try_files

语法格式:try_files path1 <span class="token punctuation">[</span>path2<span class="token punctuation">]</span> uri<span class="token punctuation">;</span>

    location <span class="token operator">/</span> <span class="token punctuation">{</span>
        root   html<span class="token punctuation">;</span>
        index  index<span class="token punctuation">.</span>html index<span class="token punctuation">.</span>htm<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    #error_page  <span class="token number">404</span>              <span class="token operator">/</span><span class="token number">404.</span>html<span class="token punctuation">;</span>

    # redirect server error pages to the static page <span class="token operator">/</span><span class="token number">50</span>x<span class="token punctuation">.</span>html
    #
    error_page   <span class="token number">500</span> <span class="token number">502</span> <span class="token number">503</span> <span class="token number">504</span>  <span class="token operator">/</span><span class="token number">50</span>x<span class="token punctuation">.</span>html<span class="token punctuation">;</span>
    location <span class="token operator">=</span> <span class="token operator">/</span><span class="token number">50</span>x<span class="token punctuation">.</span>html <span class="token punctuation">{</span>
        root   html<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    # proxy the PHP scripts to Apache listening on <span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">80</span>
    #
    #location <span class="token operator">~</span> \<span class="token punctuation">.</span>php$ <span class="token punctuation">{</span>
    #    proxy_pass   http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">;</span>
    #<span class="token punctuation">}</span>

    # pass the PHP scripts to FastCGI server listening on <span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">9000</span>
    #
    #location <span class="token operator">~</span> \<span class="token punctuation">.</span>php$ <span class="token punctuation">{</span>
    #    root           html<span class="token punctuation">;</span>
    #    fastcgi_pass   <span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">9000</span><span class="token punctuation">;</span>
    #    fastcgi_index  index<span class="token punctuation">.</span>php<span class="token punctuation">;</span>
    #    fastcgi_param  SCRIPT_FILENAME  <span class="token operator">/</span>scripts$fastcgi_script_name<span class="token punctuation">;</span>
    #    include        fastcgi_params<span class="token punctuation">;</span>
    #<span class="token punctuation">}</span>

    # deny access to <span class="token punctuation">.</span>htaccess files<span class="token punctuation">,</span> <span class="token keyword">if</span> Apache's document root
    # concurs with nginx's one
    #
    #location <span class="token operator">~</span> <span class="token operator">/</span>\<span class="token punctuation">.</span>ht <span class="token punctuation">{</span>
    #    deny  all<span class="token punctuation">;</span>
    #<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

# another virtual host using mix of IP<span class="token operator">-</span><span class="token punctuation">,</span> name<span class="token operator">-</span><span class="token punctuation">,</span> and port<span class="token operator">-</span>based configuration
#
#server <span class="token punctuation">{</span>
#    listen       <span class="token number">8000</span><span class="token punctuation">;</span>
#    listen       somename<span class="token punctuation">:</span><span class="token number">8080</span><span class="token punctuation">;</span>
#    server_name  somename  alias  another<span class="token punctuation">.</span>alias<span class="token punctuation">;</span>

#    location <span class="token operator">/</span> <span class="token punctuation">{</span>
#        root   html<span class="token punctuation">;</span>
#        index  index<span class="token punctuation">.</span>html index<span class="token punctuation">.</span>htm<span class="token punctuation">;</span>
#    <span class="token punctuation">}</span>
#<span class="token punctuation">}</span>

# HTTPS server
#
#server <span class="token punctuation">{</span>
#    listen       <span class="token number">443</span> ssl<span class="token punctuation">;</span>
#    server_name  localhost<span class="token punctuation">;</span>

#    ssl_certificate      cert<span class="token punctuation">.</span>pem<span class="token punctuation">;</span>
#    ssl_certificate_key  cert<span class="token punctuation">.</span>key<span class="token punctuation">;</span>

#    ssl_session_cache    shared<span class="token punctuation">:</span>SSL<span class="token punctuation">:</span><span class="token number">1</span>m<span class="token punctuation">;</span>
#    ssl_session_timeout  <span class="token number">5</span>m<span class="token punctuation">;</span>

#    ssl_ciphers  HIGH<span class="token punctuation">:</span><span class="token operator">!</span>aNULL<span class="token punctuation">:</span><span class="token operator">!</span>MD5<span class="token punctuation">;</span>
#    ssl_prefer_server_ciphers  on<span class="token punctuation">;</span>

#    location <span class="token operator">/</span> <span class="token punctuation">{</span>
#        root   html<span class="token punctuation">;</span>
#        index  index<span class="token punctuation">.</span>html index<span class="token punctuation">.</span>htm<span class="token punctuation">;</span>
#    <span class="token punctuation">}</span>
#<span class="token punctuation">}</span>

<span class="token punctuation">}</span>

</code></pre>
<p>参考资料:</p>
<p>《深入理解Nginx》</p>
<p>《<a href="http://kb.cnblogs.com/page/98352/#section1-2" target="_blank">Nginx模块开发入门</a>》</p>
<p>《<a href="http://tengine.taobao.org/book/index.html" target="_blank">Nginx开发从入门到精通</a>》</p>
<p>https://www.kancloud.cn/digest/understandingnginx/202587</p>
<p><a href="https://mobile.yangkeduo.com/goods.html?goods_id=975456052" target="_blank">点击得好礼</a></p>
</div>

本文由作者pm1024:JAVA实验手册 发布,交流:583284584!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容