nginx配置详解(一)

1.worker_processes:工做进程数,经过以下命令能够看出worker_processes默认工做进程数为1个worker进程html

通常配置须要配置成CPU的核心数或者直接配置成autonginx

[root@localhost ~]# cat /apps/nginx/conf/nginx.conf|grep work
worker_processes  1;
#修改worker_processes参数以前nginx的worker进程数
[root@localhost ~]# ps -ef|grep nginx
root       1162      1  0 00:00 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      1225   1162  0 00:19 ?        00:00:00 nginx: worker process
root       1302   1250  0 01:24 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# 
#将worker进程修改成2以后,而后进行热启动查看nginx的worker进程数
[root@localhost ~]# vi /apps/nginx/conf/nginx.conf

user  nginx nginx;
worker_processes  2;

[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps -ef|grep nginx
root       1162      1  0 00:00 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      1306   1162  0 01:26 ?        00:00:00 nginx: worker process
nginx      1307   1162  0 01:26 ?        00:00:00 nginx: worker process
root       1309   1250  0 01:26 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# 

#配置成auto
[root@localhost ~]# ps -ef|grep nginx
root       1162      1  0 00:00 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      1306   1162  0 01:26 ?        00:00:00 nginx: worker process
nginx      1307   1162  0 01:26 ?        00:00:00 nginx: worker process
root       1309   1250  0 01:26 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# vi /apps/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  auto;
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps -ef|grep nginx
root       1095      1  0 23:38 ?        00:00:00 nginx: master process nginx
nginx      1103   1095  0 23:40 ?        00:00:00 nginx: worker process
nginx      1104   1095  0 23:40 ?        00:00:00 nginx: worker process
root       1107   1073  0 23:41 pts/0    00:00:00 grep --color=auto nginx

2.nginx的worker_cpu_affinity将nginx的work进程绑定在指定的CPU核心上运行,默认nginx是不会进行进程绑定web

的,绑定并不意味着当前nginx进程独占一个CPU核心,可是能够保证次进程不会运行在其余核心上,这就极大减apache

少nginx的工做进程在不一样CPU核心上来回跳转,减小了CPU对进程的资源分配与回收以及内存管理等,所以能够bash

有效的提高nginx服务器性能。服务器

[root@localhost ~]# vi /apps/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  auto;
worker_cpu_affinity 00000001 00000010;
[root@localhost ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps axo pid,cmd,psr |grep nginx
  1095 nginx: master process nginx   1
  1113 nginx: worker process         0
  1114 nginx: worker process         1
  1116 grep --color=auto nginx       0
  #从上图中能够看出来当前worker进程工做在不一样CPU上

验证不绑定CPU时,是否会出现CPU漂移,首先须要下载ab命令,使用ab命令来进行压测模拟,而后查看进并发

程在哪一个CPU上运行app

[root@localhost ~]# yum -y install httpd-tools		#下载ab命令
[root@localhost ~]# ps axo pid,cmd,psr |grep nginx
  1095 nginx: master process nginx   1
  1124 nginx: worker process         0
  1125 nginx: worker process         1
  1165 grep --color=auto nginx       1
[root@localhost ~]# ab -n1000 -c1000 http://192.168.1.170/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.170 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.14.2
Server Hostname:        192.168.1.170
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      1000
Time taken for tests:   0.065 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      845000 bytes
HTML transferred:       612000 bytes
Requests per second:    15424.24 [#/sec] (mean)
Time per request:       64.833 [ms] (mean)
Time per request:       0.065 [ms] (mean, across all concurrent requests)
Transfer rate:          12728.01 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   17   5.8     16      27
Processing:     7   15   6.9     11      30
Waiting:        0   14   7.5     11      30
Total:         17   32   5.7     34      38

Percentage of the requests served within a certain time (ms)
  50%     34
  66%     35
  75%     36
  80%     36
  90%     36
  95%     37
  98%     38
  99%     38
 100%     38 (longest request)
[root@localhost ~]# ps axo pid,cmd,psr |grep nginx
  1095 nginx: master process nginx   1
  1124 nginx: worker process         0
  1125 nginx: worker process         0
  1168 grep --color=auto nginx       1 

三、worker_connections:设置每一个nginx的worker进程能够接受的最大并发数,做为web服务器的时候nginx的负载均衡

最大并发数为worker_connections * worker_processes;做为反向代理服务器或者负载均衡服务器的时候,性能

nginx的最大并发数为(worker_connections * worker_processes)/2;改参数的默认值为1024;也能够设置成10

万个链接。

#查看默认配置
[root@localhost ~]# cat /apps/nginx/conf/nginx.conf|grep worker
worker_processes  auto;
worker_cpu_affinity 00000001 00000010;
    worker_connections  1024;#默认配置大小
  
#设置每一个worker进程能够并发100000个链接
[root@localhost ~]# cat /apps/nginx/conf/nginx.conf|grep -1 worker_connections
events {
    worker_connections  100000;
}
相关文章
相关标签/搜索