1.查看nginx进程
ps -axu | grep nginx
root 4968 0.0 0.1 46168 1992 ? Ss Jun16 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 24548 0.0 0.0 112816 976 pts/0 S+ 17:27 0:00 grep --color=auto nginx
nginx 30744 0.0 0.4 217724 8352 ? S 08:43 0:00 php-fpm: pool www
nginx 30745 0.0 0.4 217724 8356 ? S 08:43 0:00 php-fpm: pool www
nginx 31607 0.0 0.1 46532 2296 ? S 09:11 0:00 nginx: worker process
ps -ef | grep nginx
root 4968 1 0 Jun16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 26277 26253 0 18:03 pts/2 00:00:00 grep --color=auto nginx
nginx 30744 30743 0 08:43 ? 00:00:00 php-fpm: pool www
nginx 30745 30743 0 08:43 ? 00:00:00 php-fpm: pool www
nginx 31607 4968 0 09:11 ? 00:00:00 nginx: worker process
master process(主进程) 是管理 nginx 的 worker process (工作进程) 的
work process(工作进程) 的进程才是为用户提供服务的
参数名称 | 参数说明 |
---|---|
master process | 以 root 用户运行的进程是 nginx 主进程, master process(主进程) 是管理 nginx 的 worker process (工作进程) 的 |
worker process | 以 nginx 用户运行的 worker process(工作进程) 是 nginx 的工作进程, work process(工作进程) 的进程才是为用户提供服务的 |
2.设置nginx
#查看linux系统中各个进程的资源占用情况
top
top - 17:41:01 up 1 day, 5:11, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 102 total, 1 running, 101 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1882104 total, 610204 free, 277828 used, 994072 buff/cache
KiB Swap: 8388604 total, 8382460 free, 6144 used. 1383916 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 190964 3468 2116 S 0.0 0.2 0:21.77 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.31 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:01.64 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.38 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 1:14.10 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:03.47 watchdog/0
12 root rt 0 0 0 0 S 0.0 0.0 0:02.11 watchdog/1
13 root rt 0 0 0 0 S 0.0 0.0 0:04.77 migration/1
14 root 20 0 0 0 0 S 0.0 0.0 0:03.54 ksoftirqd/1
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H
17 root rt 0 0 0 0 S 0.0 0.0 0:02.84 watchdog/2
18 root rt 0 0 0 0 S 0.0 0.0 0:00.32 migration/2
19 root 20 0 0 0 0 S 0.0 0.0 0:02.10 ksoftirqd/2
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/2:0H
22 root rt 0 0 0 0 S 0.0 0.0 0:02.32 watchdog/3
参数名称 | 参数说明 |
---|---|
VIRT | 进程使用的虚拟内存总量,单位为kb。VIRT=SWAP+RES |
RES | 进程使用的物理内存大小,单位kb。RES=CODE+DATA |
SHR | 共享内存大小,单位kb |
S | 进程状态,D=不可中断的睡眠状态,R=运行,S=睡眠,T=跟踪/停止,Z=僵尸进程 |
%CPU | CPU占用百分比 |
%MEM | 进程使用的物理内存百分比 |
#查看CPU的核心数,在top里面按下 1 就可以显示CPU的核心数,按下 q 就可以退出
#设置nginx的CPU核心数
vi /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 4;
......
#检查nginx配置是否有问题
nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重新加载nginx
nginx -s reload
配置要注意对应Linux系统的CPU核心数
#查看进程,显示出来了nginx的4个工作进程
#安装查看进程树之间的关系
yum install psmisc -y
#查看nginx进程树之间的主从关系
pstree -p | grep nginx
|-nginx(4968)-+-nginx(27502)
| |-nginx(27503)
| |-nginx(27504)
| `-nginx(27505)
#查看进程运行的CPU
taskset -pc 27502
pid 27502's current affinity list: 0-3
参数说明:
-p #查看进程ID
-c #查看 0-3 ,说明 nginx PID 48384 ,可以在 CPU ID 为 0 1,2,3 上运行,即 4 个 cpu 上都可以运行
#配置nginx进程绑定CPU
vi /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
......
#检查nginx配置是否有问题
nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重新加载nginx
nginx -s reload
参数名称 | 参数说明 |
---|---|
worker_cpu_affinity | 充分利用多核cpu,cpu是任务处理,当计算最费时的资源的时候,cpu核使用上的越多,性能就越好 |
01 10 | 2核cpu,开启2个进程 |
01 10 01 10 | 2核cpu,开启4个进程 |
0101 1010 | 4核cpu,开启2个进程 |
0001 0010 0100 1000 | 4核cpu,开启4个进程 |
00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000 | 8核cpu,开启8个进程 |
3.查看Nginx绑定Nginx进程到CPU上
#查看进程的主从
pstree -p | grep nginx
|-nginx(4968)-+-nginx(777)
| |-nginx(778)
| |-nginx(779)
| `-nginx(780)
#查看指定进程运行的CPU
taskset -pc 4968
pid 4968's current affinity list: 0-3
#查看指定进程运行的CPU
taskset -pc 777
pid 777's current affinity list: 0
#查看指定进程运行的CPU
taskset -pc 778
pid 778's current affinity list: 1
#查看指定进程运行的CPU
taskset -pc 779
pid 779's current affinity list: 2
#查看指定进程运行的CPU
taskset -pc 780
pid 780's current affinity list: 3
#查看指定nginx进程的资源占用情况
top -u nginx
top - 20:38:12 up 1 day, 8:08, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 105 total, 1 running, 104 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1882104 total, 582864 free, 279868 used, 1019372 buff/cache
KiB Swap: 8388604 total, 8382460 free, 6144 used. 1381660 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
777 nginx 20 0 46516 2048 572 S 0.0 0.1 0:00.00 nginx
778 nginx 20 0 46516 2048 572 S 0.0 0.1 0:00.00 nginx
779 nginx 20 0 46516 2048 572 S 0.0 0.1 0:00.00 nginx
780 nginx 20 0 46516 2048 572 S 0.0 0.1 0:00.00 nginx
30744 nginx 20 0 217724 8352 2896 S 0.0 0.4 0:00.00 php-fpm
30745 nginx 20 0 217724 8356 2896 S 0.0 0.4 0:00.00 php-fpm
#查看nginx内存资源占用的大小,在top里面按下 e 可以显示MB单位等,按下 q 就是退出