【轉載請註明出處】:https://developer.aliyun.com/article/759950
安裝依賴包
OpenSSL
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.11
make
make 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 i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[4]: *** [link_app.] Error 1
make[3]: *** [openssl] Error 2
make[2]: *** [build_apps] Error 1
make[1]: *** [/user/local/openssl-1.0.2q/.openssl/include/openssl/ssl.h] Error 2
make: *** [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_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [objs/nginx] Error 1
make: *** [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
再次執行
make
make install
如果還報上面的錯誤,可以嘗試手動執行下面的命令之後再執行上面的命令
./Configure darwin64-x86_64-cc --prefix=/work/tools/openssl-1.0.2q/.openssl no-shared no-threads
sudo make
sudo 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:8002
curl -s http://127.0.0.1:8500/v1/kv/upstreams/testupsync?recurse
接下來啟動nginx,再請求服務發現已經起作用了。
再下掉這個服務看看是否生效
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
測試已經沒有問題。
【轉載請註明出處】:https://developer.aliyun.com/article/759950