nginx 源码(1)编译
今年准备认真一下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教程有兴趣的朋友有所帮助。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

解决 Nginx 跨域问题有两种方法:修改跨域响应头:添加指令以允许跨域请求,指定允许的方法和头,以及设置缓存时间。使用 CORS 模块:启用模块并配置 CORS 规则,允许跨域请求、方法、头和设置缓存时间。

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP
