Home >Operation and Maintenance >Nginx >Introduction to parameters that must be configured in nginx

Introduction to parameters that must be configured in nginx

王林
王林forward
2020-11-12 16:00:082737browse

Introduction to parameters that must be configured in nginx

1: main parameters

1.1 worker_processes

# 指定nginx开启worker工作子进程数量# number默认1,建议配置数量与CPU核心数量相等worker_processes number复制代码

1.2 worker_cpu_affinity

# nginx默认未开启利用多核CPU,开启某个核心该位置标志为1即可# worker_processes 最多开启8个,所以多核最多8个也就够用。示例配置开启4核worker_cpu_affinity 00000001 00000010 00000100 00001000复制代码

1.3 worker_priority

# 进程优先级,数值越低占用资源越多# number默认10,值范围-20 ~ 20# 建议配置-10即可,最好别低于Linux系统进程-5优先级worker_priority number复制代码

1.4 worker_rlimit_nofile

# 一个子进程可以打开文件描述限制# nginx默认一个子进程打开文件描述限制数量 = (ulimt -u) / worker_processes# 因为进程处理连接任务很多时候不均衡,所以最好设置为与系统数量一致worker_rlimit_nofile (ulimit -u)复制代码

1.5 error_log

# file指定日志输出文件位置,默认logs/error.log# level指定日志输出最低级别,默认error级别。当然可以设置为debug、info等error_log file [level]复制代码

1.6 pid

# file默认值logs/pid.log,指定nginx中master进程的PID输出文件位置pid file复制代码

1.7 user

# user第二个参数user,配置nginx进程运行用户,默认nobody# group配置nginx进程运行用户组,默认nobodyuser user [group]复制代码

2: Events parameters

2.1 worker_connections

# nginx指定一个子进程可处理连接数量# number默认数量1024# nginx可处理连接总数 = worker_processes * worker_connecitonsworker_connections  number复制代码

2.2 accept_mutex

The accept_mutex parameter is the accept mutex lock switch for the worker process to process the connection. The default is on before 1.11.3, and the default is off in later versions

2.2.1 Jingqun Phenomenon

Nginx can configure multiple worker processes through the worker_processes parameter. Multiple worker processes will listen to the same port after forking. If an external connection comes in, all child processes will be awakened to seize the connection. Unless one child process successfully handles the accept event, the child processes will go back to sleep. Resulting in many unnecessary context switches. This is the thundering herd phenomenon

2.2.2 accept lock processing

nginx adds an accept mutex so that there is only one process registered in epoll, thereby avoiding the thundering herd phenomenon. That is to say, when a connection comes in, there can only be one child process to handle it

尝试获取accept锁if success
    在epoll中注册accept事件else
    在epoll中注销accept事件
处理所有连接事件
释放accept锁复制代码
2.2.3 Disadvantages of accept lock

The accept lock seems to perfectly solve the problems caused by the thundering group phenomenon, but it also brings The problem arises that some child processes are very busy and some child processes are very idle, especially in applications with relatively high throughput and concurrency. Generally speaking, it is the problem of uneven load of worker sub-processes

2.2.4 Configuration suggestions
  • For distributed application short connections, it is best to turn on this parameter to avoid too many The context switching overhead
  • For long-connection applications, it is best to turn off this parameter to avoid excessive connection load on a worker and causing high CPU utilization of a process

2.3 accept_mutex_delay

When accept_mutex is set to on, you need to debug the accept_mutex_delay parameter according to the application scenario. This parameter specifies the time after which all child processes will re-rob the accept lock. Appropriate parameter values ​​can help reduce worker load imbalance problems. Default value 500ms

Related recommendations:nginx tutorial

The above is the detailed content of Introduction to parameters that must be configured in nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete