控制 Nginx 的基本功能的指令如下:
- daemon
- debug_points
- error_log
- include
- lock_file
- 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
- worker_shutdown_timeout
daemon
是否以守护进程的方式运行nginx
语法: daemon on | off
缺省值: on
Example:
daemon off
在生产模式下使用runit / daemontools是安全的,但是你不能做一个优雅的升级。
生产环境中不要使用"daemon"和"master_process"指令,这些选项仅用于开发调试。
docker容器启动时,默认会把容器内部第一个进程,也就是pid=1的程序,作为docker容器是否正在运行的依据,如果 docker容器pid=1的进程挂了,那么docker容器便会直接退出。Docker未执行自定义的CMD之前,nginx的pid是1,执行到CMD之后,nginx就在后台运行,bash或sh脚本的pid变成了1。所以一旦执行完自定义CMD,nginx容器也就退出了。
master_process
是否以master/worker方式进行工作,在实际的环境中 nginx是以一个master进程管理多个worker进程的方式运行的,关闭后
nginx就不会fork出worker子进程来处理请求,而是用master进程自身来处理请求
语法: master_process on | off
缺省值: on
Example:
master_process on;
生产环境中不要使用"daemon"和"master_process"指令,这些选项仅用于开发调试。
debug_points
这个配置用来帮助用户调试Nginx,他接收2个参数:stop和abort。Nginx在一些关键的错误逻辑中设置了调试点。如果设置为stop,那么Nginx的代码执行到这些调试点时,会发出SIGSTOP信号。如果abort,则会产生一个coredump文件。
语法: debug_points [stop | abort]
缺省值: none
Example:
debug_points stop;
error_log
error_log是Nginx定位问题的最佳工具,我们可以根据自己的需求设置日志的路径和级别。
${prefix}是一个具体文件;也可以是/dev/null,这样就不会生成日志,这是关闭error_log的唯一手段;也可以是stderr,这样日志会输出到标准错误输出
lerror_log日志级别;debug,info,notice,warn,error,crit,alert,emerg,从左到右,依次增大。
语法: error_log file [ debug | info | notice | warn | error | crit ]
缺省值: ${prefix}/logs/
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方法,可减少主配置文件。
include指令还支持像下面配置一样的全局包含的方法,例如包含一个目录下所有以".conf"结尾的文件:
Example:
include vhosts/*.conf;
注意路径受到configure编译参数–prefix=<路径>指令的影响,如果没有指定,Nginx默认是被编译在/usr/local/nginx。
lock_file
语法: lock_file file
缺省值: compile-time option
Example:
lock_file /var/log/lock_file;
Nginx使用accept互斥来序列化accept()系统调用。如果nginx是由gcc、Intel c++或SunPro
c++编译器在i386、amd64、sparc64和ppc64上构建的,那么nginx使用原子指令来实现互斥。在其他情况下,将使用锁文件。
pid
用来指定进程id的存储文件位置。
语法: pid file
缺省值: compile-time option Example:
Example:
pid /var/log/;
进程id存储文件。可以使用 kill -HUP
cat /var/log/
对Nginx进行配置文件重新加载。
ssl_engine
语法: ssl_engine engine
缺省值: system dependent
该指令用于指定openssl使用的引擎。你可以通过下面的命令行获知系统目前支持的openssl引擎
Example:
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;
该指令允许减少gettimeofday()系统调用的次数。默认情况下,每次从kevent(), epoll, /dev/poll,
select(), poll()返回后都会调用gettimeofday()。但是,如果在记录 u p s t r e a m r e s p o n s e t i m e 或 upstream_response_time或 upstreamresponsetime或msec变量时需要在日志中记录准确的时间,那么应该使用timer_resolution
user
语法: user user [group]
缺省值: nobody nobody
指定Nginx Worker进程运行用户,默认是nobody帐号。
Example:
user www users;
worker_cpu_affinity
语法: worker_cpu_affinity cpumask [cpumask...]
缺省值: none
仅适用于linux,使用该选项可以绑定worker进程和CPU.
Example:
worker_proceses 4;
worker_cpu_affinity 0001 0010 0100 1000;
分别给每个worker进程绑定一个CPU.
worker_proceses 2;
worker_cpu_affinity 0101 1010;
将CPU0/CPU2绑定给第一个worker进程,将CPU1/CPU3绑定给第二个worker进程。
worker_priority
语法: worker_priority [-] number
缺省值: on
使用该选项可以给所有的worker进程分配优先值。
worker_processes
语法: worker_processes number
缺省值: 1
Example:
worker_processes 5;
nginx可以使用多个worker进程,原因如下:
- to use SMP
- to decrease latency when workers blockend on disk I/O
- to limit number of connections per process when select()/poll() is used
一般一个进程足够了,你可以把连接数设得很大。
如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。或者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加, 以充分利用IO带宽(主要似乎是IO操作有block)。
worker_rlimit_core
语法: worker_rlimit_core size
缺省值: -
修改工作进程的核心文件(RLIMIT_CORE)的最大大小限制。在不重启主进程的情况下增加限制。
worker_rlimit_nofile
语法: worker_rlimit_nofile number;
缺省值: —
修改工作进程最大打开文件数(RLIMIT_NOFILE)的限制。在不重启主进程的情况下增加限制。
worker_rlimit_sigpending
语法: worker_rlimit_sigpending limit
缺省值: -
(从Linux 2.6.8开始)指定在呼叫进程的真实用户ID可能排队的信号数量的限制。
working_directory
语法: working_directory path
缺省值: --prefix
--prefix==PATH
这是工人的工作目录。它只用于核心文件。Nginx只使用绝对路径,配置文件中的所有相对路径都是相对的
worker_shutdown_timeout
语法: worker_shutdown_timeout time;
缺省值: —
该指令出现在版本1.11.11中。
配置工作进程优雅关闭的超时时间。当时间到期时,nginx将尝试关闭所有当前打开的连接以方便关闭。