nginx ngx_dynamic_upstream 支持通过http 查询和修改upstream

2025/10/05 opensource

nginx-1.28.0下载:

https://nginx.org/en/download.html

ngx_dynamic_upstream 模块下载:

https://github.com/cubicdaiya/ngx_dynamic_upstream/archive/refs/tags/v0.1.6.zip

目录如下

[root@server201 nginx]# ls -lrt
total 1272
drwxr-xr-x 4 root root      138 Jul  2  2016 ngx_dynamic_upstream-0.1.6
-rw-r--r-- 1 root root  1280111 Oct  5 22:35 nginx-1.28.0.tar.gz
-rw-r--r-- 1 root root    16731 Oct  5 23:03 ngx_dynamic_upstream-0.1.6.zip
drwxr-xr-x 9  502 games     257 Oct  5 23:04 nginx-1.28.0

./configure –add-module=../ngx_dynamic_upstream-0.1.6

只编辑即可,不执行 make install,防止直接覆盖原有 nginx 文件

make

执行这个检查

./objs/nginx -V

部署编译好的nginx, which nginx 先查看nginx在哪里 我这里显示的是 /usr/sbin/nginx

cp ./objs/nginx /usr/sbin/nginx

make install 部署nginx

调整配置如下:

# 完整的 nginx.conf 配置
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   /data/package/;
            fancyindex on;
            fancyindex_localtime on;
            fancyindex_exact_size off;
            fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html";
            fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html";
            fancyindex_ignore "examplefile.html";
            fancyindex_ignore "Nginx-Fancyindex-Theme";
            fancyindex_ignore "Nginx-Fancyindex-Theme";
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
        }
    }
    server {
        listen 6000;
        
        location /dynamic {
            dynamic_upstream;
        }
    }
    include upstream/*.conf;
    include vhost/*.conf;
}

添加虚拟主机配置

# 添加虚拟主机配置
cd /usr/local/nginx/conf/vhost
vim test01.xxx.com.conf 
server {
    listen 80;
    server_name test01.xxx.com;
    location / {
        proxy_pass http://http-gateway/;
    }
}
cd /usr/local/nginx/conf/upstream/
vim upstream.conf 
upstream http-gateway {
# 定义了一个共享内存区域,存储负载均衡组(http-gateway)的状态信息
    zone zone_for_http-gateway 1m;
    server 192.168.0.128:8000 max_fails=5 fail_timeout=60;
    server 192.168.0.129:8000 max_fails=5 fail_timeout=60;
}

启动

/usr/sbin/nginx

如何测试:

查看后端服务器节点

curl “http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway”

下线后端服务器节点

curl “http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway&server=192.168.0.129:8000&down=”

上线后端服务器节点

curl “http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway&server=192.168.0.129:8000&up=”

[root@server201 upstream]# curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway"
server 192.168.0.128:8000;
server 192.168.0.129:8000;
[root@server201 upstream]# curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway&server=192.168.0.129:8000&down="
server 192.168.0.128:8000 weight=1 max_fails=5 fail_timeout=60;
server 192.168.0.129:8000 weight=1 max_fails=5 fail_timeout=60 down;
[root@server201 upstream]# curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway"
server 192.168.0.128:8000;
server 192.168.0.129:8000 down;
[root@server201 upstream]# curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway&server=192.168.0.129:8000&up="
server 192.168.0.128:8000 weight=1 max_fails=5 fail_timeout=60;
server 192.168.0.129:8000 weight=1 max_fails=5 fail_timeout=60;
[root@server201 upstream]# curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_http-gateway"
server 192.168.0.128:8000;
server 192.168.0.129:8000;
[root@server201 upstream]# 

关键词: nginx 动态修改配置

Search

    Table of Contents