windows下编译nginx-with-http-concat

作者 liang 日期 2016-07-02
windows下编译nginx-with-http-concat

最近因为工作需要,要在windows下使用nginx,nginx还要使用http-concat模块,但是windows下的nginx默认没有这个插件,不得已得自己编译一个nginx,以下是折腾的过程,及踩过的一些坑:

环境

  • windows 7 x64
  • Visual Studio 2008(记得勾选C++,别的版本也可以)
  • MSYS
  • Perl ,ActivePerl 或 Strawberry Perl
  • Mercurial (版本控制工具)
  • PCRE(8.39), zlib(1.2.8) and OpenSSL(1.0.1s) 的源代码
  • nginx-http-concat ( https://github.com/alibaba/nginx-http-concat
  • nginx 源码版本是 1.11.2 ,在 nginx\src\core\nginx.h 可以看到

步骤

  • 确保环境变量中PATH中配置了 Perl、Mercurial、MSYS 的bin目录
  • 用编辑器打开 msys\1.0\msys.bat,在最顶层添加

    1
    2
    3
    4
    set path=%path%;"D:\Program Files\Microsoft Visual Studio 9.0\VC\bin"
    set path=%path%;"D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE"
    set path=%path%;"D:\Program Files\Microsoft Visual Studio 9.0\VC\include"
    call "D:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"

    VS路径注意做下修改,这里这么做的原因是为了方便后面不用手动在msys里执行vcvars32.bat

  • 获取nginx源代码

    1
    hg clone http://hg.nginx.org/nginx
  • 进入到nginx源代码目录 ,执行

    1
    2
    3
    mkdir objs
    mkdir objs/lib
    cd objs/lib
  • 解压 nginx-http-concat、zlib、PCRE、OpenSSL源代码到 objs/lib 下面,文件夹名分别为 http-concat,zlib,pcre,openssl;
    注意如果你的 openssl 比1.0.1s新要做一些改动,定位到 nginx 目录,进入到
    auto\lib\openssl\makefile.msvc,将no-shared改为no-asm,删除ms\do_ms这一行,可是我这么做一直没有成功,从最新版本一直降到1.0.1s才成功,这个版本而且不用做改动

  • 到 nginx源代码根目录,运行 msys.bat ,执行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    cd /f/opensource/nginx  
    auto/configure --with-cc=cl --builddir=objs --prefix= \
    --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid \
    --http-log-path=logs/access.log --error-log-path=logs/error.log \
    --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp \
    --http-proxy-temp-path=temp/proxy_temp \
    --http-fastcgi-temp-path=temp/fastcgi_temp \
    --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs/lib/pcre \
    --with-zlib=objs/lib/zlib --with-openssl=objs/lib/openssl \
    --with-select_module --with-http_ssl_module --with-ipv6 \
    --add-module=objs/lib/http-concat

    配置脚本可以合并成一行,用下面的更方便,复制,在 msys 命令行里,用 shift + insert 粘贴。

1
2
cd /f/opensource/nginx
auto/configure --with-cc=cl --builddir=objs --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs/lib/pcre --with-zlib=objs/lib/zlib --with-openssl=objs/lib/openssl --with-select_module --with-http_ssl_module --with-ipv6 --add-module=objs/lib/http-concat
  • 执行 make

    1
    nmake -f objs/Makefile
  • 等完成后,在 nginx\objs 找到 nginx.exe ,下载编译好的 nginx

其它问题

按taobao-http-concat官方配置在nginx 里配置 http-concat ,可能会报 400 错,注意要还要配置concat_types,如下配置:

1
2
3
4
5
6
7
location / {
charset gbk;
concat on;
concat_max_files 20;
concat_types application/javascript;
root E:/r-resource-branch0511;
}