前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx编译安装nginx-upsync-module模块以实现动态负载

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

原创
作者头像
后端老鸟
修改2020-05-13 14:41:36
1K0
修改2020-05-13 14:41:36
举报
文章被收录于专栏:服务端技术服务端技术

【转载请注明出处】:https://cloud.tencent.com/developer/article/1627571

安装依赖包

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.gznginx-upsync-module模块源码使用git clone https://github.com/weibocom/nginx-upsync-module.git下载。

解压之后进入源码目录执行

代码语言:txt
复制
./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可以看到全部的参数,下面是一些常用配置参数的含义:

代码语言:txt
复制
--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的时候报错

代码语言:txt
复制
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,可以发现代码:

代码语言:txt
复制
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.alibcrypto.a是在openssl源码根目录下的。将此文件修改为:

代码语言:txt
复制
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 ....,这时报错

代码语言:txt
复制
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文件,最终如下

代码语言:txt
复制
/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

再次执行

代码语言:txt
复制
make
make install

如果还报上面的错误,可以尝试手动执行下面的命令之后再执行上面的命令

代码语言:txt
复制
./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

代码语言:txt
复制
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报错。

接下来向注册中心注册服务

代码语言:txt
复制
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,再请求服务发现已经起作用了。

image.png
image.png

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

代码语言:txt
复制
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
image.png
image.png

再上线这个服务

代码语言:txt
复制
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
image.png
image.png

测试已经没有问题。

【转载请注明出处】:https://cloud.tencent.com/developer/article/1627571

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装依赖包
  • 配置
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档