Nginx配置参数解释

 目录worker_processeswork_cpu_affinityworker_rlimit_nofileevents模块1、use method;2、worker_connections3、multi_accepthttp模块1、server_names_hash_bucket_size2、client_header_buffer_size3...
摘要由CSDN通过智能技术生成

 

目录

worker_processes

work_cpu_affinity

worker_rlimit_nofile

events模块

1、use method;

2、worker_connections

3、multi_accept

http模块

1、server_names_hash_bucket_size

2、client_header_buffer_size

3、large_client_header_buffers

4、client_max_body_size

5、client_header_timeout

6、client_body_timeout

7、send_timeout

8、sendfile

9、tcp_nopush

10、tcp_nodelay

11、keepalive_timeout

12、server_tokens

13、limit_conn_zone

14、limit_conn

15、limit_conn_log_level

16、gzip

17、gzip_min_length

18、gzip_comp_level

19、gzip_buffers

20、gzip_types

21、gzip_http_version  (1.0|1.1)

22、gzip_proxied

23、gzip_vary

24、gzip_disable

25、proxy_temp_path

26、proxy_cache_path

27、client_body_buffer_size

28、proxy_connect_timeout (default 60s)

29、proxy_read_timeout (default 60s)

30、proxy_send_timeout (default 60s)

31、proxy_buffer_size

32、proxy_buffers

33、proxy_busy_buffers_size

34、proxy_temp_file_write_size

35、proxy_next_upstream

EXAMPLE 基于上述 我配置的简单的nginx.conf 优化之后的 web代理配置:


worker_processes

工作进程数,操作系统启动多少个工作进程运行Nginx。注意是工作进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工作进程worker process。worker process。主进程负责监控端口,协调工作进程的工作状态,分配工作任务,工作进程负责进行任务处理。一般这个参数要和操作系统的CPU内核数成倍,(当值为 auto 时,nginx会自己决定工作进程数量)

下面是nginx官网原话:

Syntax: worker_processes number | auto;
Default: worker_processes 1;

Context: main Defines the number of worker processes. The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it). The auto parameter is supported starting from versions 1.3.8 and 1.2.5.

网上寻找的一些经验总结:

  • 一般设置为1就足够了,可以把连接数设置的很大,比如65535。
  • 如果有SSL,gzip等比较消耗CPU资源的工作,而且CPU是多核的话,可以把值设置为和CPU核数一样。
  • 如果有很多静态文件而且它们的总大小超过内存大小,那么可以增加工作进程来充分利用I/O带宽。
  • 如果要开多个工作进程,最好是CPU核数的1~2倍。正常情况下,不要太多,因为工作进程太多会影响nginx主进程调度。
  • worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

work_cpu_affinity

Nginx默认没有开启利用多核CPU,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核CPU。CPU是任务处理,计算最关键的资源,CPU核越多,性能就越好(worker_processes配合使用,当值为 auto 时,nginx会自己决定CPU核数使用)。

几个简单的配置用例:

worker_processes     2;
worker_cpu_affinity 01 10;

01表示启用第一个CPU内核,10表示启用第二个CPU内核。
worker_cpu_affinity 01 10;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。

 

worker_processes     4;
worker_cpu_affinity 01 10 01 10;

开启了四个进程,它们分别对应着开启2个CPU内核。

 

worker_processes     8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推。

worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

 

worker_rlimit_nofile

nginx官网原文:

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.

为nginx工作进程改变打开最多文件描述符数目的限制。用来在不重启主进程的情况下增加限制。

如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件。

 

events模块

nginx官网原文:

Provides the configuration file context in which the directives that affect connection processing are specified.

events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

1、use method;

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.

eg: use epoll;

使用epoll的I/O 模型(值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)。

如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。

补充说明:

与apache相类,nginx针对不同的操作系统,有不同的事件模型
    A)标准事件模型
    Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
    B)高效事件模型
    Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
    Epoll:使用于Linux内核2.6版本及以后的系统。
    /dev/poll:使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
    Eventport:使用于Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁

查看linux版本号可以使用 cat /proc/version命令

2、worker_connections

原文:

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.

设置工作进程的最大连接数量

按反向代理模式下最大连接数的理论计算公式:

   最大连接数 = worker_processes * worker_connections/4

3、multi_accept

原文:

If multi_accept is disabled, a worker process will accept one new connection at a time. Otherwise, a worker process will accept all new connections at a time.

The directive is ignored if kqueue connection processing method is used, because it reports the number of new connections waiting to be accepted.

如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接。
如果nginx使用kqueue连接方法,那么这条指令会被忽略,因为这个方法会报告在等待被接受的新连接的数量。

 

http模块

1、server_names_hash_bucket_size

Sets the bucket size for the server names hash tables. The default value depends on the size of the processor’s cache line. The details of setting up hash tables are provided in a separate document.

服务器名字的hash表大小,默认(32|64|128)

2、client_header_buffer_size

Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated.

用于指定来自客户端请求头headerbuffer大小,对于大多数请求,1KB(默认)的缓冲区大小已经足够,如果自定义了消息头或有更大的cookie,可以增加缓冲区大小。这里设置为32KB

3、large_client_header_buffers

Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released.

用来指定客户端请求中较大的消息头的缓存最大数量和大小,

eg: large_client_header_buffers 4 128k;   “4”为个数,“128”为大小,最大缓存为4个128KB

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值