nginx 源码(1)编译
Aug 08, 2016 am 09:28 AM今年准备认真一下nginx源码,目的是学习网络编程,我用的源码公开发布的第一个版本 nginx-0.1.0-RELEASE,代码地址: http://hg.nginx.org/nginx/rev/551102312e19
在浏览器里直接点左边的zip或gz就可以下载了。解压后源码目录下有4个文件夹:
- auto
- conf
- docs
- src
把auto目录下的configure文件拷贝到源码目录,运行 .configure
就可以生成Makefile,同时configure命令的输出,在我的ubuntu上看起来是这样的:
Configuration summary
+ PCRE library is not found
+ md5 library is not used
+ OpenSSL library is not used
+ using system zlib library./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.`
未找到PCRE库,因此无法正确安装HTTP rewrite模块。
执行命令: sudo apt-get install libpcre3 libpcre3-dev
后再执行make clean,configure;显示结果:
Configuration summary
+ using system PCRE library
+ md5 library is not used
+ OpenSSL library is not used
+ using system zlib librarynginx path prefix: /usr/local/nginx
nginx binary file: /usr/local/nginx/sbin/nginx
nginx configuration file: /usr/local/nginx/conf/nginx.conf
nginx pid file: /usr/local/nginx/logs/nginx.pid
nginx error log file: /usr/local/nginx/logs/error.log
nginx http access log file: /usr/local/nginx/logs/access.log
md5和openssl未使用,这个以后再说。然后make,会出现错误,打开objs/Makefie文件,查看当前的编译选项: CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused -Werror -g
其中
-Werror 把警告当作错误。出现任何警告就放弃编译。
-Wpointer-arith 对函数指针或者void *类型的指针进行算术操作时给出警告。也很有用。 -Wall 并不会打开此项。
-pipe 使用管道代替临时文件。
-Wno-unused 未使用的变量给出警告
把后面几个选项都去掉,重新make。还会出错:显示宏ngx_blocking_n在文件ngx_event_accept.c中未声明,查看objs/Makefile发现这个编译错误来自命令: gcc -c -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs src/event/ngx_event_accept.c -o ngx_event_accept.o
最终查看文件src/os/unix/ngx_socket.h,其中的定义如下:
<code><span>#if (HAVE_FIONBIO)</span><span>int</span> ngx_nonblocking(ngx_socket_t s); <span>int</span> ngx_blocking(ngx_socket_t s); <span>#define ngx_nonblocking_n "ioctl(FIONBIO)"</span><span>#define ngx_blocking_n "ioctl(!FIONBIO)"</span><span>#else</span><span>#define ngx_nonblocking(s) fcntl(s, F_SETFL, O_NONBLOCK)</span><span>#define ngx_nonblocking_n "fcntl(O_NONBLOCK)"</span><span>#define ngx_blocking_n "ioctl(!FIONBIO)"</span><span>#endif</span></code>
不含倒数第2行代码,那是我新加的,测试发现上面的if分支在ubuntu下是走的else代码块,但else中没有定义宏ngx_blocking_n,所以加上就好了。
继续make。
发现在ngx_writev_chain.c中找不到IOV_MAX, 使用命令 grep IOV_MAX -r src/os/unix/*
发现这个宏只在freebsd系统下才有定义,直接加到core/ngx_config.h中。
继续make。
发现struct msghdr中没有成员msg_accrights和msg_accrightslen,这是两个低版本的操作系统才有的变量名,高版本也有但是名字变了,查看文件src/os/unix/ngx_channel.c中代码如下:
<code><span>#if (HAVE_MSGHDR_MSG_CONTROL)</span> msg.msg_control = (caddr_t) &cmsg; msg.msg_controllen = <span>sizeof</span>(cmsg); <span>#else</span> msg.msg_accrights = (caddr_t) &fd; msg.msg_accrightslen = <span>sizeof</span>(<span>int</span>); <span>#endif</span></code>
此处已经考虑了版本问题,定义了一个宏来区分,肯定是这个宏未定义,条件走到了else分支所以报错,直接在文件内定义宏:
<code><span>#define HAVE_MSGHDR_MSG_CONTROL 1</span></code>
继续make。
这次所有的目标文件已经生成,但链接的时候出错了,原因还是一些符号找不到,出错内容如下:
objs/src/core/ngx_times.o:在函数‘ngx_time_update’中:
/home/nginx-0.1.1/src/core/ngx_times.c:179:对‘ngx_timezone’未定义的引用
objs/src/event/ngx_event_accept.o:在函数‘ngx_event_accept’中:
/home/nginx-0.1.1/src/event/ngx_event_accept.c:165:对‘ngx_blocking’未定义的引用
objs/src/event/ngx_event_connect.o:在函数‘ngx_event_connect_peer’中:
/home/nginx-0.1.1/src/event/ngx_event_connect.c:301:对‘ngx_blocking’未定义的引用
objs/src/event/modules/ngx_rtsig_module.o:在函数‘ngx_rtsig_done’中:
/home/nginx-0.1.1/src/event/modules/ngx_rtsig_module.c:173:对‘ngx_poll_module_ctx’未定义的引用
objs/src/event/modules/ngx_rtsig_module.o:在函数‘ngx_rtsig_init’中:
/home/nginx-0.1.1/src/event/modules/ngx_rtsig_module.c:134:对‘ngx_poll_module_ctx’未定义的引用
collect2: error: ld returned 1 exit status
一次全部解决
1 在src/core/ngx_times.c文件里代码又走到了else分支里,然后在 src/os/unix/ngx_time.h中只有solaris才定义了ngx_timezone这个函数:
<code><span>#<span>define</span> ngx_timezone(isdst) (- (isdst ? altzone : timezone) / 60)</span></code>
放开宏定义会发现找不到altzone, 暂时不管这个,把它直接改成0:
<code><span>#<span>define</span> ngx_timezone(isdst) (- (isdst ? 0 : timezone) / 60)</span></code>
2 src/event/ngx_event_accept.c中未定义引用ngx_blocking,原因刚才已经找到了,在src/os/unix/ngx_socket.h中走了else分支,把if里的函数声明直接拷贝一份到else中,因为这是个函数,还有定义部分,在src/os/unix/ngx_socket.c中把这个函数从if宏定义中移出来。 注意,不要修改ngx_nonblocking函数。
3 src/event/modules/ngx_rtsig_module.c中未定义引用ngx_poll_module_ctx,查代码发现这是一个全局变量:
<code><span>extern</span> ngx_event_module_t ngx_poll_module_ctx;</code>
被定义在poll模块内,但编译的时候在objs/Makefile中没有编译这个模块,把它一起编译了,改3个地方,和epoll的编译一样,有epoll的地方直接复制epoll相关的内容,把里面的epoll改成poll就可以了。
最后make成功!
生成了nginx二进制文件。直接./nginx运行,报错:
[emerg] 11732#0: open() /usr/local/nginx/conf/nginx.conf failed (2: No such file or directory)
to be continued…
以上就介绍了nginx 源码(1)编译,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment autoriser l'accès au réseau externe au serveur Tomcat

Quelles sont les commandes de démarrage et d'arrêt de nginx ?

Bienvenue sur nginx !Comment le résoudre ?

Comment déployer le projet nodejs sur le serveur

Comment résoudre le problème de nginx lors de l'accès au site Web

Comment communiquer entre les conteneurs Docker
