Nginx 添加 tcmalloc 模块

要给nginx添加tcmalloc模块需要安装gperftools,而gperftools又依赖libunwind 。


libunwind安装

libunwind下载地址

开始安装:

sudo yum -y install autoconf automake libtool gcc make gcc-c++ cmake lsof wget vim lrzsz

./configure

sudo make -j 4

sudo make install

安装gperftools

gperftools下载地址

编译安装:

./configure --enable-frame-pointers

sudo make -j 4

sudo make install

生成库文件

cat /etc/ld.so.conf.d/usr_local_lib.conf

添加

/usr/local/lib

使库文件生效:

sudo /sbin/ldconfig

重新编译Nginx

./configure --user=echoxu --group=echoxu --prefix=/home/echoxu/software/nginx-1.14.2 \
--with-zlib=/home/echoxu/software/tools/zlib-1.2.11  \
--with-pcre=/home/echoxu/software/tools/pcre-8.42  \
--with-http_stub_status_module --with-http_gzip_static_module \
--with-http_realip_module --with-http_ssl_module \
--with-openssl=/home/echoxu/software/tools/openssl-1.0.2p  --with-http_mp4_module  \
--with-http_realip_module --with-http_v2_module   --with-http_secure_link_module \
--with-google_perftools_module

注意

如果在编译时添加--with-ld-opt=-ltcmalloc即采用静态编译方法添加tcmalloc模块

  • 添加线程目录

mkdir /tmp/tcmalloc

chown -R APPDeploy:APPDeploy /tmp/tcmalloc

在nginx.conf里添加

google_perftools_profiles /tmp/tcmalloc;

如果是在编译时添加--with-ld-opt=-ltcmalloc则不用添加此项.

  • 查看tcmalloc是否生效

如下这样就是tcmalloc配置生效了:

[echoxu@mysql SPECS]$ sudo lsof -n |grep tcmalloc
nginx     21912      APPDeploy    9w      REG              253,0         0    4531767 /tmp/tcmalloc.21912
nginx     21913      APPDeploy   11w      REG              253,0         0    4531776 /tmp/tcmalloc.21913
nginx     21914      APPDeploy   13w      REG              253,0         0    4531775 /tmp/tcmalloc.21914

如果是用静态编译的方法添加的tcmalloc则效果如下:

[echoxu@mysql cron.hourly]$ sudo lsof -n | grep tcmalloc
nginx     1029      APPDeploy  mem       REG              253,0   2347000    8419330 /usr/local/lib/libtcmalloc.so.4.5.3
nginx     1032      APPDeploy  mem       REG              253,0   2347000    8419330 /usr/local/lib/libtcmalloc.so.4.5.3
nginx     1033      APPDeploy  mem       REG              253,0   2347000    8419330 /usr/local/lib/libtcmalloc.so.4.5.3
nginx     1034      APPDeploy  mem       REG              253,0   2347000    8419330 /usr/local/lib/libtcmalloc.so.4.5.3

非root用户使用1024以下的端口

用root或者拥有sudo权利的用户执行如下命令:

sudo setcap cap_net_bind_service=+eip /home/echoxu/software/nginx/sbin/nginx

  • 清除能力:

sudo setcap -r /home/echoxu/software/nginx/sbin/nginx

geoip2模块

上次更新:
贡献者: iEchoxu