概述

Nginx 是使用一个 master 进程来管理多个 worker 进程提供服务。master 负责管理 worker 进程,而 worker 进程则提供真正的客户服务,worker 进程的数量一般跟服务器上 CPU 的核心数相同,worker 之间通过一些进程间通信机制实现负载均衡等功能。Nginx 进程之间的关系可由下图表示:

Nginx 服务启动时会读入配置文件,后续的行为则按照配置文件中的指令进行。Nginx 的配置文件是纯文本文件,默认安装 Nginx 后,其配置文件均在 /usr/local/nginx/conf/ 目录下。其中,nginx.conf  为主配置文件。配置文件中以 # 开始的行,或者是前面有若干空格或者 TAB 键,然后再跟 # 的行,都被认为是注释。这里只是了解主配置文件的结构。

Nginx 配置文件是以 block(块)形式组织,每个 block 都是以一个块名字和一对大括号 “{}” 表示组成,block 分为几个层级,整个配置文件为 main 层级,即最大的层级;在 main 层级下可以有 event、http 、mail 等层级,而 http 中又会有 server block,server block中可以包含 location block。即块之间是可以嵌套的,内层块继承外层块。最基本的配置项语法格式是“配置项名  配置项值1  配置项值2  配置项值3  ... ”;

每个层级可以有自己的指令(Directive),例如 worker_processes 是一个main层级指令,它指定 Nginx 服务的 Worker 进程数量。有的指令只能在一个层级中配置,如worker_processes 只能存在于 main 中,而有的指令可以存在于多个层级,在这种情况下,子 block 会继承 父 block 的配置,同时如果子block配置了与父block不同的指令,则会覆盖掉父 block 的配置。指令的格式是“指令名 参数1 参数2 … 参数N;”,注意参数间可用任意数量空格分隔,最后要加分号。

下图是 Nginx 配置文件通常结构图示。

Nginx 服务的基本配置项

Nginx 服务运行时,需要加载几个核心模块和一个事件模块,这些模块运行时所支持的配置项称为基本配置;基本配置项大概可分为以下四类:

  • 用于调试、定位的配置项;
  • 正常运行的必备配置项;
  • 优化性能的配置项;
  • 事件类配置项;

各个配置项的具体实现如下:

  1. /* Nginx 服务基本配置项 */
  2. /* 用于调试、定位的配置项 */
  3. #以守护进程 Nginx 运行方式
  4. #语法:daemon off | on;
  5. #默认:daemon on;
  6. #master / worker 工作方式
  7. #语法:master_process on | off;
  8. #默认:master_process on;
  9. #error 日志设置
  10. # 路径 错误级别
  11. #语法:error_log /path/file level;
  12. #默认:error_log logs/error.log error;
  13. #其中/path/file是一个具体文件;level是日志的输出级别,其取值如下:
  14. # debug info notice warn error crit alert emerg
  15. #从左至右级别增大;若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;
  16. #处理特殊调试点
  17. #语法:debug_points [stop | abort]
  18. #这个设置是来跟踪调试 Nginx 的;
  19. #仅对指定的客户端输出 debug 级别的日志
  20. #语法:debug_connection [IP | DIR]
  21. #限制 coredump 核心转储文件的大小
  22. #语法:worker_rlimit_core size;
  23. #指定 coredump 文件的生成目录
  24. #语法:working_directory path;
  25. /* 正常运行的配置项 */
  26. #定义环境变量
  27. #语法:env VAR | VAR=VALUE;
  28. #VAR 是变量名,VALUE 是目录;
  29. #嵌入其他配置文件
  30. #语法:include /path/file;
  31. #include 配置项可以将其他配置文件嵌入到 Nginx nginx.conf 文件中;
  32. #pid 的文件路径
  33. #语法:pid path/file;
  34. #默认:pid logs/nginx.pid;
  35. #保存 master 进程 ID 的 pid 文件存放路径;
  36. #Nginx worker 运行的用户及用户组
  37. #语法:user username [groupname];
  38. #默认:user nobody nobody;
  39. #指定 Nginx worker进程可打开最大句柄个数
  40. #语法:worker_rlimit_nofile limit;
  41. #限制信号队列
  42. #语法:worker_rlimit_sigpending limit;
  43. #设置每个用户发给 Nginx 的信号队列大小,超出则丢弃;
  44. /* 优化性能配置项 */
  45. #Nginx worker 进程的个数
  46. #语法:worker_process number;
  47. #默认:worker_process 1;
  48. #绑定 Nginx worker 进程到指定的 CPU 内核
  49. #语法:worker_cpu_affinity cpumask [cpumask...]
  50. #SSL 硬件加速
  51. #语法:ssl_engine device;
  52. #系统调用 gettimeofday 的执行频率
  53. #语法:timer_resolution t;
  54. #Nginx worker 进程优先级设置
  55. #语法:worker_priority nice;
  56. #默认:worker_priority 0;
  57. /* 事件类配置项 */
  58. #一般有以下几种配置:
  59. #1、是否打开accept锁
  60. # 语法格式:accept_mutex [on | off];
  61. #2、lock文件的路径
  62. # 语法格式:lock_file path/file;
  63. #3、使用accept锁后到真正建立连接之间的延迟时间
  64. # 语法格式:accept_mutex_delay Nms;
  65. #4、批量建立新连接
  66. # 语法格式:multi_accept [on | off];
  67. #
  68. #5、选择事件模型
  69. # 语法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];
  70. #6、每个worker进行的最大连接数
  71. # 语法格式:worker_connections number;

HTTP 核心模块的配置

具体可以参看《Nginx 中 HTTP 核心模块配置

  1. /* HTTP 核心模块配置的功能 */
  2. /* 虚拟主机与请求分发 */
  3. #监听端口
  4. #语法:listen address:port[default | default_server | [backlong=num | rcvbuf=size | sndbuf=size |
  5. # accept_filter | deferred | bind | ipv6only=[on | off] | ssl]];
  6. # 默认:listen:80;
  7. # 说明:
  8. # default或default_server:将所在的server块作为web服务的默认server块;当请求无法匹配配置文件中的所有主机名时,就会选择默认的虚拟主机;
  9. # backlog=num:表示 TCP 中backlog队列存放TCP新连接请求的大小,默认是-1,表示不予设置;
  10. # rcvbuf=size:设置监听句柄SO_RCVBUF的参数;
  11. # sndbuf=size:设置监听句柄SO_SNDBUF的参数;
  12. # accept_filter:设置accept过滤器,只对FreeBSD操作系统有用;
  13. # deferred:设置该参数后,若用户发起TCP连接请求,并且完成TCP三次握手,但是若用户没有发送数据,则不会唤醒worker进程,直到发送数据;
  14. # bind:绑定当前端口 / 地址对,只有同时对一个端口监听多个地址时才会生效;
  15. # ssl:在当前端口建立的连接必须基于ssl协议;
  16. #配置块范围:server
  17. #主机名称
  18. #语法:server_name name[...];
  19. #默认:server_name "";
  20. #配置块范围:server
  21. #server name 是使用散列表存储的
  22. #每个散列桶占用内存大小
  23. #语法:server_names_hash_bucket_size size;
  24. #默认:server_names_hash_bucker_size 32|64|128;
  25. #
  26. #散列表最大bucket数量
  27. #语法:server_names_hash_max_size size;
  28. #默认:server_names_hash_max_size 512;
  29. #默认:server_name_in_redirect on;
  30. #配置块范围:server、http、location
  31. #处理重定向主机名
  32. #语法:server_name_in_redirect on | off;
  33. #默认:server_name_in_redirect on;
  34. #配置块范围:server、http、location
  35. #location语法:location[= | ~ | ~* | ^~ | @] /uri/ {}
  36. #配置块范围:server
  37. #location尝试根据用户请求中的URI来匹配 /uri表达式,若匹配成功,则执行{}里面的配置来处理用户请求
  38. #以下是location的一般配置项
  39. #1、以root方式设置资源路径
  40. # 语法格式:root path;
  41. #2、以alias方式设置资源路径
  42. # 语法格式:alias path;
  43. #3、访问首页
  44. # 语法格式:index file...;
  45. #4、根据HTTP返回码重定向页面
  46. # 语法格式:error_page code [code...] [= | =answer-code] uri | @named_location;
  47. #5、是否允许递归使用error_page
  48. # 语法格式:recursive_error_pages [on | off];
  49. #6、try_files
  50. # 语法格式:try_files path1 [path2] uri;
  51. /* 文件路径的定义 */
  52. #root方式设置资源路径
  53. #语法:root path;
  54. #默认:root html;
  55. #配置块范围:server、http、location、if
  56. #以alias方式设置资源路径
  57. #语法:alias path;
  58. #配置块范围:location
  59. #访问主页
  60. #语法:index file...;
  61. #默认:index index.html;
  62. #配置块范围:http、server、location
  63. #根据HTTP返回码重定向页面
  64. # 语法:error_page code [code...] [= | =answer-code] uri | @named_location;
  65. #配置块范围:server、http、location、if
  66. #是否允许递归使用error_page
  67. # 语法:recursive_error_pages [on | off];
  68. #配置块范围:http、server、location
  69. #try_files
  70. # 语法:try_files path1 [path2] uri;
  71. #配置块范围:server、location
  72. /* 内存及磁盘资源分配 */
  73. # HTTP 包体只存储在磁盘文件中
  74. # 语法:client_body_in_file_only on | clean | off;
  75. # 默认:client_body_in_file_only off;
  76. # 配置块范围:http、server、location
  77. # HTTP 包体尽量写入到一个内存buffer中
  78. # 语法:client_body_single_buffer on | off;
  79. # 默认:client_body_single_buffer off;
  80. # 配置块范围:http、server、location
  81. # 存储 HTTP 头部的内存buffer大小
  82. # 语法:client_header_buffer_size size;
  83. # 默认:client_header_buffer_size 1k;
  84. # 配置块范围:http、server
  85. # 存储超大 HTTP 头部的内存buffer大小
  86. # 语法:large_client_header_buffer_size number size;
  87. # 默认:large_client_header_buffer_size 4 8k;
  88. # 配置块范围:http、server
  89. # 存储 HTTP 包体的内存buffer大小
  90. # 语法:client_body_buffer_size size;
  91. # 默认:client_body_buffer_size 8k/16k;
  92. # 配置块范围:http、server、location
  93. # HTTP 包体的临时存放目录
  94. # 语法:client_body_temp_path dir-path [level1 [level2 [level3]]];
  95. # 默认:client_body_temp_path client_body_temp;
  96. # 配置块范围:http、server、location
  97. # 存储 TCP 成功建立连接的内存池大小
  98. # 语法:connection_pool_size size;
  99. # 默认:connection_pool_size 256;
  100. # 配置块范围:http、server
  101. # 存储 TCP 请求连接的内存池大小
  102. # 语法:request_pool_size size;
  103. # 默认:request_pool_size 4k;
  104. # 配置块范围:http、server
  105. /* 网络连接设置 */
  106. # 读取 HTTP 头部的超时时间
  107. # 语法:client_header_timeout time;
  108. # 默认:client_header_timeout 60;
  109. # 配置块范围:http、server、location
  110. # 读取 HTTP 包体的超时时间
  111. # 语法:client_body_timeout time;
  112. # 默认:client_body_timeout 60;
  113. # 配置块范围:http、server、location
  114. # 发送响应的超时时间
  115. # 语法:send_timeout time;
  116. # 默认:send_timeout 60;
  117. # 配置块范围:http、server、location
  118. # TCP 连接的超时重置
  119. # 语法:reset_timeout_connection on | off;
  120. # 默认:reset_timeout_connection off;
  121. # 配置块范围:http、server、location
  122. # 控制关闭 TCP 连接的方式
  123. # 语法:lingering_close off | on | always;
  124. # 默认:lingering_close on;
  125. # 配置块范围:http、server、location
  126. # always 表示关闭连接之前无条件处理连接上所有用户数据;
  127. # off 表示不处理;on 一般会处理;
  128. # lingering_time
  129. # 语法:lingering_time time;
  130. # 默认:lingering_time 30s;
  131. # 配置块范围:http、server、location
  132. # lingering_timeout
  133. # 语法:lingering_timeout time;
  134. # 默认:lingering_time 5s;
  135. # 配置块范围:http、server、location
  136. # 对某些浏览器禁止keepalive功能
  137. # 语法:keepalive_disable [mise6 | safari | none]...
  138. # 默认:keepalive_disable mise6 safari;
  139. # 配置块范围:http、server、location
  140. # keepalive超时时间
  141. # 语法:keepalive_timeout time;
  142. # 默认:keepalive_timeout 75;
  143. # 配置块范围:http、server、location
  144. # keepalive长连接上允许最大请求数
  145. # 语法:keepalive_requests n;
  146. # 默认:keepalive_requests 100;
  147. # 配置块范围:http、server、location
  148. # tcp_nodelay
  149. # 语法:tcp_nodelay on | off;
  150. # 默认:tcp_nodelay on;
  151. # 配置块范围:http、server、location
  152. # tcp_nopush
  153. # 语法:tcp_nopush on | off;
  154. # 默认:tcp_nopush off;
  155. # 配置块范围:http、server、location
  156. /* MIME 类型设置 */
  157. # MIME type 与文件扩展的映射
  158. # 语法:type{...}
  159. # 配置块范围:http、server、location
  160. # 多个扩展名可映射到同一个 MIME type
  161. # 默认 MIME type
  162. # 语法:default_type MIME-type;
  163. # 默认:default_type text/plain;
  164. # 配置块范围:http、server、location
  165. # type_hash_bucket_size
  166. # 语法:type_hash_bucket_size size;
  167. # 默认:type_hash_bucket_size 32 | 64 | 128;
  168. # 配置块范围:http、server、location
  169. # type_hash_max_size
  170. # 语法:type_hash_max_size size;
  171. # 默认:type_hash_max_size 1024;
  172. # 配置块范围:http、server、location
  173. /* 限制客户端请求 */
  174. # 按 HTTP 方法名限制用户请求
  175. # 语法:limit_except method...{...}
  176. # 配置块:location
  177. # method 的取值如下:
  178. # GET、HEAD、POST、PUT、DELETE、MKCOL、COPY、MOVE、OPTIONS、
  179. # PROPFIND、PROPPATCH、LOCK、UNLOCK、PATCH
  180. # HTTP 请求包体的最大值
  181. # 语法:client_max_body_size size;
  182. # 默认:client_max_body_size 1m;
  183. # 配置块范围:http、server、location
  184. # 对请求限制速度
  185. # 语法:limit_rate speed;
  186. # 默认:limit_rate 0;
  187. # 配置块范围:http、server、location、if
  188. # 0 表示不限速
  189. # limit_rate_after规定时间后限速
  190. # 语法:limit_rate_after time;
  191. # 默认:limit_rate_after 1m;
  192. # 配置块范围:http、server、location、if
  193. /* 文件操作的优化 */
  194. # sendfile系统调用
  195. # 语法:sendfile on | off;
  196. # 默认:sendfile off;
  197. # 配置块:http、server、location
  198. # AIO 系统调用
  199. # 语法:aio on | off;
  200. # 默认:aio off;
  201. # 配置块:http、server、location
  202. # directio
  203. # 语法:directio size | off;
  204. # 默认:directio off;
  205. # 配置块:http、server、location
  206. # directio_alignment
  207. # 语法:directio_alignment size;
  208. # 默认:directio_alignment 512;
  209. # 配置块:http、server、location
  210. # 打开文件缓存
  211. # 语法:open_file_cache max=N [inactive=time] | off;
  212. # 默认:open_file_cache off;
  213. # 配置块:http、server、location
  214. # 是否缓存打开文件的错误信息
  215. # 语法:open_file_cache_errors on | off;
  216. # 默认:open_file_cache_errors off;
  217. # 配置块:http、server、location
  218. # 不被淘汰的最小访问次数
  219. # 语法:open_file_cache_min_user number;
  220. # 默认:open_file_cache_min_user 1;
  221. # 配置块:http、server、location
  222. # 检验缓存中元素有效性的频率
  223. # 语法:open_file_cache_valid time;
  224. # 默认:open_file_cache_valid 60s;
  225. # 配置块:http、server、location
  226. /* 客户请求的特殊处理 */
  227. # 忽略不合法的 HTTP 头部
  228. # 语法:ignore_invalid_headers on | off;
  229. # 默认:ignore_invalid_headers on;
  230. # 配置块:http、server
  231. # HTTP 头部是否允许下划线
  232. # 语法:underscores_in_headers on | off;
  233. # 默认:underscores_in_headers off;
  234. # 配置块:http、server
  235. # If_Modified_Since 头部的处理策略
  236. # 语法:if_modified_since [off | exact | before]
  237. # 默认:if_modified_since exact;
  238. # 配置块:http、server、location
  239. # 文件未找到时是否记录到error日志
  240. # 语法:log_not_found on | off;
  241. # 默认:log_not_found on;
  242. # 配置块:http、server、location
  243. # 是否合并相邻的“/
  244. # 语法:merge_slashes on | off;
  245. # 默认:merge_slashes on;
  246. # 配置块:http、server、location
  247. # DNS解析地址
  248. # 语法:resolver address...;
  249. # 配置块:http、server、location
  250. # DNS解析的超时时间
  251. # 语法:resolver_timeout time;
  252. # 默认:resolver_timeout 30s;
  253. # 配置块:http、server、location
  254. # 返回错误页面是否在server中注明Nginx版本
  255. # 语法:server_tokens on | off;
  256. # 默认:server_tokens on;
  257. # 配置块:http、server、location

以下是在 Ubuntu 12.04 系统成功安装 Nginx 之后的主配置文件:

  1. #Nginx服务器正常启动时会读取该配置文件,以下的值都是默认的,若需要可自行修改;
  2. #以下是配置选项
  3. #Nginx worker进程运行的用户以及用户组
  4. #语法格式:user username[groupname]
  5. #user nobody;
  6. #Nginx worker 进程个数
  7. worker_processes 1;
  8. #error 日志设置
  9. #语法格式:error /path/file level
  10. #其中/path/file是一个具体文件;level是日志的输出级别,其取值如下:
  11. #debug info notice warn error crit alert emerg,从左至右级别增大;
  12. #若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;
  13. #error_log logs/error.log;
  14. #error_log logs/error.log notice;
  15. #error_log logs/error.log info;
  16. #保存master进程ID的pid文件存放路径
  17. #语法格式:pid path/file
  18. #pid logs/nginx.pid;
  19. #事件类配置项
  20. #一般有以下几种配置:
  21. #1、是否打开accept锁
  22. # 语法格式:accept_mutex [on | off];
  23. #2、lock文件的路径
  24. # 语法格式:lock_file path/file;
  25. #3、使用accept锁后到真正建立连接之间的延迟时间
  26. # 语法格式:accept_mutex_delay Nms;
  27. #4、批量建立新连接
  28. # 语法格式:multi_accept [on | off];
  29. #5、选择事件模型
  30. # 语法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];
  31. #6、每个worker进行的最大连接数
  32. # 语法格式:worker_connections number;
  33. events {
  34. worker_connections 1024;
  35. }
  36. #以下是http模块
  37. http {
  38. include mime.types;
  39. default_type application/octet-stream;
  40. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  41. # '$status $body_bytes_sent "$http_referer" '
  42. # '"$http_user_agent" "$http_x_forwarded_for"';
  43. #access_log logs/access.log main;
  44. sendfile on;
  45. #tcp_nopush on;
  46. #keepalive_timeout 0;
  47. keepalive_timeout 65;
  48. #gzip on;
  49. #server块
  50. # 每个server块就是一个虚拟主机,按照server_name来区分
  51. server {
  52. #监听端口
  53. listen 80;
  54. #主机名称
  55. server_name localhost;
  56. #charset koi8-r;
  57. #access_log logs/host.access.log main;
  58. #location语法:location[= | ~ | ~* | ^~ | @] /uri/ {}
  59. #location尝试根据用户请求中的URI来匹配 /uri表达式,若匹配成功,则执行{}里面的配置来处理用户请求
  60. #以下是location的一般配置项
  61. #1、以root方式设置资源路径
  62. # 语法格式:root path;
  63. #2、以alias方式设置资源路径
  64. # 语法格式:alias path;
  65. #3、访问首页
  66. # 语法格式:index file...;
  67. #4、根据HTTP返回码重定向页面
  68. # 语法格式:error_page code [code...] [= | =answer-code] uri | @named_location;
  69. #5、是否允许递归使用error_page
  70. # 语法格式:recursive_error_pages [on | off];
  71. #6、try_files
  72. # 语法格式:try_files path1 [path2] uri;
  73. location / {
  74. root html;
  75. index index.html index.htm;
  76. }
  77. #error_page 404 /404.html;
  78. # redirect server error pages to the static page /50x.html
  79. #
  80. error_page 500 502 503 504 /50x.html;
  81. location = /50x.html {
  82. root html;
  83. }
  84. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  85. #
  86. #location ~ \.php$ {
  87. # proxy_pass http://127.0.0.1;
  88. #}
  89. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  90. #
  91. #location ~ \.php$ {
  92. # root html;
  93. # fastcgi_pass 127.0.0.1:9000;
  94. # fastcgi_index index.php;
  95. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  96. # include fastcgi_params;
  97. #}
  98. # deny access to .htaccess files, if Apache's document root
  99. # concurs with nginx's one
  100. #
  101. #location ~ /\.ht {
  102. # deny all;
  103. #}
  104. }
  105. # another virtual host using mix of IP-, name-, and port-based configuration
  106. #
  107. #server {
  108. # listen 8000;
  109. # listen somename:8080;
  110. # server_name somename alias another.alias;
  111. # location / {
  112. # root html;
  113. # index index.html index.htm;
  114. # }
  115. #}
  116. # HTTPS server
  117. #
  118. #server {
  119. # listen 443 ssl;
  120. # server_name localhost;
  121. # ssl_certificate cert.pem;
  122. # ssl_certificate_key cert.key;
  123. # ssl_session_cache shared:SSL:1m;
  124. # ssl_session_timeout 5m;
  125. # ssl_ciphers HIGH:!aNULL:!MD5;
  126. # ssl_prefer_server_ciphers on;
  127. # location / {
  128. # root html;
  129. # index index.html index.htm;
  130. # }
  131. #}
  132. }

参考资料:

《深入理解Nginx》

Nginx模块开发入门

Nginx开发从入门到精通

https://www.kancloud.cn/digest/understandingnginx/202587

点击得好礼