能够说nginx
的使用基本体如今了配置文件的指令上,而每一个模块中的指令又对应了不少的不一样的功能,因此本文旨在了解了以前的nginx的初始nginx的基本思想和功能的前提下,进一步的了解一些比较经常使用的模块和其中的指令;php
主要参考nginx的官方文档,nginx的指令集合字母排序,nginx的变量字母排序html
主模块的指令主要包括如下这些,基本都是简单指令,不过每一个指令的执行环境context是不同的,有的能够在http、location、server等中均可以使用,主模块指令如下是主模块中的指令列表:linux
1.2 daemon 1.3 debug_points 1.4 error_log 1.5 include 1.6 master_process 1.7 pid 1.8 ssl_engine 1.9 timer_resolution 1.10 user 1.11 worker_cpu_affinity 1.12 worker_priority 1.13 worker_processes 1.14 worker_rlimit_core 1.15 worker_rlimit_nofile 1.16 worker_rlimit_sigpending 1.17 working_directory
daemon指令,守护进程QA,生产环境中不要使用"daemon"和"master_process"指令,这些选项仅用于开发调试[主要方便开发过程当中的进程清除]。nginx
语法: daemon on | off
缺省值: on
使用环境context: main主模块
debug_points指令,主要也是在开发过程来调试nginx正则表达式
语法: debug_points [stop | abort]
缺省值: none
error_log 指令用于记录nginx的某些级别的异常错误日志express
语法: error_log file [ debug | info | notice | warn | error | crit ]
缺省值: ${prefix}/logs/error.log
include 指令,主要是来引入其余配置文件,避免单一配置文件过大和复杂apache
语法: include file | *
缺省值: none
你能够在任意地方使用include指令实现配置文件的包含,相似于apache中的include方法,可减小主配置文件的篇幅。include 指令还支持像下面配置同样的全局包含的方法,例如包含一个目录下全部以".conf"结尾的文件:segmentfault
include vhosts/*.conf;
pid 指令,进程id存储文件缓存
语法: pid file
缺省值: compile-time option Example:
pid /var/log/nginx.pid;
user
这个指令名表明的是执行worker processes
的本机用户,默认是nobody
,那么若是须要读写一些roort
或者其余用户全部权的文件时,若是当前配置文件填写的user
这个指令名对应的用户又不具备r+w+x
的权限时,就会出现一些权限问题;服务器
语法: user user [group]
缺省值: nobody nobody
指定Nginx Worker进程运行用户,默认是nobody账号。
worker_processes
这个指令名是指配置worker_processes
的数量,nginx
启动时会有一个主进程和若干的工做进程,这个指令就是来规定工做进程的数量的,对应的是一个数值,
nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests.
使用: worker_processes number | auto;
默认:worker_processes 1;
使用环境context: main 主模块
若是nginx处理的是cpu密集型(比较耗费cpu的)的操做,建议将此值设置为cpu个数或cpu的核数。
一个cpu配置多于一个worker数,对nginx而言没有任何益处。
须要注意的是,经过event
模块中的worker_connections
和主模块的worker_proceses
能够计算出maxclients
,也就同时最大链接数:
max_clients = worker_processes * worker_connections
worker_rlimit_nofile指令,对于上述的的最大链接数有限制做用
Syntax: worker_rlimit_nofile number;
Default: —
Context: main
Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.
这其中使用比较多的就是include
指令。
事件模块主要是设置Nginx处理链接请求;事件模块指令指令列表以下:
2.1 accept_mutex 2.2 accept_mutex_delay 2.3 debug_connection 2.4 devpoll_changes 2.5 devpoll_events 2.6 kqueue_changes 2.7 kqueue_events 2.8 epoll_events 2.9 multi_accept 2.10 rtsig_signo 2.11 rtsig_overflow_events 2.12 rtsig_overflow_test 2.13 rtsig_overflow_threshold 2.14 use 2.15 worker_connections
accept_mutex指令,nginx 使用链接互斥锁进行顺序的accept()系统调用.
Syntax: accept_mutex on | off;
Default:
accept_mutex off;
Context: events
If accept_mutex is enabled, worker processes will accept new connections by turn. Otherwise, all worker processes will be notified about new connections, and if volume of new connections is low, some of the worker processes may just waste system resources.
这个指令之前默认是开的,Prior to version 1.11.3, the default value was on.
worker_connections
Syntax: worker_connections number;
Default:
worker_connections 512;
Context: events
Sets the maximum number of simultaneous connections that can be opened by a worker process. 设置一个工做进程的同一时刻的最大链接数
It should be kept in mind that this number includes all connections (e.g. connections with proxied servers, among others), not only connections with clients. Another consideration is that the actual number of simultaneous connections cannot exceed the current limit on the maximum number of open files, which can be changed by worker_rlimit_nofile.
其余的之类须要参考官方文档来看: 主模块和事件模块的简单指令详细介绍:http://nginx.org/en/docs/ngx_core_module.html
http相关模块能够看做是nginx的核心模块,是其功能的最核心部分,因此http相关的模块有将近几十个,其中使用的最多的是http核心模块;
http模块的主要组成部分是server和一些公用提取出来指令【能够在http、server、server甚至是if内使用的指令,在server块级指令集中设置的话须要书写屡次,因此能够做为公共提取到http块指令中】,而server的重要部分是location、listen、server_name等;
大部分配置也能够写在server、http块级中,可是为了不重复书写,能够考虑提取到外层;部分指令以下:
types 响应内容的文件类型,Maps file name extensions to MIME types of responses;MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。MIME 消息能包含文本、图像、音频、视频以及其余应用程序专用的数据。
Syntax: types { ... }
Default: types {
text/html html;
image/gif gif;
image/jpeg jpg;
}
Context: http, server, location
一个完整的映射表在conf/mime.types文件中,因此默认的写法是include mime.types;
;默认类型还可使用:To make a particular location emit the “application/octet-stream” MIME type for all requests, the following configuration can be used:
location /download/ { types { } default_type application/octet-stream; }
keepalive_timeout 客户端keeplive的连接无任何操做的保持最久时间
Syntax: keepalive_timeout timeout [header_timeout];
Default: keepalive_timeout 75s;
Context: http, server, location
The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.
error_page 为一些特定的错误返回定义好的一些URI路径, 详细介绍error_page
Syntax: error_page code ... [=[response]] uri;
Default: —
Context: http, server, location, if in location
如下example:
error_page 404 /404.html; error_page 500 502 503 504 /50x.html; error_page 404 =200 /empty.gif; error_page 404 = /404.php; location / { error_page 404 = @fallback; } location @fallback { proxy_pass http://backend; } error_page 403 http://example.com/forbidden.html; error_page 404 =301 http://example.com/notfound.html;
listen 详细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
一些例子:
listen 127.0.0.1:8000; listen 127.0.0.1; listen 8000; listen *:8000; listen localhost:8000; //IPv6 addresses (0.7.36) are specified in square brackets: listen [::]:8000; listen [::1];
若是没有这个listen
指令,那么须要根据启动用户是不是超级管理员用户,是的话将会启动监听80端口,不然使用8000端口;目前已经支持hhtp2 https
的方式。
server_name server_name详情为每一个服务设置上名称,在http指令中能够设置多个server,多个server能够监听同一个端口,那么端口接收到的请求就须要根据名字匹配来进行相应的服务中进行处理,若是都没有匹配上的话,就是第一个derver指令来处理。
Syntax: server_name name ...;
Default: server_name "";
Context: server
可使用全匹配、通配符、正则匹配,例子以下:
server { server_name example.com www.example.com; } //The first name becomes the primary server name. //Server names can include an asterisk (“*”) replacing the first or last part of a name: server { server_name example.com *.example.com www.example.*; } //Such names are called wildcard names. //The first two of the names mentioned above can be combined in one: server { server_name .example.com; } It is also possible to use regular expressions in server names, preceding the name with a tilde (“~”): server { server_name www.example.com ~^www\d+\.example\.com$; } Regular expressions can contain captures (0.7.40) that can later be used in other directives: server { server_name ~^(www\.)?(.+)$; location / { root /sites/$2; } } server { server_name _; location / { root /sites/default; } } Named captures in regular expressions create variables (0.8.25) that can later be used in other directives: server { server_name ~^(www\.)?(?<domain>.+)$; location / { root /sites/$domain; } } server { server_name _; location / { root /sites/default; } }
server_name_in_redirect 在由nginx发布的绝对重定向中,启用或禁用使用由server_name指令指定的主服务器名称
Syntax: server_name_in_redirect on | off;
Default: server_name_in_redirect off;
Context: http, server, location
在开启的时候,若是须要重定向,就可使用已经开启过的server_name
来书写,快速的定位;
server_name cwj.cc; server_name_in_redirect on; #charset koi8-r; #access_log logs/host.access.log main; location /test { proxy_pass http://cwj.cc:8080; //此服务名开启了重定向,能够直接使用 }
port_in_redirect 和上面的指令差很少,只不过换成了端口号,默认开启
Syntax: port_in_redirect on | off;
Default: port_in_redirect on;
Context: http, server, location
location 将解析后的URI请求进行匹配,匹配到后根据配置完成相应的处理和转发,该指令还能够嵌套使用;
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
可使用= 、^~、 ~|~*、@
四种方法来配置;匹配顺序以下序列号:
= 使用等号匹配符代表两点:
例子以下:
location = /test{ return 'hello_world no1'; } // 须要彻底匹配,只有 127.0.0.1:port/test 能够正确匹配 ;连127.0.0.1:port/test/也没法匹配上
空字符彻底匹配,空字符匹配既能够彻底匹配也能够匹配URI的前半部分【prefix string】;区别是:只有彻底匹配的时候,匹配优先级才会高于正则匹配,而prefix string的
例子以下:
location /test/test.jpg{ return '/test/test.jpg'; } // 须要彻底匹配优先级才高,只有 127.0.0.1:port//test/test.jpg 能够算彻底匹配 ; //连127.0.0.1:port/test.jgp.test 也能够匹配上,只是不算彻底匹配
^~ 正则前缀匹配,表示匹配以正则匹配的内容开头的URI,
例子以下:
location ^~ /test{ return 'hello_world no3'; } // 匹配 127.0.0.1:port/test + 其余后缀路径等;
~|~*
例子以下:
location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ { alias /data/w3/images/$1; } // 须要与正则表达式匹配,127.0.0.1:port/users/test.jpg 能够正确匹配 ;还能够在后续来使用这个匹配上的可捕获的子表达式
空字符非精确
例子以下:
location = /test{ return 'hello_world no5'; } // 能够彻底和部分匹配,只有 127.0.0.1:port/test 才算是全匹配 ;127.0.0.1:port/test/也能够匹配上,就是非彻底匹配,优先级最低
还有不少其余的,此处不一一例举。
alias 替换URI访问的具体路径
Syntax: alias path;
Default: —
Context: location
仅在location中使用,用来替换访问路径。下面的例子就是一个路径的映射关系
location /i/ { alias /data/w3/images/; } //on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.
root 设置请求的根目录
Syntax: root path;
Default: root html;
Context: http, server, location, if in location
使用后全部的请求的资源都将映射到设置的根目录下面:
location /i/ { root /data/w3; } //The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request.
etag 为请求设置etag响应头,和缓存先关的设置
Syntax: etag on | off;
Default: etag on;
Context: http, server, location
This directive appeared in version 1.3.3.
Enables or disables automatic generation of the “ETag” response header field for static resources.
if_modified_since 替换URI访问的具体路径
Syntax: if_modified_since off | exact | before;
Default: if_modified_since exact;
Context: http, server, location
This directive appeared in version 0.7.24.
还有不少其余的,此处不一一例举。
为了不篇幅过长,部份内容移到下一篇文章中