3.3.3 直接修改Makefile文件

3.3.2 节中介绍的方法毫无疑问是最方便的,因为大量的工作已由Nginx中的configure脚本帮我们做好了。在使用其他第三方模块时,一般也推荐使用该方法。

我们有时可能需要更灵活的方式,比如重新决定ngx_module_t*ngx_modules[]数组中各个模块的顺序,或者在编译源代码时需要加入一些独特的编译选项,那么可以在执行完configure后,对生成的objs/ngx_modules.c和objs/Makefile文件直接进行修改。

在修改objs/ngx_modules.c时,首先要添加新增的第三方模块的声明,如下所示。


extern ngx_module_t ngx_http_mytest_module;


其次,在合适的地方将模块加入到ngx_modules数组中。


ngx_module_t*ngx_modules[]={

……

&ngx_http_upstream_ip_hash_module,

&ngx_http_mytest_module,

&ngx_http_write_filter_module,

……

NULL

};


注意,模块的顺序很重要。如果同时有两个模块表示对同一个请求感兴趣,那么只有顺序在前的模块会被调用。

修改objs/Makefile时需要增加编译源代码的部分,例如:


objs/addon/httpmodule/ngx_http_mytest_module.o:$(ADDON_DEPS)\

../sample/httpmodule//ngx_http_mytest_module.c

$(CC)-c$(CFLAGS)$(ALL_INCS)\

-o objs/addon/httpmodule/ngx_http_mytest_module.o\

../sample/httpmodule//ngx_http_mytest_module.c


还需要把目标文件链接到Nginx中,例如:


objs/nginx:

objs/src/core/nginx.o\

……

objs/addon/httpmodule/ngx_http_mytest_module.o\

objs/ngx_modules.o

$(LINK)-o objs/nginx\

objs/src/core/nginx.o\

……

objs/addon/httpmodule/ngx_http_mytest_module.o\

objs/ngx_modules.o\

-lpthread-lcrypt-lpcre-lcrypto-lcrypto-lz


请慎用这种直接修改Makefile和ngx_modules.c的方法,不正确的修改可能导致Nginx工作不正常。