安裝依賴包
OpenSSL
在官網下載下傳頁下到最新穩定版1.0.2q。
PCRE
在 PCRE 官網可以找到下載下傳位址,這裡選擇8.x的最高版本 pcre-8.42.tar.gz。
zlib
zlib 直接選擇官網首頁最新的zlib-1.2.11.tar.gz。
下載下傳nginx 源碼包及nginx-upsync-module子產品源碼
這裡下載下傳的是nginx穩定版nginx-1.14.2.tar.gz,nginx-upsync-module子產品源碼使用git clone https://github.com/weibocom/nginx-upsync-module.git下載下傳。解壓之後進入源碼目錄執行
./configure --sbin-path=/usr/local/opt/nginx --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/opt/nginx/nginx.pid --prefix=/usr/local/opt/nginx --with-http_ssl_module --add-module=/work/tools/nginx-modules/nginx-upsync-module --with-openssl=/work/tools/openssl-1.0.2q --with-pcre=/work/tools/pcre-8.42 --with-zlib=/work/tools/zlib-1.2.11makemake install
檢視檔案auto/options可以看到全部的參數,下面是一些常用配置參數的含義:
--prefix #nginx安裝目錄,預設在/usr/local/nginx--pid-path #pid問件位置,預設在logs目錄--lock-path #lock問件位置,預設在logs目錄--with-http_ssl_module #開啟HTTP SSL子產品,以支援HTTPS請求。--with-http_dav_module #開啟WebDAV擴充動作子產品,可為檔案和目錄指定權限--with-http_flv_module #支援對FLV檔案的拖動播放--with-http_realip_module #支援顯示真實來源IP位址--with-http_gzip_static_module #預壓縮檔案傳前檢查,防止檔案被重複壓縮--with-http_stub_status_module #取得一些nginx的運作狀态--with-mail #允許POP3/IMAP4/SMTP代理子產品--with-mail_ssl_module #允許POP3/IMAP/SMTP可以使用SSL/TLS--with-pcre=../pcre-8.11 #注意是未安裝的pcre路徑--with-zlib=../zlib-1.2.5 #注意是未安裝的zlib路徑--with-debug #允許調試日志--http-client-body-temp-path #用戶端請求臨時檔案路徑--http-proxy-temp-path #設定http proxy臨時檔案路徑--http-fastcgi-temp-path #設定http fastcgi臨時檔案路徑--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #設定uwsgi 臨時檔案路徑--http-scgi-temp-path=/var/tmp/nginx/scgi #設定scgi 臨時檔案路徑:
在make的時候報錯
ld: symbol(s) not found for architecture i386clang: error: linker command failed with exit code 1 (use -v to see invocation)make[4]: *** [link_app.] Error 1make[3]: *** [openssl] Error 2make[2]: *** [build_apps] Error 1make[1]: *** [/user/local/openssl-1.0.2q/.openssl/include/openssl/ssl.h] Error 2make: *** [build] Error 2
這個是因為我先前裝了别的版本的openssl導緻的,檢視Nginx源碼目錄檔案auto/lib/openssl/conf,可以發現代碼:
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
實際的openssl源碼目錄是沒有.openssl目錄的,ssl.h檔案是在openssl源碼目錄的include/openssl/目錄下的,libssl.a 和libcrypto.a是在openssl源碼根目錄下的。将此檔案修改為:
CORE_INCS="$CORE_INCS $OPENSSL/include"CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"CORE_LIBS="$CORE_LIBS $OPENSSL/libssl.a"CORE_LIBS="$CORE_LIBS $OPENSSL/libcrypto.a"
執行make clean 之後重新執行上面的./configure ....,這時報錯
ld: symbol(s) not found for architecture x86_64clang: error: linker command failed with exit code 1 (use -v to see invocation)make[1]: *** [objs/nginx] Error 1make: *** [build] Error 2
查了一下,看到好多人的解決方式都是修改objs/Makefile檔案,找到編譯openssl的地方,将./config --prefix= 改成./Configure darwin64-x86_64-cc --prefix=,改完之後千萬不要執行./configure ....,否則會重新生成objs/Makefile檔案,最終如下
/work/tools/openssl-1.0.2q/.openssl/include/openssl/ssl.h: objs/Makefile cd /work/tools/openssl-1.0.2q && if [ -f Makefile ]; then $(MAKE) clean; fi && ./Configure darwin64-x86_64-cc --prefix=/work/tools/openssl-1.0.2q/.openssl no-shared no-threads && $(MAKE) && $(MAKE) install_sw LIBDIR=lib
再次執行
makemake install
如果還報上面的錯誤,可以嘗試手動執行下面的指令之後再執行上面的指令
./Configure darwin64-x86_64-cc --prefix=/work/tools/openssl-1.0.2q/.openssl no-shared no-threads sudo makesudo make install
有時候報類似symbol(s) not found 有可能是權限不夠導緻的,可以嘗試加sudo執行指令。這時啟動nginx已經可以啟動了。
配置
本文以Consul作為注冊中心,關于Consul的知識将不再介紹。進入配置檔案目錄建立一個目錄servers以放将來添加的配置檔案,修改配置檔案nginx.conf添加include servers/*.conf; ,進入servers建立一個空檔案upsync-test-tmp.conf作為upsync的緩存檔案,再建立配置檔案 test-upsync.conf
upstream testupsync { upsync 127.0.0.1:8500/v1/kv/upstreams/testupsync/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off; upsync_dump_path /usr/local/etc/nginx2/servers/upsync-test-tmp.conf; include /usr/local/etc/nginx2/servers/upsync-test-tmp.conf; server 127.0.0.1:11111 down ;}server { listen 8000; server_name localhost; location / { proxy_pass http://testupsync; } location = /upstream_show { upstream_show; }}
server 127.0.0.1:11111 down ;是為了占位,防止啟動nginx報錯。接下來向注冊中心注冊服務
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10 }' http://127.0.0.1:8500/v1/kvtreams/testupsync/127.0.0.1:8002curl -s http://127.0.0.1:8500/v1/kv/upstreams/testupsync?recurse
接下來啟動nginx,再請求服務發現已經起作用了。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iZ3U2M0MGO1YjNjZ2NiVTYiZWOzADO4MmZiJGNlBjYi9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
再下掉這個服務看看是否生效
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10,"down":1}' http://127.0.0.1:8500/v1/kvtreams/testupsync/127.0.0.1:8002
再上線這個服務
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10,"down":0}' http://127.0.0.1:8500/v1/kvtreams/testupsync/127.0.0.1:8002
測試已經沒有問題。