Heim  >  Artikel  >  Backend-Entwicklung  >  Nginx核心配置深入理解

Nginx核心配置深入理解

WBOY
WBOYOriginal
2016-07-28 08:27:13950Durchsuche

原文链接:http://blog.csdn.net/xyang81/article/details/51814787

Nginx的配置是以模块为单位来组织的,每一个模块包含一个或多个指令,指令是配置文件中的最小配置单元,一切配置项皆为指令。如http核心模块中的include、default_type、sendfile指令,都属于http模块。nginx所有模块中的指令见官方文档说明:http://nginx.org/en/docs/dirindex.html

<code>注意:以下配置中的“上下文”表示指令可以配置在哪些模块中。
main:顶层配置,约束服务器的行为
</code>

1、服务器级别核心配置

指令 上下文 语法 默认值 功能描述
user main user nobody nobyd; nobody 以哪个用户权限运行工作线程
daemon main daemon yes; yes nginx是否以守护进程运行
worker_processes main worker_processes number; 1 配置工作进程数。传统的web服务器(如apache)都是同步阻塞模型,一请求一进(线)程模式,当进(线)程数增达到一定程度后,更多CPU时间浪费在线程和进程切换当中,性能急剧下降,所以负载率不高。Nginx是基于事件的非阻塞多路复用(epoll或kquene)模型,一个进程在短时间内就可以响应大量的请求。建议将该值设置
worker_connections events worker_connections number; 1024 并发响应能力的关键配置值,表示每个进程允许的最大同时连接数。maxConnection = work_connections * worker_processes;一般一个浏览器会同时开两条连接,如果是反向代理,nginx到后服务器的连接数量也要占用2条连接数,所以,做静态服务器,一般maxConnection = work_connections * worker_processes / 2; 做反代理服务器时maxConnection = work_connections * worker_processes / 4;
use events use epoll; 根据不同的平台,选择最高效的连接处理方法 指定处理连接请求的方法。linux内核2.6以上默认使用epoll方法,其它平台请参考:http://nginx.org/en/docs/events.html 备注:要达到超高负载下最好的网络响应能力,还有必要优化与网络相关的linux内核参数
worker_cpu_affinity main worker_cpu_affinity cpumask …; 将工作进程绑定到特定的CPU上,减少CPU在进程之间切换的开销。用二进制bit位表示进程绑定在哪个CPU内核。如8内核4进程时的设置方法:worker_cpu_affinity 00000001 00000010 00000100 10000000
worker_rlimit_nofile main worker_rlimit_core size; 受linux内核文件描述符数量限制 设置nginx最大能打开的文件描述符数量。因为Linux对每个进程所能打开的文件描述数量是有限制的,默认一般是1024个,可通过ulimit -n FILECNT或/etc/securit/limits.conf配置修改linux默认能打开的文件句柄数限制。建议值为:系统最大数量/进程数。但进程间工作量并不是平均分配的,所以可以设置在大一些。推荐值为:655350
error_log main, http, mail, stream, server, location error_log 日志文件路径 日志级别; error_log logs/error.log error; 配置错误日志文件的路径和日志级别。日志级别有debug, info, notice, warn, error, crit, alert和emerg几种。nginx的日志使用syslog输出,所以输出的日志格式是有规律的,系统运维人员可以根据日志规则进行查错或统计分析。更多说明请参考官方文档:http://nginx.org/en/docs/ngx_core_module.html#error_log
pid main pid 守护进程socket文件路径; pid logs/nginx.pid 配置nginx守护进程ID存储文件路径(不是工作进程)

以上是nginx的顶层配置,管理服务器级别的行为。更多配置请参考官方文档:http://nginx.org/en/docs/ngx_core_module.html#working_directory

2、HTTP模块核心配置

nginx做为一个HTTP反向代理服务器,平时接触得最多的应该是针对http请求的相关配置了,和http模块有关的所有配置都放在http { ... }配置中。

指令 上下文 语法 功能描述
types http, server, location types { mime类型 文件后缀;}; 配置能处理的文件类型。如:text/html html htm shtml;
include any include 文件路径; 将外部文件的内容做为配置拷贝到nginx.conf文件中。如:include mime.type; 将当前目录下的mime.type配置文件拷贝到nginx配置文件中。文件路径可以是相对路径或绝对路径。文件名可以用*来表示通配符。
default_type http, server, location default_type mime类型; 文件名到后缀的映射关系。配置默认的mime类型,当在types指令中找不到请求的文件类型时,就使用default_type指定的类型。默认为text/plain类型。
access_log http, server, location, if in location, limit_except access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
关闭或开启访问日志。默认配置为:access_log logs/access.log combined; 表示根据combined定义的日志格式,写入logs/access.log文件中,combined是http模块默认格式。如果定义了buffer和gzip其中一个参数,日志默认会先写入缓存中,当缓存满了之后,通过gzip压缩缓存中的日志并写入文件,启用了gzip压缩必须保证nginx安装的时候添加了gzip模块。缓存大小默认为64K。可以配置gzip的1~9的压缩级别,级别越高压缩效率越大,日志文件占用的空间越小,但要求系统性能也越高。默认值是1。http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
log_format http log_format 格式名称 日志格式; 定义http访问日志的格式,在日志格式中可以访问http模块的内嵌变量,如果变存在的话,会做为日志输出。如:remoteaddrrequest等,更多变量请参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
sendfile http, server, location, if in location sendfile on | off; 启用内核复制模式。作为静态服务器可以提高最大的IO访问速度。传统的文件读写采用read和write方式,流程为:硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >>协议栈,采用sendfile文件读写的流程为:硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈,很明显sendfile这个系统调用减少了内核到用户模式之间的切换和数据拷贝次数,直接从内核缓存的数据拷贝到协议栈,提高了很大的效率。这篇文章介绍比较详细:http://xiaorui.cc/?p=1673
tcp_nodelay http, server, location off|on;
tcp_nopush http, server, location off|on; tcp_nodelay和tcp_nopush这两个参数是配合使用的,启动这两项配置,会在数据包达到一定大小后再发送数据。这样会减少网络通信次数,降低阻塞概率,但也会影响响应及时性。比较适合于文件下载这类的大数据通信场景。
keepalive_timeout http, server, location keepalive_time 65; 客户端到服务器建立连接的超时时长,超过指定的时间服务器就会断开连接。默认为75秒。降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
gzip http, server, location, if in location gzip on | off; 开启内容压缩,可以有效降低客户端的访问流量和网络带宽
gzip_min_length http, server, location gzip_min_length length; 单位为k,默认为20k。内容超过最少长度后才开启压缩,因为太短的内容压缩效果不佳,且压缩过程还会浪费系统资源。这个压缩长度会作为http响应头Content-Length字段返回给客户端。 建议值:1000
gzip_comp_level http, server, location gzip_comp_level 1~9; 压缩级别,默认值为1。范围为1~9级,压缩级别越高压缩率越高,但对系统性能要求越高。建议值:4
gzip_types http, server, location gzip_types mime-type …; 压缩内容类型,默认为text/html;。只压缩html文本,一般我们都会压缩js、css、json之类的,可以把这些常见的文本数据都配上。如:text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache http, server, location open_file_cache off; open_file_cache max=N [inactive=time]; 默认值为off; 设置最大缓存数量,及缓存文件未使用的存活期。建议值:max=655350(和worker_rlimit_nofile参数一致) inactive=20s;
open_file_
cache_min_uses
http, server, location open_file_cache_min_uses number; 默认为1,有效期内文件最少使有的次数。建议值:2
open_file
_cache_valid
http, server, location open_file_cache_valid time; 默认为60s,验证缓存有效期时间间隔。 表示每隔60s检查一下缓存的文件当中,有哪些文件在20s以内没有使用超过2次的,就从缓存中删除。采用lru算法。
server server { … } http HTTP服务器的核心配置,用于配置HTTP服务器的虚拟主机,可以配置多个
listen listen ip[:端口] server 配置虚拟主机监听的IP地址和端口,默认监听本机IP地址和80或8000端口。如果只设置了IP没设端口,默认使用80端口。如果只设置了端口,没设置IP默认使用本机IP。详细配置请参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#listen
server_name server_name domain_name …; server 配置虚拟主机的域名,可以指定多个,用空格分隔。默认为空
charset http, server, location, if in location charset charset | off; 设置请求编码,和url参数乱码问题有关。
location server, location location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
http请求中的一个重要配置项,用于配置客户端请求服务器url地址的匹配规则。可以配置多个匹配规则
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 Nginx核心配置深入理解,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn