openresty中nginx的执行文件为何如此大?

151 views
Skip to first unread message

lhmwzy

unread,
Nov 13, 2012, 7:20:48 AM11/13/12
to open...@googlegroups.com
nginx/sbin/nginx的大小为10M,正常的NGINX大小只有几百K,是不是加的插件多了,就变大了?

nginx/sbin/nginx -V
nginx version: ngx_openresty/1.2.4.7
built by gcc 4.2.1 20070719  [FreeBSD]
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --add-module=../ngx_devel_kit-0.2.17 --add-module=../echo-nginx-module-0.41 --add-module=../xss-nginx-module-0.03rc9 --add-module=../ngx_coolkit-0.2rc1 --add-module=../set-misc-nginx-module-0.22rc8 --add-module=../form-input-nginx-module-0.07rc5 --add-module=../encrypted-session-nginx-module-0.02 --add-module=../drizzle-nginx-module-0.1.4 --add-module=../srcache-nginx-module-0.16 --add-module=../ngx_lua-0.7.4 --add-module=../headers-more-nginx-module-0.18 --add-module=../array-var-nginx-module-0.03rc1 --add-module=../memc-nginx-module-0.13rc3 --add-module=../redis2-nginx-module-0.09 --add-module=../redis-nginx-module-0.3.6 --add-module=../auth-request-nginx-module-0.2 --add-module=../rds-json-nginx-module-0.12rc10 --add-module=../rds-csv-nginx-module-0.05rc2 --with-ld-opt=-Wl,-rpath,/usr/local/libdrizzle/lib:/usr/local/openresty/luajit/lib --with-http_ssl_module

smallfish

unread,
Nov 13, 2012, 7:24:46 AM11/13/12
to open...@googlegroups.com
不会啊。我的只有1.5MB

1.5M 11 12 13:53 /tools/nginx/sbin/nginx

nginx version: ngx_openresty/1.2.4.7
built by gcc 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
TLS SNI support enabled
configure arguments: --prefix=/tools/ngx_openresty-1.2.4.7/nginx --add-module=../ngx_devel_kit-0.2.17 --add-module=../echo-nginx-module-0.41 --add-module=../xss-nginx-module-0.03rc9 --add-module=../ngx_coolkit-0.2rc1 --add-module=../set-misc-nginx-module-0.22rc8 --add-module=../form-input-nginx-module-0.07rc5 --add-module=../encrypted-session-nginx-module-0.02 --add-module=../drizzle-nginx-module-0.1.4 --add-module=../srcache-nginx-module-0.16 --add-module=../ngx_lua-0.7.4 --add-module=../headers-more-nginx-module-0.18 --add-module=../array-var-nginx-module-0.03rc1 --add-module=../memc-nginx-module-0.13rc3 --add-module=../redis2-nginx-module-0.09 --add-module=../redis-nginx-module-0.3.6 --add-module=../auth-request-nginx-module-0.2 --add-module=../rds-json-nginx-module-0.12rc10 --add-module=../rds-csv-nginx-module-0.05rc2 --with-ld-opt=-Wl,-rpath,/tools/ngx_openresty-1.2.4.7/luajit/lib --with-pcre=/downloads/pcre-8.31 --with-pcre-jit --with-http_ssl_module
--


lhmwzy

unread,
Nov 13, 2012, 7:27:07 AM11/13/12
to open...@googlegroups.com
我的确实是10M


2012/11/13 smallfish <smallf...@gmail.com>

agentzh

unread,
Nov 13, 2012, 2:37:20 PM11/13/12
to open...@googlegroups.com
Hello!

On Tue, Nov 13, 2012 at 4:20 AM, lhmwzy wrote:
> nginx/sbin/nginx的大小为10M,正常的NGINX大小只有几百K,是不是加的插件多了,就变大了?
>

可执行文件的大部分空间其实都是调试符号:

$ ls -lh /usr/local/openresty/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 9.6M Nov 11 11:50
/usr/local/openresty/nginx/sbin/nginx*

$ sudo strip /usr/local/openresty/nginx/sbin/nginx

$ ls -lh /usr/local/openresty/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 992K Nov 13 11:28
/usr/local/openresty/nginx/sbin/nginx*

我们看到,strip 掉可执行文件中的调试符号后,就从 9.6 MB 缩小到只有 992KB 了。但是,这样的话,使用 gdb 或者
systemtap/dtrace 就无法得到函数名、文件名以及行号等很多信息了。

如果你想保留调试符号,同时也真的很在乎可执行文件的大小的话,可以使用 DWARF 4 新增的 .debug_types
段特性来对调试符号进行压缩。具体做法是使用类似下面这样的命令行重新构造 openresty:

./configure --with-luajit --with-cc-opt='-fdebug-types-section' -j8
make -j8
sudo make install

然后我们再来看一下 nginx 可执行文件的大小:

$ ls -lh /usr/local/openresty/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 3.8M Nov 13 11:32
/usr/local/openresty/nginx/sbin/nginx*

现在只有 3.8 MB 了,相比原先的 9.6 MB 缩小了 60% 以上。

Best regards,
-agentzh

lhmwzy

unread,
Nov 13, 2012, 6:52:19 PM11/13/12
to open...@googlegroups.com
在FreeBSD下,无效

 + gcc version: 4.2.1 20070719  [FreeBSD]
checking for gcc -pipe switch ... found
checking for --with-ld-opt="-Wl,-rpath,/usr/local/libdrizzle/lib:/usr/local/openresty/luajit/lib" ... not found
./configure: error: the invalid value in --with-ld-opt="-Wl,-rpath,/usr/local/libdrizzle/lib:/usr/local/openresty/luajit/lib"

failed to run command: ./configure --prefix=/usr/local/openresty/nginx \...



agentzh

unread,
Nov 13, 2012, 7:33:21 PM11/13/12
to open...@googlegroups.com
Hello!

2012/11/13 lhmwzy:


> 在FreeBSD下,无效
>
> + gcc version: 4.2.1 20070719 [FreeBSD]

FreeBSD 9.0 默认的 gcc 4.2.1 是 5 年多以前的老版本。如果查看
build/nginx-1.2.4/objs/autoconf.err 文件末尾的错误信息的话,可以看到类似下面这样的输出:

checking for --with-ld-opt="-Wl,-rpath,/usr/local/openresty/luajit/lib"

cc1: error: unrecognized command line option "-fdebug-types-section"

即这个老版本的 gcc 并不支持 -fdebug-types-section 选项。

如果你的 FreeBSD 系统已经安装有 gcc47 软件包的话,可以通过 --with-cc=gcc47 选项来使用 gcc 4.7 编译:

./configure --with-cc=gcc47 --with-luajit
--with-cc-opt='-fdebug-types-section' -j8
gmake -j8
gmake install

这样虽然可以成功构造,但出于某种我不知道的原因,DWARF4 的 debug types 段在 FreeBSD 上并没有生效:

$ readelf -WS /usr/local/openresty/nginx/sbin/nginx | fgrep .debug_types

该命令在 FreeBSD 9.0 上没有任何输出,所以可执行文件的大小还是 10MB.

而在 Linux 上,该命令是有输出的:

$ readelf -WS /usr/local/openresty/nginx/sbin/nginx | fgrep .debug_types
[34] .debug_types PROGBITS 0000000000000000
37ba4c 01ea00 00 0 0 1

即确实生效了。

或许你可以发邮件到 FreeBSD 的官方邮件列表问询 :)

Best regards,
-agentzh

Reply all
Reply to author
Forward
0 new messages