Nginx — 配置文件详细解读(一)
Nginx是一款免费、开源、性能强大且非常流行的服务器,主要有三个功能:Web Server , Reverse Proxy Server , IMAP or POP3 Proxy Server 。Nginx是为了解决互联网业内著名的 “C10K” 问题而生,且因其具有丰富的特性、极其强大的性能、配置简单、工作稳定及资源占用低等特点而闻名于世。
Nginx的主要功能有:作为Web服务器可与httpd服务一样提供静态的Web资源,结合FastCGI/uwSGI/SCGI等协议反代动态资源请求,http/https协议的反向代理,imap4/pop3协议的反向代理,tcp/udp协议的请求转发(四层转发)。
Nginx在功能丰富的同时,其配置文件种类非常多。但相对于httpd的配置文件来说,Nginx配置文件逻辑严谨,条理清晰,配置方法相对简单。
- 【注】本文全部示例以CentOS7.3作为运行环境,使用Nginx版本为EPEL源的Nginx-1.10.2。
Nginx配置详解
配置文件的组成部分
- 主配置文件:/etc/nginx/nginx.conf
两个配置段文件目录
include conf.d/*.conf
include default.d/*.conf
配置指令格式及注意要点
directive value [value2 ...];
* ==编写配置文件注意要点==*
1. 所有指令必须以分号结尾;
2. Nginx配置文件支持使用配置变量
内建变量:由Nginx模块引入,可直接引用
自定义变量:使用set指令定义的变量;
3. 指令有使用位置要求,只有在适用的上下文中才能使用;
4. 某些指令(如root、alias)可以在不同的多个上下文中同时出现时,优先级高的生效,优先级低的不生效,注意指令执行或匹配的优先级;
5. 一个配置文件一般仅存在一个http配置段,一个http配置段可包含多个server虚拟主机配置段,一个server配置段内可存在多个location配置段;
6. 本文采用和Nginx官方文档类似的格式对配置指令进行解读,格式如下:
- Syntax :语法|句法,使用方法
- Default:默认value,在没有显式指定时该指令的默认值
- Context:上下文,即本指令可以在哪个配置段中使用,可能为多个
- Example:示例,对本指令如何的简单示例
- Notes :注意|注释 ,注意要点或使用方法补充
- #注释内容 :代表非正文注释
配置文件结构
#主配置段,定义全局配置
main block {
#事件驱动相关配置段
events {
...
}
#Web服务及Reverse Proxy相关服务配置段
http {
...
#虚拟主机配置段
server {
...
#URI特性定义配置段
location /URI {
...
}
}
}
#邮件服务配置段
mail {
...
}
#stream模块相关配置段(四层转发)
stream {
...
}
}
主配置文件详解
因为Nginx丰富的特性,使得配置文件中指令繁多且组合方式不胜枚举,为了方便梳理整体结构,对主配置文件中的各类配置指令进行分类。
主配置文件位置:/etc/nginx/nginx.conf
- 第一类:Nginx正常运行必备的基本配置
- 第二类:优化Nginx工作性能的配置
- 第三类:events相关配置
- 第四类:用于Nginx调试和问题定位的相关配置
第一类:Nginx正常运行必备的基本配置
1. user :指定worker进程的用户和组
- Syntax :==user USER_NAME [GROUP_NAME];==
- Default :user nobody nobody;
- Context :main
- Example :user nginx nginx;
2. pid :指定nginx进程的pid文件
- Syntax :pid /PATH/TO/pid_file;
- Default :pid nginx.pid;
- Context :main
- Example :pid /var/run/nginx/nginx.pid;
3. worker_rlimit_nofile :设置全部worker进程一共能打开的最大文件句柄数
- Syntax :==worker_rlimit_nofile #;==
- Default:——
- Context:main
- Example:worker_rlimit_nofile 10240;
- Notes :本指令设置的数值大小即单个worker进程所能响应的并发连接数的上限
第二类:优化Nginx性能的相关配置
1. worker_processes :设置worker进程的个数
- Syntax :==worker_processes #|auto;==
- Default :worker_processed 1;
- Context :main
- Example :worker_processes 3;
- Notes :auto代表程序自动检测硬件后选择,此数值应该小于等于服务器CPU物理核芯的颗数,否则会起到反面效果。
2. worker_cpu_affinity :配置cpu绑定
- Syntax1 :worker_cpu_affinity CPU_MASK …;
- Syntax2 :==worker_cpu_affinity auto;==
- Default :——
- Context :main
- Example1:worker_cpu_affinity 00000001 00000010 00000100 #绑定第1、2、3颗cpu
- Example2:worker_cpu_affinity auto; #自动选择绑定
- Notes :建议使用auto ,此配置可提升cpu缓存的命中率,提高性能
- time_resolution :计算器解析度
- Syntax :time_resolution INTERVAL;
- Default :——
- Context :main
- Example :time_resolution 100ms;
- Notes :降低此值可减少gettimeofday()系统调用的次数,提高性能
4. worker_priority :指明worker进程的nice值
- Syntax :==worker_priority #;==
- Default :worker_priority 0;
- Context :main
- Example :worker_priority -5;
- Notes :数值范围:-20~20,nice值越小,优先被调度的等级越高
第三类 :events相关配置
- accept_mutex :主控进程master调度用户的请求至各worker进程时使用的负载均衡锁
- Syntax :accept_mutex {on|off};
- Default :off
- Context :events
- Example :accept_mutex off;
- Notes1 :on 表示能让多个worker轮流的、序列化的响应新的请求,off表示多个worker进程一同接收master的调度,哪个worker接收到,其他的worker则不接收。
* Notes2 :推荐使用off选项,on选项保证了每个worker从master调度后接收到的请求数几乎相同,即保证了各个worker的起点平衡;off选项保证了接收调度的请求速度快的worker能优先接收,因每个用户请求的处理时间不同,会导致某些worker会比较空闲,有些worker会比较繁忙,一般来说,空闲的worker会比繁忙的worker接收的速度快,off选项保证了各个worker进程的终点平衡。*
lock_file :设置锁文件的路径
- Syntax :lock_file FILE;
- Default :lock_file logs/nginx.lock;
- Context :main
- Example :lock_file logs/nginx.lock;
- Notes :指accept_mutex用到的锁文件路径
use :指明使用的事件模型
- Syntax :use METHOD; #METHOD为{epoll|rtsig|select|poll}
- Default :——
- Context :events
- Example :use epoll;
- Notes :此项设置建议让nginx自动选择
4. worker_connections :配置单个worker进程能处理的最大并发连接数量
- Syntax :==worker_connections #;==
- Default :worker_connections 512;
- Context :events
- Example :worker_connections 10240;
Notes1 :此数值不能超过 “worker_rlimit_nofile” 指令设置的值
Notes2 :nginx能响应的所有并发连接数为:单个worker进程能响应的数量 * worker进程数量
- Notes3 :该项设置需根据服务器的实际性能设置,不可设置过大(会起到反面效果)或过小(资源过剩导致浪费)。
第四类:用于Nginx调试和问题定位的相关配置
daemon :Nginx是否以守护进程方式运行的开关选项
- Syntax :daemon {on|off};
- Default :on
- Context :main
- Example :daemon on;
- Notes :调试时应该设置为off,其他情况下应该设置为on
master_process :Nginx是否以master/worker模型来运行的开关选项
- Syntax :master_process {on|off};
- Default :on
- Context :main
- Example :master_process on;
- Notes :调试时可以设置为off
error_log :错误日志存储位置和记录的启用级别
- Syntax :error_log FILE [level];
- Default :error_log logs/error.log error;
- Context :main, http, mail, stream, server, location
- Example :error_log /var/logs/nginx/error.log error;
- Notes :若要使用debug级别,需要在编译时增加 ” –with-debug ” 选项
Nginx的Web Server和Reverse Proxy Server配置详解
Nginx配置也采用了流行的类似容器的片段式配置分段方法,各虚拟机的配置可保存在一个单独的以 “.conf” 为后缀的文件,通过主配置文件中的 “include” 指令加以调用,使配置文件可以更加灵活的启用或停用。Nginx默认的配置段目录有两个:”/etc/nginx/conf.d/” 和 “/etc/nginx/default.d” 。
Nginx以强大的Web服务和Reverse Proxy服务的高并发响应能力闻名,有了以上配置的基础,我们来见识下Nginx最出名的服务的配置方法。本部分以模块的不同对配置指令进行分类
配置按模块分类
1. http模块相关配置
ngx_http_core_module模块
- 访问控制模块
- ngx_http_access_module模块
ngx_http_auth_basic_module模块
- 状态页、日志、压缩模块
- ngx_http_stub_status_module模块
- ngx_http_log_module模块
- ngx_http_gzip_module
4. https模块
ngx_http_ssl_module模块
- URL重定向模块
ngx_http_rewrite_module模块
- 链接引用模块
ngx_http_referer_module模块
- 响应报文首部修改模块
- ngx_http_headers_module模块
8. 反向代理模块
- ngx_http_proxy_module模块
- ngx_http_fastcgi_module模块
http模块相关配置
与套接字相关的配置
- server :虚拟主机配置
- Syntax :==server {…}==
- Default :——
- Context :http
- Example
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
Notes :Nginx中的虚拟主机与httpd类似,也可以基于IP、PORT、SERVER_NAME三种方式建立不同的虚拟主机。
- listen :监听端口
- Syntax :==listen ADDRESS:PROT [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size];==
- Default :listen {(:80)|(:8080)};
- Context :server
- Example1:listen 80 default_server;
- Example2:listen *:8080;
- Example3:listen 127.0.0.1:443 ssl;
- Example4:listen localhost:8000;
Notes :default_server:设定为默认虚拟主机;
ssl:限制仅能够通过ssl连接提供服务;
backlog=number:后援队列长度;
rcvbuf=size:接收缓冲区大小;
sndbuf=size:发送缓冲区大小;- server_name :设置虚拟主机名称
- Syntax :==server_nmae NAME;==
- Default :server_nmae “”;
- Context :server
- Example :server_name www.achudk.com;
- Notes1 :支持通配任意长度的任意字符;server_name .magedu.com www.magedu.*;支持~起始的字符做正则表达式模式匹配;server_name ~^www\d+\.magedu\.com$
Notes2 :匹配机制:
(1) 首先是字符串精确匹配;
(2) 左侧*通配符;
(3) 右侧*通配符;
(4) 正则表达式.- tcp_nodelay :在keepalived模式下的连接是否启用TCP_NODELAY选项
- Syntax :tcp_nodelay on | off;
- Default :tcp_nodelay off;
- Context :http, server, location
- Example :tcp_nodelay on;
Notes :建议设置为on,设置为off时虽然会节省服务器端资源,但会降低用户体验。
- tcp_nopush :在sendfile模式下,是否启用TCP_CORK选项
- Syntax :tcp_nopush on|off;
- Default :tcp_nopush off;
- Context :http, server, location
- Example :tcp_nopush on;
Notes :建议设置为on,该选项生效的前提是sendfile功能已启用。
- sendfile :是否启用sendfile功能
- Syntax :sendfile on | off;
- Default :sendfile off;
- Context :http, server, location, if in location
- Example :sendfile on;
- Notes :建议设置为on 。
定义URI映射路径相关的配置
root :设置web服务URL资源映射的本地文件系统的资源所在的目录
- Syntax :==root /PATH/OF/ROOT;==
- Default :root html;
- Context :http, server, location, if in location
- Example :root /web/nginx;
- Notes :注意root与alias的区别。
==location :设置请求的URI的属性和功能特性==
- Syntax :location [ = | ~ | ~* | ^~ ] uri { … }
- Syntax :location @name { … }
- Default :——
- Context :server,location
- Example :这里引用官网上的一个示例:
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
Notes1 :The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.
Notes2 :在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;nginx会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置。
Notes3 :
匹配符号:
~:对URI做正则表达式模式匹配,区分字符大小写;
~*:对URI做正则表达式模式匹配,不区分字符大小写;
^~:对URI的左半部分做匹配检查,不区分字符大小写;
不带符号:匹配起始于此uri的所有的url;
匹配优先级:
从高到低依次为:=, ^~, ~/~*,不带符号;
- alias :定义路径别名
- Syntax :alias /PATH/TO/ALIAS_DIR;
- Default :——
- Context :location
- Example1:如果请求host/abc/1.jpg,则用/data/w3/images/1.jpg响应请求。
location /abc/ {
alias /data/w3/images/;
}
- Example2:如果请求host/abc/1.jpg,则用/data/w3/images/abc/1.jpg响应请求。
location /abc/ {
root /data/w3/images/;
}
- Notes1 :==root和alias二者映射的路径的异同点==
相同点:都是从给定的路径(/web/html/或/vhost/picture)的右侧开始匹配;
不同点:root从给出的路径(/web/html/)的右侧匹配访问时输入的的URL(/images)路径的左侧,alias从给出的路径(/vhost/picture)的右侧匹配访问时输入的URL(/images)路径的右侧。
- Notes2 :注意URI和映射的路径的格式要对应:
如果给定的URL末端有” / ” ,则下面的root或alias的路径末端也必须有” / “,如果没有” / ” 则都应该没有。
index :定义默认主页面
- Syntax :index index.{html|php|htm|…};
- Default :——
- Context :server
- Example :index index.html index.php;
error_page 指定错误页面
- Syntax :error_page code […] [=code] URI;
- Default :——
- Context :http, server, location, if in location
- Example :
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
- Notes :[=code] :以指定的响应码进行响应,而不是默认的原来的响应,默认表示以新资源的响应码为其响应码.
定义客户端请求的相关配置
keepalive_timeout :设定保持连接的超时时长
- Syntax :keepalive_timeout TIMEOUT [head_timeout];
- Default :75s
- Context :http, server, location
- Example :keepalive_timeout 60s;
- Notes :值为0代表禁止长连接
keepalive_requests :设定单次长连接所允许请求的资源的最大数量
- Syntax :keepalive_requests #;
- Default :100
- Context :http, server, location
- Example :keepalive_requests 70;
keepalive_disable :设定对哪种浏览器禁用长连接
- Syntax :keepalive_disable none | brower …;
- Default :keepalive_disable msie6;
- Context :http, server, location
- Example :keepalive_disable msie6;
- Notes :禁用是因为某些浏览器不支持此功能
send_timeout :设定向客户端发送响应报文的超时时长
- Syntax :send_timeout TIMEout;
- Default :send_timeout 60s;
- Context :http, server, location
- Example :send_timeout 90s;
- Notes :此选项是指两次写操作之间的间隔时长
client_body_buffer_size :设定接收客户端请求报文的body部分的缓冲区的大小
- Syntax :client_body_buffer_size SIZE;
- Default :client_header_buffer_size 1k
- Context :http, server
- Example :client_header_buffer_size 10k
- Notes :超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置
client_body_temp_path :设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量
- Syntax :client_body_temp_path /TMP/PATH [level1 [level2 [level3]]];
- Default :client_body_temp_path client_body_temp;
- Context :http, server, location
- Example :client_body_temp_path /var/tmp/client_body 1 2 2;
- Notes :1:表示用一位16进制数字表示一级子目录;0-f
2:表示用2位16进程数字表示二级子目录:00-ff
2:表示用2位16进程数字表示三级子目录:00-ff
对客户端进行限制的相关配置
- limit_rate :限制响应给客户端的传输速率,单位是bytes/second
- Syntax :limit_rate RATE;
- Default :limit_rate 0;
- Context :http, server, location, if in location
- Example :
server {
if ($slow) {
set $limit_rate 4k;
}
...
}
Notes :值为0表示不设限制
- limit_except :限定对指定客户端不准使用的请求方法
- Syntax :limit_except METHOD … {…};
- Default :——
- Context :location
- Example :
limit_except GET {
allow 192.168.1.0/24;
deny all;
}
文件操作优化的配置
- aio :设定异步I/O功能的开关选项
- Syntax :aio on | off | threads[=pool];
- Default :aio off;
- Context :http, server, location
- Example :
location /video/ {
aio on;
output_buffers 1 64k;
}
direcrio :在Linux主机启用O_DIRECT标记,
- Syntax :directio size | off;
- Default :directio off;
- Context :http, server, location
- Example :directio 4m;
- Notes :当一个文件较大时,正在读取的文件超过了指定的大小,启用此选项
open_file_cache :配置Nginx文件缓存
- Syntax :open_file_cache off;
- Syntax :open_file_cache max=N [inacitve=TIME];
- Default :open_file_cache off;
- Context :http, server, location
- Example :open_file_cache max=1000 inactive=20s;
- Notes1 :nginx可以缓存以下三种信息:
(1) 文件的描述符、文件大小和最近一次的修改时间;
(2) 打开的目录结构;
(3) 没有找到的或者没有权限访问的文件的相关信息;
- Notes2 :两个参数的含义
max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;
inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项。
open_file_cache_valid :缓存有效性的检查时间间隔
- Syntax :open_file_cache_valid TIME;
- Default :60s
- Context :http, server, location
- Example :open_file_cache_valid 100s;
open_file_cache_min_uses :某一个缓存项的最少被命中次数
- Syntax :open_file_cache_min_uses #;
- Default :open_file_cache_min_uses 1;
- Context :http, server, location
- Example :open_file_cache_min_uses 3;
- Notes :少于此项指令设定的值,在到达缓存检查时间点后,该项缓存将会被删除
open_file_cache_errors :设定是否缓存错误的(否定的)查找结果
- Syntax :open_file_cache_errors on | off;
- Default :open_file_cache_errors off;
- Context :http, server, location
- Example :open_file_cache_errors on;
- Notes :启用此项指令,当某次查找结果为错误时,该错误结果也会被缓存。