CentOS7中安装nginx与upsync

方法1:OpenResty+Upsync

下载Upsync

1
2
3
$ cd /usr/local
$ wget https://github.com/weibocom/nginx-upsync-module/archive/v2.0.0.tar.gz
$ tar zxvf v2.0.0.tar.gz

安装依赖

1
$ yum install -y pcre-devel openssl-devel gcc curl

下载OpenResty

1
2
3
4
$ cd /usr/local
$ wget https://openresty.org/download/openresty-1.11.2.2.tar.gz
$ tar zxvf openresty-1.11.2.2.tar.gz
$ mv openresty-1.11.2.2 openresty

编译安装

1
2
3
$ cd openresty
$ ./configure --prefix=/usr/local/openresty --add-dynamic-module=/usr/local/nginx-upsync-module-2.0.0
$ gmake && sudo gmake install

然后在 /usr/local/openresty/nginx/modules 目录下会出现 ngx_http_upsync_module.so 模块文件,然后在 /usr/local/openresty/nginx/conf/nginx.conf 中导入

1
2
3
4
load_module modules/ngx_http_upsync_module.so;
events {
worker_connections 1024;
}

配置环境变量

1
$ vi /etc/profile

添加如下内容:

1
export PATH=/usr/local/openresty/nginx/sbin:$PATH

1
$ source /etc/profile

启动nginx

1
$ nginx

方法2:Nginx+Upsync

安装依赖

1
2
3
$ yum -y install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential pcre-devel openssl openssl-devel
$ yum -y install git
$ yum install -y patch

编译安装Nginx

1
2
3
4
5
6
7
8
9
$ cd /usr/local/
$ git clone https://github.com/weibocom/nginx-upsync-module
$ git clone https://github.com/xiaokai-wang/nginx_upstream_check_module
$ wget http://nginx.org/download/nginx-1.12.1.tar.gz
$ tar zxvf nginx-1.12.1.tar.gz
$ cd nginx-1.12.1
$ patch -p1 < /usr/local/nginx_upstream_check_module/check_1.12.1+.patch
$ ./configure --add-module=../nginx_upstream_check_module --add-module=../nginx-upsync-module
$ make && make install

安装Consul

1
2
3
4
$ cd /usr/local
$ wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip
$ unzip consul_1.1.0_linux_amd64.zip
$ nohup consul agent -advertise=192.168.0.11 -client=0.0.0.0 -dev &

配置Nginx.conf

1
2
$ mkdir -p /usr/local/nginx/conf/servers
$ vi /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/conf/nginx.conf内容如下:

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
#user  nobody;
worker_processes 5;

events {
worker_connections 4096;
}

http {
upstream test {
# fake server otherwise ngx_http_upstream will report error when startup
server 127.0.0.1:11111;

# all backend server will pull from consul when startup and will delete fake server
upsync 192.168.0.11:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}

upstream bar {
server 127.0.0.1:8090 weight=1 fail_timeout=10 max_fails=3;
}

server {
listen 8080;

location = /proxy_servers {
proxy_pass http://test;
}

location = /bar {
proxy_pass http://bar;
}

location = /upstream_show {
upstream_show;
}

}
}

启动Nginx

1
/usr/local/nginx/sbin/nginx

管理节点

测试节点

1
2
$ curl curl http://192.168.0.11:8080
$ curl http://192.168.0.11:8080/upstream_show

查看节点信息

1
$ curl http://192.168.0.11:8080/upstream_show?test

Consul添加新节点

1
2
3
$ curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://192.168.0.11:8500/v1/kv/upstreams/test/192.168.0.12:8001
$ curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://192.168.0.11:8500/v1/kv/upstreams/test/192.168.0.12:8002
$ curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://192.168.0.11:8500/v1/kv/upstreams/test/192.168.0.12:8003

下线节点

1
$ curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10, "down":1}' http://192.168.0.11:8500/v1/kv/upstreams/test/192.168.0.12:8003

上线节点

1
$ curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}' http://192.168.0.11:8500/v1/kv/upstreams/test/192.168.0.12:8003
坚持原创技术分享,您的支持将鼓励我继续创作!