首页 > 系统相关 >Nginx调优总结-第六部分编译优化与简单测试

Nginx调优总结-第六部分编译优化与简单测试

时间:2023-01-24 22:44:54浏览次数:32  
标签:opt 编译 nginx -- Nginx 调优 conf http

第六部分 编译优化

Nginx可以自行编译,所以里面可以设置多个编译策略. 
也可以自行修改源码,便于比如进行ip_hash的全IP地址验证.
也可以修改nginx的版本号等信息, 避免内发现. 

还可以按照不同的CPU类型进行优化
选择更高级别的优化参数.
不编译debug等的信息,便于降低内存占用等. 

修改源码-1

ip_hash 时修改为全部IPV4地址段进行取hash处理

src/http/modules/ngx_http_upstream_ip_hash_module.c

把里面的3修改成4 就可以进行全IP地址段的hash运算. 

    case AF_INET:
        sin = (struct sockaddr_in *) r->connection->sockaddr;
        iphp->addr = (u_char *) &sin->sin_addr.s_addr;
        iphp->addrlen = 3; # 修改成4 
        break;
    default:
        iphp->addr = ngx_http_upstream_ip_hash_pseudo_addr;
        iphp->addrlen = 3; # 修改成4
    }

    for ( ;; ) {

        for (i = 0; i < (ngx_uint_t) iphp->addrlen; i++) {
            hash = (hash * 113 + iphp->addr[i]) % 6271;
        }

修改源码-2

修改nginx的版本号
需要修改多个文件:
注意 我把 nginx修改成我我的ID. 

vim src/core/nginx.h
#define NGINX_VERSION      "1127"
#define NGINX_VER          "JNXLH/" NGINX_VERSION
#define NGINX_VAR          "JNXLH"

vim src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: JNXLH" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;

修改源码-3

去除 debug信息

vim auto/cc/gcc
# debug
CFLAGS=”$CFLAGS -g”
可以将 CFLAGS进行注释就可以. 

注意 取消了debug之后文件会出现极大的缩小. 

我用下面的编译脚本编译完后文件由8.3M 减小到了4.4M大小. 

编译打包命令

./configure --prefix=/opt/nginx \
--sbin-path=/opt/nginx/nginx \
--conf-path=/opt/nginx/nginx.conf \
--pid-path=/opt/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre2-10.42 \
--with-zlib=../zlib-1.2.13 \
--with-openssl=../openssl-1.1.1s \
--with-stream \
--with-stream_ssl_preread_module \
--with-cc-opt='-O3' \
--with-cpu-opt=core 

编译打包命令解析

--with-pcre=../pcre2-10.42 \
--with-zlib=../zlib-1.2.13 \
--with-openssl=../openssl-1.1.1s \
这三个是使用了很新的组建, 有正则表达式, 压缩 和openssl加解密.

--with-http_ssl_module \
增加https的支持.

--with-stream \
--with-stream_ssl_preread_module \
增加四层负载的功能. 
增加四层负载然后可以进行转发https的功能. 

--with-cc-opt='-O3' \
编译优化参数开到最高, 编译时间变长,但是编译后的性能最好. 

--with-cpu-opt=core 
官方说明里面可以写pentium等.但是据说如果是服务器可以写core
所以进行验证了. 

config的结果信息

Configuration summary
  + using PCRE2 library: ../pcre2-10.42
  + using OpenSSL library: ../openssl-1.1.1s
  + using zlib library: ../zlib-1.2.13

  nginx path prefix: "/opt/nginx"
  nginx binary file: "/opt/nginx/nginx"
  nginx modules path: "/opt/nginx/modules"
  nginx configuration prefix: "/opt/nginx"
  nginx configuration file: "/opt/nginx/nginx.conf"
  nginx pid file: "/opt/nginx/nginx.pid"
  nginx error log file: "/opt/nginx/logs/error.log"
  nginx http access log file: "/opt/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译成功后处理响应文件

进入编译后的文件目录
cd /opt/nginx
执行命令
sed -i 's/nginx/JNXLH/g' html/index.html
就会修改 访问界面, 没有nginx的字样了. 

然后F12看到的服务器信息为:
Server: JNXLH/1127

使用ab进行验证

安装ab非常简单:
yum -y install httpd-tools

最简单的测试可以使用两个参数进行:
-n	即requests,用于指定压力测试总共的执行次数。
-c	即concurrency,用于指定的并发数。

简单的ab测试结果

优化后的测试结果:
ab -c 10000 -n 50000 http://10.110.80.116:81/

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  244  65.2    242    1287
Processing:    71  258  85.4    253     504
Waiting:        0   32  99.2      0     381
Total:        385  503  55.8    496    1502

优化编译前的测试结果:
ab -c 10000 -n 50000 http://10.110.80.116:82/

              min  mean[+/-sd] median   max
Connect:        0  258  84.9    252    1388
Processing:    76  271  95.5    264     541
Waiting:        0   34 105.8      0     444
Total:        426  529  74.8    506    1509

基于容器运行的测试结果

docker run --name nginx83 -d -p 83:83 -v /opt/nginx/nginx83.conf:/etc/nginx/nginx.conf nginx
使用docker0 方式 原生 nginx 无调优的容器.
挂载模式的nginx的配置文件唯一不同的是使用83端口. 
ab -c 10000 -n 50000 http://10.110.80.116:83/
              min  mean[+/-sd] median   max
Connect:        0  344 158.6    325    1344
Processing:    48  326 109.4    326     664
Waiting:        0   28 105.5      0     470
Total:        429  671 175.9    633    1829

docker run --name nginx84 -d --net=host -v /opt/nginx/nginx84.conf:/etc/nginx/nginx.conf nginx
这个命令使用 宿主机的网络进行暴露服务. 
端口使用 84的端口. 
ab -c 10000 -n 50000 http://10.110.80.116:84/
              min  mean[+/-sd] median   max
Connect:        0  254  55.7    254     363
Processing:    53  266  88.9    263     515
Waiting:        0   33 102.2      0     373
Total:        346  520  46.6    516     678

最小化编译的二进制的性能测试结果

编译配置文件为:
./configure --prefix=/opt/nginx85 \
--sbin-path=/opt/nginx85/nginx \
--conf-path=/opt/nginx85/nginx.conf \
--pid-path=/opt/nginx85/nginx.pid \
--with-cc-opt='-O3' \
--with-cpu-opt=core 

注意这样编译打包的文件 仅有 900k 左右. 比之前的4.4M又有了较大的缩减. 
ab -c 10000 -n 50000 http://10.110.80.116:85/
              min  mean[+/-sd] median   max
Connect:        0  242  68.6    240    1292
Processing:    53  254  86.9    250     502
Waiting:        0   31  97.9      0     356
Total:        336  496  62.6    493    1507

测试结果的简单结论

1. 使用--net=host 应该比使用单纯的 -p docker0 网络要好很多. 
2. 进行编译优化的二进制应该比不编译优化的二进制要好一些. 但是不是特别明显.
3. 增加了过多的模块可能对性能有影响. 最简化的编译应该是性能最好的. 
4. 基于3的结论,重新编译一个最小化编译的包进行再次验证. 发现包含的模块越少,性能越好.
5. 多次进行ab 的压测与验证, 结论基本上符合这个规律.
6. 平均数较难取得.本次仅使用一个worker 进行测试验证. 机器配置较高.

扩充测试

工作线程和内核绑定都是AUTO时
4路18核心 144线程的测试结果:
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  434 165.6    403    1359
Processing:    44  487 143.3    487     761
Waiting:        0  142  91.3    134     749
Total:        594  922 137.8    941    1921
是所有的测试结果里面最差的. 

改成 8个工作进程.加auto绑定核心
              min  mean[+/-sd] median   max
Connect:        0  381 170.0    350    1549
Processing:    63  408 112.4    410     616
Waiting:        0  157  98.7    141     589
Total:        506  788 158.1    818    1842

也比较差. 但是比AUTO要好
说明在简单处理情况下单线程最好. 
但是稍微复杂一点 包含反向代理的还是需要多几个worker进程才可以.

标签:opt,编译,nginx,--,Nginx,调优,conf,http
From: https://www.cnblogs.com/jinanxiaolaohu/p/17066480.html

相关文章

  • 反编译 小程序
    原文来自:抓取微信小程序源码-丁少华-博客园(cnblogs.com)我怕原文删除了,我就找不到了想成为一名微信小程序的开发者,前端思路的学习和安全意识是非常有必要的,故务必......
  • Nginx同一个worker进程先后处理耗时不同的请求
    epoll监控的事件src/event/modules/ngx_epoll_module.cEPOLLIN:连接上有数据可读(包括TCP连接的远端主动关闭连接)EPOLLLPRI:连接上要读紧急数据EPOLLOUT:连接上写入数据......
  • STM32F401 Proteus 仿真 串口两种发送方式 编译用GCC ,寄存器配置方式
    用的proteus8.9中文版,STM32F401可能是支持的最复杂的MCU了吧,就用这个做实验了。编译器用GCC,在proteus中安装调试都很方便,编程实验用寄存器配置方式,因为仅仅是学习,简单直......
  • Nginx与LUA(7)
    您好,我是湘王,这是我的51CTO博客。值此新春佳节,我给您拜年啦~祝您在新的一年中所求皆所愿,所行皆坦途,展宏“兔”,有钱“兔”,多喜乐,常安宁!软件开发中,除了进程和线程,还有协程的概......
  • nginx添加身份认证
    前言有一些静态网站资源,我们不希望所有人都可以访问,那么可以简单使用nginx内置模块实现身份认证。实现修改配置文件:auth_basic"nginxbasichttptest";auth_basic_u......
  • Nginx与LUA(7)
    您好,我是湘王,这是我的博客园。值此新春佳节,我给您拜年啦~祝您在新的一年中所求皆所愿,所行皆坦途,展宏“兔”,有钱“兔”,多喜乐,常安宁!   软件开发中,除了进程和线程,还有......
  • nginx添加身份认证
    前言有一些静态网站资源,我们不希望所有人都可以访问,那么可以简单使用nginx内置模块实现身份认证。实现修改配置文件:auth_basic"nginxbasichttptest";auth_basic_us......
  • Nginx 系列 | (转)Nginx 上传文件:client_max_body_size 、client_body_buffer_size
    原文:http://php-note.com/article/detail/488client_max_body_sizeclient_max_body_size默认1M,表示客户端请求服务器最大允许大小,在“Content-Length”请求头中指......
  • 精华推荐 | 【JVM深层系列】「GC底层调优系列」一文带你彻底加强夯实底层原理之GC垃圾
    前提介绍很多小伙伴,都跟我反馈,说自己总是对JVM这一块的学习和认识不够扎实也不够成熟,因为JVM的一些特性以及运作机制总是混淆以及不确定,导致面试和工作实战中出现了很多的纰......
  • Linux下手工编译libiconv库的小问题
    我的电脑是Ubuntu14.04LTS,自己手工编译php5.6,打开ZEND_EXTRA_LIBS='-liconv'时,发现没有安装libiconv,也就是编码转换的库,所以百度该库的安装方法,如下:......