nginx core configuration Introduction

nginx core configuration Introduction

First, the scope

The main configuration is divided into three parts nginx

  • Global configuration (contex: main)

    Portion of the outside http {}

  • server part

    In the http {}

  • location

    In server

Second, the global configuration

See the official documentation: http: //nginx.org/en/docs/ngx_core_module.html

2.1 user nginx nginx;

User and group work process to start Nginx

2.2 worker_processes [number | auto];

The number of start Nginx worker processes, should be set to the number of cpu

Optimum values ​​depend on many factors, including (but not limited to) the number of the number of CPU cores hard disk drive, for storing data, and loading mode.

2.3 worker_cpu_affinity 00000001 00000010 00000100 00001000;

Writing: 00000001 behalf of a CPU

00000010 represents the first two CPU and so on, there are a few core CPU writes a few

The Nginx worker process to bind to a specific CPU core, the default process Nginx is not binding, the binding does not mean the current nginx process exclusive to a core CPU, but you can guarantee that this process will not run on other core , which greatly reduces the nginx worker processes to jump back and forth on different cpu core, reducing the CPU and memory resource allocation and management of the recycling process, etc., and therefore can effectively enhance the performance of nginx server.

Why work processes to bind cpu? Pressure measurement process will be time to switch CPU

Example: No pressure measured before the worker processes (1) 25 876 ​​in (0) above the first CPU in the second CPU above 25877

root@z2:~# ps axo pid,cmd,psr | grep nginx
 25669 nginx: master process /apps   1
 25876 nginx: worker process         0
 25877 nginx: worker process         1

Pressure-test

root@z2:~# apt install apache2-utils 
root@z2:~# ab -n20000 -c1000  http://192.168.1.102/

After the pressure was found to have an 25877 cpu also run on the first (0)

root@z2:~# ps axo pid,cmd,psr | grep nginx
 25669 nginx: master process /apps   1
 25876 nginx: worker process         0
 25877 nginx: worker process         0

worker process after binding cpu no matter how pressure measured and found all the same

2.4 error_log

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

#error_log logs/error.log;
#error_log logs/error.log notice;

#error_log logs/error.log info;

2.5 pid

#pid logs/nginx.pid;

note: In the logs here is a relative directory, relative to a specified during installation prexfix_path, you can also write complete directory, such as

pid /apps/nginx/logs/nginx.pid;

2.6 worker_priority

worker_priority 0; # work process priority, -20 to 19

2.7 worker_rlimit_nofile

worker_rlimit_nofile 65536; # This figure includes all connections Nginx (eg connection to the proxy server, etc.), not
only is the connection with the client, the other consideration is the actual number of concurrent connections can not exceed the system-level maximum open file limit

Set the maximum number of pieces of nuclear energy for each worker open for breakthrough limit without restarting the master process.
This value is not set, the system employing values ulimit -a, it would generally increase point, question "too many open files" in case of error.

2.8 daemon off;

Front running Nginx services for testing, docker environment.

2.9 master_process off|on;

Election on, enabled by default

# Whether to open the master-woker operating mode of Nginx.

2.10 events

events {

worker_connections 65536; the maximum number of concurrent single nginx worker # acceptable process, the maximum number of concurrent connections to a web server as worker_connnetions * work_processes; as a reverse proxy worker_connections * worker_processes) / 2

Remember, this number includes all connections (including with the connection between the back-end server, not just with the connection between the client), Another point to note is that this value can not exceed the limit on the maximum number of files can be opened by a single worker process ( this value may be worker_rlimit_nofileinstruction sets).

use epoll; # use epoll event-driven, Nginx supports a large number of event-driven, such as select, poll, epoll, only settings in the events module.

accept_mutex on; # only one request at a time optimization and to avoid more sleep process wakes up the set, on the same time in order to prevent being awakened by default to off, the entire process has become a wake-up "thundering herd", so after nginx just finished the installation to be proper optimization.

multi_accept on; each worker process Nginx server can accept multiple simultaneous new network connection, but need to be configured in the configuration file, this command is off by default, which is the default for the first time a work process can only accept a new network connection, several open simultaneously receiving a plurality of arranged following syntax:
}

Three, http configuration

3.1 nginx supported file types
  • include mime.types;

Import supported file types

  • default_type application/octet-stream;

      设置默认的类型,会提示下载不匹配的类型文件
    
3.2 log configuration section
#定义的日志格式
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

#定义访问日志的位置、以及记录的格式,默认值是所有的server都在一起,即所有的网站的访问记录都在一起,实际应该分开
access_log  logs/access.log  main;

3.3 customized optimization parameter
  • sendfile on; # 开启sendfile

  • tcp_nopush on; # In the case of sendfile opened, the combined unified request is sent to the client.

  • tcp_nodelay off; # connection opened keepalived mode is active on TCP_NODELAY option, when is off, sending delay 0.2s, the default On, do not delay sending, sends the user an immediate corresponding message.

  • keepalive_timeout 65 long connection time in seconds

  • gzip on; # open file compression

3.4 server configuration

Set up a virtual machine host, you can include your own global fast, but also can include multiple location modules. For example, this virtual machine monitor port name and IP configuration of this virtual machine, you can use a multiple server ports, such as port 80 are provided web services

server {

    listen 80; #配置server监听的端口
	charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8
    server_name www.a.com; 本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配。
    access_log logs/www_a_com_access.log main; #定义访问日志的路径,格式
    error_log  /apps/nginx/logs/www_a_com_error.log error; #定义错误日志的路径,格式
  
    error_page    500 502 503 504  /50x.html; #定义指定的状态码 错误页面 
    location = /50x.html {
        root   html;   #错误页面的网站
    }

    location / { #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令,都是在location中提现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定的指令进行处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三方模块的配置也是在location模块中配置。
            root html; #相当于默认页面的目录名称,默认是相对路径,可以使用绝对路径配置。
        	index index.html index.htm; #默认的页面文件名称
	}
    
}
3.5 server_tokens off;

# Hide Nginx server version.

Published 62 original articles · won praise 7 · views 1262

Guess you like

Origin blog.csdn.net/qq_36801585/article/details/104880347