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;
}
相關文章
相關標籤/搜索