Windows编译Nginx源码
上一篇講到nginx 1.7.11.3 Gryphon版本無法支持http2,于是心生一計:自己編譯源碼構建一個自己的版本。
1、需要工具
perl:ActivePerl_v5.28.1.0000.exe
msys2:msys2-x86_64-20180531.exe
nginx源碼:nginx-861f076eab72.zip
rtmp插件:nginx-rtmp-module-1.2.2.tar.gz
openssl:openssl-1.0.2u.tar.gz
pcre:pcre-8.42.tar.gz
zlib:zlib-1.2.11.tar.gz
VisualStudio2015:vs2015.2.com_chs-VS2015.iso(選擇功能的時候記得多勾選VisualC++移動開發選項(為了使用nmake命令工具:VS2015 x64 本機工具命令提示符),其他都默認即可)
Win64OpenSSL-3_0_3.exe【可選】該程序在這里只用作配合生成SSL證書。
一鍵下載: https://pan.baidu.com/s/1JwPbpiDv3FwofPP9HD72VQ 提取碼: gw5h 也可以根據文件名字去搜官網但是里面有很多坑。
2、VusualStudio2015安裝
注意選中Visucal C++移動開發,這樣才會在win菜單中多出nmake工具[ VS2015 x64 本機工具命令提示符 ]
3、msys2安裝
直接下一步下一步即可,安裝完成記得配置環境變量
?4、Nginx源碼解壓
Nginx源碼解壓到msys2文件目錄的home目錄中,并在nginx文件夾中新建文件夾build/lib
里面存放nginx依賴的其他模塊源碼(解壓后的):nginx-rtmp-module-1.2.2、openssl-1.0.2u、pcre-8.42、zlib-1.2.11
目錄結構如下圖:
4.1、修改32位nginx源碼為64位(可選)
編輯nginx\auto\lib\openssl\makefile.msvc文件
“VC-WIN32”替換為“VC-WIN64A”
“if exist ms\do_ms.bat”替換為“if exist ms\do_win64a.bat”
“ms\do_ms”替換為“ms\do_win64a”
4.2、使用msys2工具編譯
msys2工具不懂的教你一招:直接cd /?? 然后ls命令看看熟不熟悉,是不是和linux系統長得差不多
進入到/home目錄查看文件ls,可以看到剛解壓放進去的nginx文件,cd nginx
再然后執行命令:
auto/configure --with-cc=cl --builddir=objs \ --with-debug --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 \ --http-scgi-temp-path=temp/scgi_temp \ --http-uwsgi-temp-path=temp/uwsgi_temp \ --with-cc-opt=-DFD_SETSIZE=1024 \ --with-select_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_stub_status_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_auth_request_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-mail \ --with-stream \ --with-openssl-opt=no-asm \ --with-http_ssl_module \ --with-mail_ssl_module \ --with-stream_ssl_module \ --with-openssl=./build/lib/openssl-1.0.2u \ --with-pcre=./build/lib/pcre-8.42 \ --with-zlib=./build/lib/zlib-1.2.11 \ --add-module=./build/lib/nginx-rtmp-module-1.2.2/4.3、使用nmake工具生成可執行文件
因為引入了nginx-rtmp-module模塊,nmake生成exe過程會報錯,各種上網查,最后發現需要修改objs\Makefile文件
報錯信息:
?修改生成的文件nginx\objs\Makefile,找到第3行:CFLAGS =......??? 去掉 -WX? 即可,去掉后需要重新執行nmake命令編譯。
?使用nmake工具生成nginx.exe文件(nmake工具在win菜單里面找到VisualStudio文件夾下的VS2015 x64 本機工具命令提示符)cd到nginx所在目錄后執行:nmake -f objs/Makefile
最后可能會報一些不影響大雅的錯誤,反正nginx.exe生成了就能用了,也沒仔細去研究怎么優化,有時間再搞。
本篇文章目的已達到,包含多個功能模塊的nginx.exe生成。
=============================分隔=================================
5、Nginx配置SSL。
5.1、生成SSL證書
安裝Win64OpenSSL-3_0_3.exe工具,配置環境變量
驗證:cmd中執行openssl help命令即可
使用CMD命令行生成證書步驟
客戶端:(生成的CA-certificate.crt需要導入到證書管理里面的受信任的根證書頒發機構)
1、生成ca證書【CA-certificate.crt】和ca私鑰【CA-private.key】
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -subj "/C=CN/ST=MyProvince/L=MyCity/O=BearOS" -out CA-certificate.crt -keyout CA-private.key -reqexts v3_req -extensions v3_ca
服務端:(生成的private.key和private.crt需要放到nginx里面)
2、生成私鑰【private.key】
openssl genrsa -out private.key 2048
3、生成證書請求文件【private.csr】
(-subj是附加企業信息:BeiJing、Beijing、企業名BearOS、請求頒發的目標網址kuaishou.bearos.com)
openssl req -new -key private.key -subj "/C=CN/ST=BeiJing/L=BeiJing/O=BearOS/CN=a.b.com" -sha256 -out private.csr
4、新建文本文件private.ext
?? 文件內容:
[ req ] default_bits = 1024 distinguished_name = req_distinguished_name req_extensions = san extensions = san [ req_distinguished_name ] countryName = CN stateOrProvinceName = Definesys localityName = Definesys organizationName = Definesys [SAN] authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment #subjectAltName = IP:127.0.0.1 subjectAltName = @alt_names [ alt_names ] IP.1 = 127.0.0.1 DNS.1 = a.b.com DNS.2 = localhost DNS.3 = 127.0.0.1?5、生成自簽名證書【private.crt】
根據根證書私鑰【CA-private.key】,根證書【CA-certificate.crt】,自簽名證書申請文件【private.csr】,自簽名證書擴展文件【private.ext】,生成自簽名證書【private.crt】
(參數 -days是有效天數,經網上查得知最多24年左右)
openssl x509 -req -days 3650 -in private.csr -CA CA-certificate.crt -CAkey CA-private.key -CAcreateserial -sha256 -out private.crt -extfile private.ext -extensions SAN
最后生成兩個可以用在nginx里面的文件:server.crt、server.key?
6、將CA-certificate.crt證書導入到電腦上受信任的根證書頒發機構里面。
可以從瀏覽器進入證書管理窗口,也可以win+R輸入certmgr.msc進入。
將private.cat、private.key兩個文件放到nginx/conf/ssl目錄下,并配置nginx.conf
5.2、nginx.conf配置
server{#https監聽443端口listen 443 ssl http2;#對應的域名server_name xxx.xxx.xxx.xxx;#SSL-START SSL相關配置#HTTP_TO_HTTPS_START#強制使用httpsif ($server_port !~ 443){rewrite ^(/.*)$ https://$host$1 permanent;}#HTTP_TO_HTTPS_END#注釋掉ssl on; 是為了讓此server同時處理http和https#ssl on;#獲取到的第一個文件的全路徑ssl_certificate ssl/private.crt;#獲取到的第二個文件的全路徑ssl_certificate_key ssl/private.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;#SSL-ENDlocation / {alias D:\\Apps\\nginx-server\\kuaishou-manager-web\\;index index.html index.htm;}#hls直播地址location /hls {types {application/vnd.apple.mpegurl m3u8; #或 application/x-mpegURLvideo/mp2t ts;}alias D:/videos/vod/; #點播視頻文件(.ts;.m3u8)存放位置expires -1;add_header Cache-Control no-cache; #跨域支持,不然網頁播放不了add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";}}5.3、最終結果
總結
以上是生活随笔為你收集整理的Windows编译Nginx源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sun java认证考试大纲_SUN认证
- 下一篇: 平昌县网上书城系统设计与实现