cmake nginx 本地开发调试

主要是学习下如何基于cmake 进行nginx 的本地调试,也方便学习如何开发nginx 模块

参考配置

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(ngx_hello_module)
 
# Path to the Nginx source code
set(NGINX_SOURCE_PATH ${CMAKE_SOURCE_DIR}/nginx)
 
# Nginx module source files
set(MODULE_SOURCES
        src/ngx_http_hello_world_module.c
)
 
set(WITH_DEBUG "-O0 -g")
 
include(ExternalProject)
# 动态模块模式
ExternalProject_Add(
        nginx
        SOURCE_DIR ${NGINX_SOURCE_PATH}
        CONFIGURE_COMMAND cd ${NGINX_SOURCE_PATH} && ./configure --with-debug  --with-cc-opt=${WITH_DEBUG} --add-dynamic-module=${NGINX_SOURCE_PATH}/../src --with-compat
        BUILD_COMMAND make -C ${NGINX_SOURCE_PATH}/ modules
        INSTALL_COMMAND ""
)
# all-in-one 模式
ExternalProject_Add(
        nginxall
        SOURCE_DIR ${NGINX_SOURCE_PATH}
        CONFIGURE_COMMAND cd ${NGINX_SOURCE_PATH} && ./configure --with-debug  --with-cc-opt=${WITH_DEBUG}  --add-module=${NGINX_SOURCE_PATH}/../src --with-compat
        BUILD_COMMAND make -C ${NGINX_SOURCE_PATH}
        INSTALL_COMMAND ""
)
 
#ExternalProject_Add(
#        nginx_single
#        SOURCE_DIR ${NGINX_SOURCE_PATH}
#        CONFIGURE_COMMAND cd ${NGINX_SOURCE_PATH} && ./configure --with-debug  --with-cc-opt=${WITH_DEBUG}   --with-compat
#        BUILD_COMMAND make -C ${NGINX_SOURCE_PATH}
#        INSTALL_COMMAND ""
#)
# 模块copy
add_custom_command(
        TARGET nginx
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/output
        COMMAND ${CMAKE_COMMAND} -E copy ${NGINX_SOURCE_PATH}/objs/*.so ${CMAKE_SOURCE_DIR}/output/
        COMMENT "Copy nginx module to ${CMAKE_SOURCE_DIR}/output/"
)
# Nginx module name
set(MODULE_NAME ngx_hello_module)
# 核心是添加一些以来,方便本地开发的模块进行使用
# Include directories
include_directories(${NGINX_SOURCE_PATH}/src/core)
include_directories(${NGINX_SOURCE_PATH}/src/event)
include_directories(${NGINX_SOURCE_PATH}/src/os/unix)
include_directories(${NGINX_SOURCE_PATH}/objs)
include_directories(${NGINX_SOURCE_PATH}/src/http)
include_directories(${NGINX_SOURCE_PATH}/src/http/modules)
include_directories(${NGINX_SOURCE_PATH}/src/mail)
include_directories(${NGINX_SOURCE_PATH}/src/stream)
# change to your pcre2 include path
include_directories(/usr/local/Cellar/pcre2/10.42/include)
include_directories(src)
 
set(SOURCE_FILES
        ${NGINX_SOURCE_PATH}/objs/ngx_modules.c)
# 一个target 核心是为了方便添加本地代码,方便ide 使用
add_library(nginxapp  ${MODULE_SOURCES})
 

debug 配置

核心是选择选择构建的应用(nginx)以及配置nginx 启动配置,推荐的nginx 配置

worker_processes  1;
master_process off;
daemon off;

同时可以结合实际配置一些debug 日志

说明

以上测试比较粗糙,可以简单的集成起来,按照比较标准的是应该修改一些nginx 的构建命令,让支持cmake 这样就可以更方便的进行开发测试了
以下参考链接中也有一个关于基于cmake 以及ExternalProject_Add 开发nginx 的实践可以参考

参考资料

https://cmake.org/cmake/help/latest/module/ExternalProject.html
https://github.com/rongfengliang/nginx_debug_cmake_learning
https://blog.openziti.io/lessons-learned-writing-a-zero-trust-nginx-module

posted on 2023-11-15 09:32  荣锋亮  阅读(120)  评论(0编辑  收藏  举报

导航