Nginx如何编译呢?本章节对Nginx 服务器编译优化进行详细说明:
1、 GCC 参数:
默认Nginx 使用的GCC 编译参数是-O,需要更加优化可以使用以下两个参数
–with-cc-opt=’-O3′
–with-cpu-opt=opteron
使得编译针对特定CPU 以及增加GCC 的优化,针对优化后的结果.我们进行测试结果表明使用-O2 以及以上
的参数,可以微量增加性能 1%左右,而 O2 和O3 基本可以认为是相同的:
./http_load -parallel 100 -seconds 10 urls
10811 fetches, 100 max parallel, 5.23252e+06 bytes, in 10 seconds
a、默认参数 -O
1087.2 fetches/sec, 526204 bytes/sec
msecs/connect: 45.5374 mean, 63.984 max, 1.008 min
msecs/first-response: 45.7679 mean, 64.201 max, 2.216 min
1088.9 fetches/sec, 527027 bytes/sec
msecs/connect: 45.0159 mean, 65.291 max, 0.562 min
msecs/first-response: 46.1236 mean, 67.397 max, 9.169 min
1102.2 fetches/sec, 533465 bytes/sec
msecs/connect: 44.5593 mean, 67.649 max, 0.547 min
msecs/first-response: 45.499 mean, 67.849 max, 2.495 min
B、优化编译后 -O2
1081.1 fetches/sec, 523252 bytes/sec
msecs/connect: 45.7144 mean, 63.324 max, 0.823 min
msecs/first-response: 46.1008 mean, 61.814 max, 4.487 min
1110.2 fetches/sec, 537337 bytes/sec
msecs/connect: 43.4943 mean, 60.066 max, 0.715 min
msecs/first-response: 45.756 mean, 62.076 max, 3.536 min
1107 fetches/sec, 535788 bytes/sec
msecs/connect: 44.872 mean, 3036.51 max, 0.609 min
msecs/first-response: 44.8625 mean, 59.831 max, 3.178 min
C、优化编译后 -O3
1097.5 fetches/sec, 531189 bytes/sec
msecs/connect: 45.1355 mean, 3040.24 max, 0.583 min
msecs/first-response: 45.3036 mean, 68.371 max, 4.416 min
1111.6 fetches/sec, 538014 bytes/sec
msecs/connect: 44.2514 mean, 64.831 max, 0.662 min
msecs/first-response: 44.8366 mean, 69.904 max, 3.928 min
1099.4 fetches/sec, 532109 bytes/sec
msecs/connect: 44.7226 mean, 61.445 max, 0.596 min
msecs/first-response: 45.4883 mean, 287.113 max, 3.336 min
2、修改Nginx 的header 伪装服务器
# cd Nginx-0.6.31
# vi src/core/Nginx.h
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define NGINX_VERSION "7.2"
#define NGINX_VER "Freeke/" NGINX_VERSION
#define NGINX_VAR "NGINX"
#define NGX_OLDPID_EXT ".oldbin"
#endif /* _NGINX_H_INCLUDED_ */
# curl -I www.qvdv.net
HTTP/1.1 200 OK
Server: Freeke/7.2
Date: Mon, 24 Nov 2008 02:42:51 GMT
Content-Type: text/html; charset=gbk
Transfer-Encoding: chunked
Connection: keep-alive
3、Tcmalloc 优化Nginx 性能
从Nginx 0.6.29 添加 Feature: the ngx_google_perftools_module,那Nginx 也可以利用Tcmalloc 来提长
性能。
[root@chinarenservice ~]# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
[root@chinarenservice ~]# tar zxvf libunwind-0.99-alpha.tar.gz
[root@chinarenservice ~]# cd libunwind-0.99-alpha/
[root@chinarenservice ~]# CFLAGS=-fPIC ./configure
[root@chinarenservice ~]# make CFLAGS=-fPIC
[root@chinarenservice ~]# make CFLAGS=-fPIC install
[root@chinarenservice ~]# wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz
[root@chinarenservice ~]# tar zxvf google-perftools-0.98.tar.gz
[root@chinarenservice ~]# cd google-perftools-0.98/
[root@chinarenservice ~]# ./configure
[root@chinarenservice ~]# make && make install
[root@chinarenservice ~]# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
[root@chinarenservice ~]# ldconfig
[root@ chinarenservice local]# lsof -n | grep tcmalloc
Nginx 7323 root mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7324 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7325 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7326 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7327 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7328 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7329 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7330 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
Nginx 7331 www mem REG 8,2 1412859 440730
/usr/local/lib/libtcmalloc.so.0.0.0
在编译Nginx 时添加参数--with-google_perftools_module
4、减小编译后文件大小:
默认的Nginx 编译选项里居然是用debug 模式(-g)的(debug 模式会插入很多跟踪和ASSERT 之类),编
译以后一个Nginx 有好几兆。去掉Nginx 的debug 模式编译,编译以后只有 375K (Nginx-0.5.33, gcc4 )。在
auto/cc/gcc,最后几行有:
# debug
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。-g
关于Nginx编译的问题,讲的就这么多,希望对使用Nginx服务器的朋友有所帮助。
本文版权所有,转载须注明:来源 https://www.qvdv.net/qvdv-zhuji-798.html