Young's blog Young's blog
首页
Spring
  • 前端文章1

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Young

首页
Spring
  • 前端文章1

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Nginx基础
  • Nginx配置
  • Nginx proxy_pass 配置转发 / 路径
  • Nginx 配置密码认证
  • Nginx 配置 CPU 亲和性
    • 环境说明
    • 默认的配置
      • CPU 绑定关系
    • 配置 workercpuaffinity
      • CPU 绑定关系
    • 压测验证
  • Nginx 配置 UDP/TCP/WebSocket 反向代理
  • Nginx 配置目录文件列表显示功能
  • Nginx 配置 gzip 压缩、缓存功能
  • Nginx 配置端口转发
  • Nginx底层进程机制剖析
  • nginx配置location匹配顺序总结
  • nginx
andanyang
2023-03-29
目录

Nginx 配置 CPU 亲和性

Ningx 使用 worker_cpu_affinity 绑定 CPU,提高 Nginx 工作效率。

# 环境说明

  • CentOS 7 4c4g

# 默认的配置

修改 /etc/nginx/nginx.conf,默认的 CPU 参数为:

worker_processes auto;
1

# CPU 绑定关系

启动服务后,Nginx 进程与 CPU 亲和关系:

$ systemctl start nginx
$ ps -ef|grep nginx
root       1889      1  0 10:02 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      1890   1889  0 10:02 ?        00:00:00 nginx: worker process
nginx      1891   1889  0 10:02 ?        00:00:00 nginx: worker process
nginx      1892   1889  0 10:02 ?        00:00:00 nginx: worker process
nginx      1893   1889  0 10:02 ?        00:00:00 nginx: worker process
root       1896   1745  0 10:02 pts/0    00:00:00 grep --color=auto nginx
$ taskset -c -p 1890
pid 1890's current affinity list: 0-2
$ taskset -c -p 1891
pid 1891's current affinity list: 0-2
$ taskset -c -p 1892
pid 1892's current affinity list: 0-2
$ taskset -c -p 1893
pid 1893's current affinity list: 0-2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

通过 taskset 命令,我们可以看到每一个 nginx 子进程都 0-2 的 CPU 和绑定。

# 配置 worker_cpu_affinity

修改 worker_processes auto 为:

worker_processes    4;
worker_cpu_affinity 0001 0010 0100 1000;
1
2

说明:

  • worker_processes 分配给 Nginx 的 CPU 数量
  • worker_cpu_affinity 二进制,0001 表示启用第一个 CPU,依次类推

# CPU 绑定关系

Nginx 进程与 CPU 亲和关系:

$ ps -ef|grep nginx
root       1930      1  0 10:03 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      1931   1930  0 10:03 ?        00:00:00 nginx: worker process
nginx      1932   1930  0 10:03 ?        00:00:00 nginx: worker process
nginx      1933   1930  0 10:03 ?        00:00:00 nginx: worker process
nginx      1934   1930  0 10:03 ?        00:00:00 nginx: worker process
root       1936   1745  0 10:03 pts/0    00:00:00 grep --color=auto nginx
$ taskset -c -p 1931
pid 1931's current affinity list: 0
$ taskset -c -p 1932
pid 1932's current affinity list: 1
$ taskset -c -p 1933
pid 1933's current affinity list: 2
$ taskset -c -p 1934
pid 1934's current affinity list: 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

通过 taskset 命令,我们可以看到每一个 nginx 子进程都和指定的 CPU 核绑定。

# 压测验证

用 wrk 工具,进行性能测试,在服务器上执行 top,然后按 1,可以看到 4 个 cpu 内核的利用率差不多,证明 nginx 已经成功利用了多核 cpu。

编辑 (opens new window)
上次更新: 2024/04/19, 08:52:45
Nginx 配置密码认证
Nginx 配置 UDP/TCP/WebSocket 反向代理

← Nginx 配置密码认证 Nginx 配置 UDP/TCP/WebSocket 反向代理→

最近更新
01
idea 热部署插件 JRebel 安装及破解,不生效问题解决
04-10
02
spark中代码的执行位置(Driver or Executer)
12-12
03
大数据技术之 SparkStreaming
12-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Young | MIT License
浙ICP备20002744号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式