nginx配置详解(一)

1.worker_processes:工作进程数,通过如下命令可以看出worker_processes默认工作进程数为1个worker进程

一般配置需要配置成CPU的核心数或者直接配置成auto

[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是不会进行进程绑定

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

少nginx的工作进程在不同CPU核心上来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以

有效的提升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上运行

[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 

3、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;
}

  

 

猜你喜欢

转载自www.cnblogs.com/tester-hqser/p/12105265.html