01_自己在开发nginx的坑
1.同目录下文件无法识别
在nginx
的源码中configure
后,会在objs
路径下生成一个MakeFile
文件,作为noob的我为了可以再扩展模块中添加自定义的C文件,目前只能在自己自定义的ngx_http_helloworld_module.c
文件中使用
#include <cJSON.h>
#include <cJSON.c>
如上方式解决无法引用文件函数的问题。
2.编译添加库
同样也是修改objs\Makefile
3.使用g++进行编译
在MakeFile
中,其他文件依旧使用cc进行编译,自定义的C++文件使用g++进行编译、
3.1 修改一:MakeFiLe
CC = cc
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -lm
CPP = cc -E
#LINK = $(CC)
#下列内容为新增
CXX = g++
CXXFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -lm
LINK = $(CXX)
3.2 修改二:链接部分进行修改
在Makefile
中,找到自定义模块对应的链接部分,使用g++进行链接
objs/addon/src/ngx_http_helloworld_module.o: $(ADDON_DEPS) \
/home/tianyiyi/nginx/nginx-1.19.6/extends/ngx_http_helloworld_module/src/ngx_http_helloworld_module.c
#$(CC) -c $(CFLAGS) $(ALL_INCS) |
$(CXX) -c $(CXXFLAGS) $(ALL_INCS)
-o objs/addon/src/ngx_http_helloworld_module.o \
/home/tianyiyi/nginx/nginx-1.19.6/extends/ngx_http_helloworld_module/src/ngx_http_helloworld_module.c
3.3 修改三:添加extern C
在自定义模块的文件中,如果引用nginx的相关文件,需要使用extern C
extern C{
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_config.h>
}
4. 开启主线程和前台运行模式
在nginx.conf
中做如下配置
#前台运行
daemon off;
#master处理请求,不会fork出子进程
master_process off
5. 在已经编译好的Nginx中追加模块
5.1 先知晓Nginx当前配置
[root@tianyiyi ngx_TAP]# /usr/local/nginx_TAP/sbin/nginx -V
nginx version: nginx/1.19.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --with-http_auth_request_module --prefix=/usr/local/nginx_TAP --add-module=/home/tianyiyi/nginx/ngx_TAP/extends/ngx_http_access_control_module
5.2 在原先基础上增加要添加的module
此处追加http_auth_request_module
模块
在Nginx
的src
目录中执行
./configure --with-http_auth_request_module --prefix=/usr/local/nginx_TAP --add-module=/home/tianyiyi/nginx/ngx_TAP/extends/ngx_http_access_control_module
接着 make 即可