Lazy loaded image
交叉编译 Openwrt 平台可用的 nginx 和需要的动态模块
Words 1401Read Time 4 min
2025-9-29
2025-11-10
date
related_level
slug
type
relate_date
summary
status
tags
category
last_updated
Nov 10, 2025 10:03 PM
是否已更新
orginal_page
是否推荐
ubuntu 环境安装编译相关依赖
  • sudo apt install -y gcc make
设置 Openwrt 目标平台交叉编译环境
  • 假设 openwrt 系统源码在 ~/openwrt
编译并安装 openwrt 工具链
  • cd ~/openwrt
  • ./scripts/feeds update -a
  • ./scripts/feeds install -a
  • make download
  • make tools/install
  • make toolchain/install -j$(($(nproc)/2))
  • make package/base-files/compile V=99
编译目标平台必要的依赖库文件
  • 直接提供 pcre, zlib, openssl 等库文件,不需要提供对应源代码路径
  • make package/libs/pcre2/compile V=99
  • make package/libs/zlib/compile V=99
  • make package/libs/openssl/compile V=99
  • make package/libs/libxml2/compile V=99
检查及设置构筑环境
设置编译变量
  • 优先使用交叉编译环境中的工具链
  • 宏文件和库文件路径
    • ls -al $TOOLCHAIN_DIR/include
    • ls -al $TARGET_DIR/usr/include
    • ls -al $TOOLCHAIN_DIR/lib
    • ls -al $TARGET_DIR/usr/lib
  • 测试是否正常设置
    • $CC -v
    • grep -r "memalign" {$TARGET_DIR/usr,$TOOLCHAIN_DIR}/include
      • 查找是否包含某个函数实现
在 Openwrt 上查看字节序
  • lscpu | grep "Byte Order”
报错解决
下载项目源码并构筑为 nginx 动态模块
下载源码
  • cd ~
  • git clone https://github.com/nginx/nginx.git
  • git clone https://github.com/nginx/njs.git
openwrt 上获取当前 nginx 编译参数
  • 个人使用的 iStoreOS 24.10-rc4 版本的 nginx-full 核心编译选项如下
/usr/sbin/nginx -V 2>&1 | tr ' ' '\n'
  • --target=x86_64-openwrt-linux \ --host=x86_64-openwrt-linux \ --build=x86_64-pc-linux-gnu \ --crossbuild=Linux::x86_64 \ --with-cc=x86_64-openwrt-linux-musl-gcc \ --with-ld-opt='-L/builder/shared-workdir/build/sdk/staging_dir/toolchain-x86_64_gcc-13.3.0_musl/usr/lib -L/builder/shared-workdir/build/sdk/staging_dir/toolchain-x86_64_gcc-13.3.0_musl/lib -fuse-ld=bfd -Wl,--gc-sections -flto=auto -fuse-linker-plugin -znow -zrelro'
--with-ld-opt 选项进一步解释
  • -L 指定链接器搜索库文件的路径
  • -fuse-ld=bfd 指定链接器后端为 GNU BFD(Binary File Descriptor)链接器
  • -Wl,--gc-sections 启用“垃圾回收”功能
  • -flto=auto 自动决定是否启用链接时优化
  • -fuse-linker-plugin 启用链接器插件
  • -znow 设置动态链接器的行为,强制所有符号在程序启动时立即解析
  • -zrelro 启用 RELRO(Relocation Read-Only)保护
修改 nginx 源码以支持交叉编译
注释 os 系统检测
  • vim ~/nginx/auto/configure
修改 $NGX_CC_NAME 为 openwrt 对应 cc
  • vim ~/nginx/auto/cc/conf
根据目标平台位数(32/64)设置 ngx_size=4/8 并注释自检
  • 这里为 64 位
  • vim ~/nginx/auto/types/sizeof
交叉编译时需要注释自检部分
  • 在 Openwrt 上查看字节序
    • lscpu | grep "Byte Order”
vim ~/nginx/auto/endianness
  • 全部注释掉
  • 如果是小端序,添加以下内容
    vim ~/njs/auto/endianness
    • 全部注释掉
    • 根据目标平台的字节序,替换掉以下部分
      vim ~/nginx/auto/feature
      替换 libxml2 路径(没有提供指定路径的选项)
      • njs 需要启用,记得禁掉自检
      vim ~/nginx/auto/lib/libxslt/conf
      • 自行替换 $TARGET_DIR 为实际路径
      配置并编译 nginx
      • cd ~/nginx
      • git checkout release-1.26.1
        • 与当前 openwrt 平台版本保持一致
      (可选)nginx 添加 openwrt 已有的动态模块
      • cd ~/istoreos
      make menuconfig
      • 互动式配置编译选项,勾选需要的模块
      • / 检索配置项所在位置,y 勾选启用,S 保存当前配置
        • nginx-mod-xxx 均在 network/Web Servers 项下
      • make package/nginx/prepare V=99
      • make package/nginx/compile -j$(($(nproc)/2)) V=99
      • 报错:package/kernel/linux "modules.builtin" No such file
        • 需要执行 make target/linux/compile V=99
      • 编译后可以在对应路径找到编译所需的 config 文件
        • --add-dynamic-module=~/openwrt/build_dir/target-x86_64_musl/nginx-ssl/nginx-1.26.1/nginx-mod-ubus
      • 注意,有些 config 文件路径并非对应库的根目录,如
        • /nginx-mod-naxsi/naxsi_src
        • /nginx-mod-njs/nginx
      创建配置脚本build.sh
      • 需要要其他 Nginx 模块的话自行配置
      • 部分选项说明
        • export CFLAGS="... -DNJS_HAVE_POSIX_MEMALIGN=1 -DNJS_HAVE_MEMALIGN=1"
          • ~/njs/src/njs_malloc.c 没法自行找到 MEMALIGN 所以直接指定 -DNJS_HAVE_POSIX_MEMALIGN=1 -DNJS_HAVE_MEMALIGN=1"
      • vim build.sh
      • bash build.sh
      • sudo make modules
      • sudo make
      问题解决
      gcc 报错 warning: environment variable 'STAGING_DIR' not defined
      • 不影响,可以在 makefile 中添加 export 来回避
      • 最终编译得到的 nginx 二进制文件和动态模块
        • ls -al ~/nginx/objs/nginx
        • ls -al ~/nginx/objs/*.so
      (可选)清理不需要的中间文件并手动打包
      • cd ~/nginx/objs && \ find . -type f \( -name "*.o" -o -name "*.c" \) -exec rm -f {} \; && \ find . -type d -empty -delete
      • mkdir -p ~/nginx/openwrt_install/{bin,modules}
        • mv ~/nginx/objs/nginx ~/nginx/openwrt_install/bin
        • mv ~/nginx/objs/*.so ~/nginx/openwrt_install/modules
      • cd ~/nginx && tar -cvf openwrt_install.tar -C ~/nginx/openwrt_install
      • 上传文件到 openwrt 后进行测试后替换
       
      上一篇
      Zsh 与 zinit 终端包管理
      下一篇
      Openwrt 系统交叉编译

      Comments
      Loading...