20190428--nginx--知其然必知其所以然2(核心模块)

目录

1.Nginx主模块

指令

daemon

debug_points

error_log

include

master_process

pid

ssl_engine

timer_resolution

user

worker_cpu_affinity

worker_priority

worker_processes

worker_rlimit_core

worker_rlimit_nofile

worker_rlimit_sigpending

working_directory

指令

accept_mutex

accept_mutex_delay

debug_connection

devpoll_changes

devpoll_events

kqueue_events

epoll_events

multi_accept

事件模块

rtsig_signo

rtsig_overflow_events

rtsig_overflow_test

rtsig_overflow_threshold

use

worker_connections


1.Nginx主模块

这里是控制 Nginx 的基本功能的指令.

指令

  • [#daemon daemon]
  • [#debug_points debug_points]
  • [#error_log error_log]
  • [#include include]
  • [#lock_file lock_file]
  • [#master_process master_process]
  • [#pid pid]
  • [#ssl_engine ssl_engine]
  • [#timer_resolution timer_resolution]
  • [#user user group]
  • [#worker_cpu_affinity worker_cpu_affinity]
  • [#worker_priority worker_priority]
  • [#worker_processes worker_processes]
  • [#worker_rlimit_core worker_rlimit_core]
  • [#worker_rlimit_nofile worker_rlimit_nofile]
  • [#worker_rlimit_sigpending worker_rlimit_sigpending]
  • [#working_directory working_directory]

daemon

语法: daemon on | off

缺省值: on

daemon  off;

Do not use the "daemon" and "master_process" directives in a production mode, these options are mainly used for development only. You can usedaemon offsafely in production mode with runit / daemontools however you can't do a graceful upgrade.master_process offshould never be used in production.

生产环境中不要使用"daemon"和"master_process"指令,这些选项仅用于开发调试。

debug_points

语法: debug_points [stop | abort]

缺省值: none

debug_points stop;

There are some assertion points inside nginx that allow to stop nginx to attach the debugger, or to abort and to create the core file.

应该适用于调试,在调试器内设置断点之类的。

error_log

语法: error_log file [ debug | info | notice | warn | error | crit ]

缺省值: ${prefix}/logs/error.log

Nginx 添加--with-debug 编译参数, 你还能够使用以下配置:

error_log LOGFILE [ debug_core | debug_alloc | debug_mutex | debug_event
]: | debug_http | debug_imap ;

include

语法: include file | *

缺省值: none

你可以在任意地方使用include指令实现配置文件的包含,类似于apache中的include方法,可减少主配置文件d。

include指令还支持像下面配置一样的全局包含的方法,例如包含一个目录下所有以".conf"结尾的文件:

include vhosts/*.conf;

注意路径受到configure编译参数--prefix=<路径>指令的影响,如果没有指定,Nginx默认是被编译在/usr/local/nginx。

语法: lock_file file

缺省值: compile-time option

lock_file  /var/log/lock_file;

nginx uses accept mutex to serialize accept() syscalls. If nginx is built by gcc, Intel C++, or SunPro C++ compilers on i386, amd64, sparc64, and ppc64, then nginx uses the atomic instructions to implement the mutex. In other cases the lock file would be used.

master_process

语法: master_process on | off

缺省值: on

master_process  off;

Do not use the "daemon" and "master_process" directives in a production mode, these options are mainly used for development only.

生产环境中不要使用"daemon"和"master_process"指令,这些选项仅用于开发调试。

pid

语法: pid file

缺省值: compile-time option Example:

pid /var/log/nginx.pid;

进程id存储文件。可以使用 kill -HUPcat /var/log/nginx.pid\对Nginx进行配置文件重新加载。

ssl_engine

语法: ssl_engine engine

缺省值: system dependent

Here you can set your preferred openssl engine if any available. You can figure out which one do you have with the commandline tool:

该指令用于指定openssl使用的引擎。你可以通过下面的命令行获知系统目前支持的openssl引擎

openssl engine -t

例如:

$ openssl engine -t
(cryptodev) BSD cryptodev engine
: [ available ] 
(dynamic) Dynamic engine loading support
: [ unavailable ] 

timer_resolution

语法: timer_resolution t

缺省值: none

Example:

timer_resolution  100ms;

The directive allows to decrease number gettimeofday() syscalls. By default gettimeofday() is called after each return from kevent(), epoll, /dev/poll, select(), poll().

But if you need an exact time in logs when logging $upstream_response_time, or $msec variables, then you should usetimer_resolution.

user

语法: user user [group]

缺省值: nobody nobody

指定Nginx Worker进程运行用户,默认是nobody帐号。

例如:

user www users;

worker_cpu_affinity

语法: worker_cpu_affinity cpumask [cpumask...]

缺省值: none

Linux only.

With this option you can bind the worker process to a CPU, it calls sched_setaffinity().

仅适用于linux,使用该选项可以绑定worker进程和CPU.

For example,

worker_proceses     4;
worker_cpu_affinity 0001 0010 0100 1000;

Bind each worker process to one CPU only.

分别给每个worker进程绑定一个CPU.

worker_proceses     2;
worker_cpu_affinity 0101 1010;

Bind the first worker to CPU0/CPU2, bind the second worker to CPU1/CPU3. This is suitable for HTT.

将CPU0/CPU2绑定给第一个worker进程,将CPU1/CPU3绑定给第二个worker进程。

worker_priority

语法: worker_priority [-] number

缺省值: on

With this option you can give to all worker processes the priority (nice) you need/wish, it calls setpriority().

使用该选项可以给所有的worker进程分配优先值。

worker_processes

语法: worker_processes number

缺省值: 1

e.g.:

worker_processes 5;

nginx has the ability to use more than one worker process for several reasons:

nginx可以使用多个worker进程,原因如下:

  1. to use SMP
  2. to decrease latency when workers blockend on disk I/O
  3. to limit number of connections per process when select()/poll() is used

Theworker_processesandworker_connectionsfrom the event sections allows you to calculatemaxclientsvalue: k

max_clients = worker_processes * worker_connections

worker_rlimit_core

语法: worker_rlimit_core size

缺省值: '

Maximum size of core file per worker;

worker_rlimit_nofile

语法: worker_rlimit_nofile limit 缺省值: '

Specifies the value for maximum file descriptors that can be opened by this process.

指定

worker_rlimit_sigpending

语法: worker_rlimit_sigpending limit 缺省值: '

(Since Linux 2.6.8) Specifies the limit on the number of signals that may be queued for the real user ID of the calling process.

working_directory

语法: working_directory path 缺省值: --prefix

This is the working directory for the workers. It's used for core files only. nginx uses absolute paths only, all relative paths in configuration files are relative to--prefix==PATH

 

指令

accept_mutex

Syntax: accept_mutex [ on | off ]

Default: on

nginx 使用连接互斥锁进行顺序的accept()系统调用.

accept_mutex_delay

Syntax: accept_mutex_delay Nms;

Default: 500ms

如果一个进程没有互斥锁,它将延迟至少多长时间。默认情况下,延迟是500ms 。

 

debug_connection

Syntax: debug_connection [ip | CIDR]

Default: none

Since 0.3.54 this option support CIDR address format

This option gives you the ability to write debug log only for the clients of this IP/NET.

Several different directives are possible.

Example:

error_log /var/log/nginx/errors;
events {
  debug_connection   192.168.1.1;

}

devpoll_changes

devpoll_events

kqueue_events

epoll_events

Syntax: devpoll_changes

Default:

These directives specify how many events may be passed to/from kernel, using appropriate method.

The defaultdevpollvalues are 32, the rest are 512.

multi_accept

Syntax: multi_accept [ on | off ]

Default: off

multi_accepttries to accept() as many connections as possible after nginx gets notification about a new connection.

 

事件模块

rtsig_signo

Syntax: rtsig_signo

Default:

nginx uses two signals when thertsigmethod is used. The directive specified the first signal number. The second is plus 1.

By defaultrtsig_signois SIGRTMIN+10 (40).

rtsig_overflow_events

rtsig_overflow_test

rtsig_overflow_threshold

Syntax: rtsig_overflow_*

Default:

These directives specifies how to handle rtsig queue overflows. When overflow occurred nginx flushes rtsig queue, then it handles events switching between poll() and rtsig. poll() handles consecutively all unhandled events, while rtsig periodicaly drains queue to prevent a new overflow. When overflow is handled completely, nginx switches to rtsig method again.

The rtsig_overflow_events specifies the number of events to be passed via poll(). The default is 16.

The rtsig_overflow_test specifies after which number of events handled by poll() nginx will drains rtsig queue. The default is 32.

The rtsig_overflow_threshold works in Linux 2.4.x only. Before to drain rtsig queue nginx looks in a kernel how the queue is filled up

The default is 1/10. "rtsig_overflow_threshold 3" means 1/3.

use

Syntax: use [ kqueue | rtsig | epoll | /dev/poll | select | poll | eventport ]

Default:
如果在./configure的时候指定了不止一种事件模型,那么可以设置其中一个,以便告诉nginx使用哪种事件模型。默认情况下nginx会在./configure时找出最适合系统的事件模型。

你可以在 这里 查看可用的事件模型以及如何在./configure时激活

worker_connections

Syntax: worker_connections number

Default:

通过worker_connections和worker_proceses可以计算出maxclients:

max_clients = worker_processes * worker_connections

作为反向代理,max_clients为:

max_clients = worker_processes * worker_connections/4

Since a browser opens 2 connections by default to a server and nginx uses the fds (file descriptors) from the same pool to connect to the upstream backend

 

参考链接:官网 http://www.nginx.cn/doc/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值