天天看点

nginx编译安装_Nginx编译安装nginx-upsync-module模块以实现动态负载

安装依赖包

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,再请求服务发现已经起作用了。

nginx编译安装_Nginx编译安装nginx-upsync-module模块以实现动态负载

再下掉这个服务看看是否生效

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
           
nginx编译安装_Nginx编译安装nginx-upsync-module模块以实现动态负载

再上线这个服务

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
           
nginx编译安装_Nginx编译安装nginx-upsync-module模块以实现动态负载

测试已经没有问题。

nginx编译安装_Nginx编译安装nginx-upsync-module模块以实现动态负载

继续阅读