0%

Nginx动态管理upstream

Nginx动态管理upstream


编译 nginx + ngx_dynamic_upstream

https://github.com/cubicdaiya/ngx_dynamic_upstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cd ~
curl -fSL https://github.com/cubicdaiya/ngx_dynamic_upstream/archive/refs/tags/v0.1.6.tar.gz -o ngx_dynamic_upstream.tar.gz
curl -fSL http://nginx.org/download/nginx-1.22.0.tar.gz -o nginx.tar.gz
tar zxf nginx.tar.gz
tar zxf ngx_dynamic_upstream.tar.gz

yum install -y pcre2-dev openssl-devel
cd nginx-1.22.0
./configure \
--prefix=/data/nginx \
--user=apache \
--group=apache \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-threads \
--with-stream \
--with-stream_realip_module \
--with-compat \
--with-file-aio \
--with-http_v2_module \
--add-module=/root/ngx_dynamic_upstream-0.1.6


./configure \
--prefix=/data/nginx \
--user=apache \
--group=apache \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-threads \
--with-stream \
--with-stream_realip_module \
--with-compat \
--with-file-aio \
--with-openssl=/usr \
--with-http_v2_module \
--add-module=/root/ngx_dynamic_upstream-0.1.6
错误
1
2
3
4
5
6
7
8
9
10
11
12
[root@jre5167 nginx-1.22.0]# make
make -f objs/Makefile
make[1]: 进入目录“/root/nginx-1.22.0”
cd /usr/lib64 \
&& if [ -f Makefile ]; then make clean; fi \
&& ./config --prefix=/usr/lib64/.openssl no-shared no-threads \
&& make \
&& make install_sw LIBDIR=lib
/bin/sh:行2: ./config: 没有那个文件或目录
make[1]: *** [/usr/lib64/.openssl/include/openssl/ssl.h] 错误 127
make[1]: 离开目录“/root/nginx-1.22.0”
make: *** [build] 错误 2

打开nginx源文件下的 /root/nginx-1.22.0auto/lib/openssl/conf 文件:

找到这么一段代码:

1
2
3
4
5
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"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

修改成以下代码:

1
2
3
4
5
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

然后再进行Nginx的编译安装即可

openssl 源码编译安装

nginx 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
load_module /usr/lib/nginx/modules/ngx_dynamic_upstream_module.so;

events {
worker_connections 1024;
}

http {
upstream news-service {
zone zone_for_backends 1m;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 8082;
return 200 "hi! I'm 8082\n";
}
server {
listen 8081;
return 200 "hi! I'm 8081\n";
}
server {
listen 80;
server_name localhost;
location /dynamic {
allow 127.0.0.1;
deny all;
dynamic_upstream;
}
location / {
proxy_pass http://news-service;
}
}
}
1
2
3
4
5
6
7
8
9
10
curl 127.0.0.1
curl 127.0.0.1/dynamic?upstream=zone_for_backends
curl "127.0.0.1/dynamic?upstream=zone_for_backends&verbose="

curl "http://127.0.0.1/dynamic?upstream=zone_for_backends&server=127.0.0.1:8081&down="
curl "http://127.0.0.1/dynamic?upstream=zone_for_backends&server=127.0.0.1:8081&up="


curl "http://127.0.0.1/dynamic?upstream=backend-service&server=:8080&down="
curl "127.0.0.1/dynamic?upstream=backend-service&verbose="
1
2
3
4
5
curl "http://127.0.0.1:8080/dynamic?upstream=news-service&server=news-service-1&down="

# nginx 日志
172.31.20.1 - - [02/Sep/2022:14:00:16 +0800] "GET /dynamic?upstream=news-service&server=news-service-1&down= HTTP/1.1" 400 157 "-" "curl/7.29.0" "-"
2022/09/02 14:00:16 [error] 9#9: *3 server news-service-1 is not found. ngx_dynamic_upstream_op_update_param:428, client: 172.31.20.1, server: localhost, request: "GET /dynamic?upstream=news-service&server=news-service-1&down= HTTP/1.1", host: "127.0.0.1:8080"
1
2
3
4
5
6
7
8
9
10
11
docker inspect -f '{{(index .NetworkSettings.Networks "news_default").IPAddress}}' news-service-1
172.31.20.6
docker inspect -f '{{(index .NetworkSettings.Networks "news_default").IPAddress}}' news-service-2
172.31.20.5

# upstream服务状态
curl "127.0.0.1:8080/dynamic?upstream=news-service&verbose="

# news-service-1
curl "127.0.0.1:8080/dynamic?upstream=news-service&server=172.31.20.5:80&up="
curl "127.0.0.1:8080/dynamic?upstream=news-service&server=172.31.20.5:80&down="

nginx 的docker hub 官方维护仓库

https://github.com/nginxinc/docker-nginx