- 前端服务器:192.168.2.10
- 后端服务器1:192.168.2.20
- 后端服务器2:192.168.2.30
[root@bogon nginx]# vim /usr/local/ngin/conf/nginx.conf
# 在http指令块下配置upstream指令块
upstream web {
server 192.168.2.20;
server 192.168.2.30;
}
# 在location指令块配置proxy_pass
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
proxy_next_upstream error http_404 http_502;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
- proxy_next_upstream error http_404 http_502;
通过这个指令,可以处理当后端服务返回404等报错时,直接将请求转发给其他服务器,而不是把报错信息返回客户端。
- proxy_set_header Host $host;
通过这个指令,把客户端请求的host,转发给后端。
- proxy_set_header X-Real-IP $remote_addr
通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
在后端服务器192.168.2.20配置如下(配置文件不需要修改即可)
vim /usr/local/nginx/html/index.html
this is 2.20 page
在后端服务器192.168.2.30配置如下(配置文件不需要修改即可)
vim /usr/local/nginx/html/index.html
this is 2.30 page
upstream web {
server 192.168.2.20;
server 192.168.2.30;
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
}
[root@localhost ~]# while true;do curl 192.168.2.10;sleep 2;done
this is 2.20 page
this is 2.30 page
this is 2.20 page
this is 2.30 page
this is 2.20 page
this is 2.30 page
#可以看到后端服务器,非常平均的处理请求。
upstream web {
server 192.168.2.20 weight=3;
server 192.168.2.30 weight=1;
}
默认是weight=1
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# while true;do curl 192.168.2.10;sleep 2;done
this is 2.20 page
this is 2.20 page
this is 2.20 page
this is 2.30 page
this is 2.20 page
this is 2.20 page
this is 2.20 page
this is 2.30 page
#后端服务,根据权重比例处理请求,适用于服务器性能不均的环境。
vim /usr/local/nginx/conf/nginx.conf
upstream web {
server 192.168.2.20 weight=1 max_fails=3 fail_timeout=9s;
#先将2.20这台nginx关了。
server 192.168.2.30 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
#proxy_next_upstream error http_404 http_502;
proxy_next_upstream off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
# 在这里,我们将超时时间设置为9s,最多尝试3次,
这里要注意,尝试3次,依然遵循轮询的规则,并不是一个请求,连接3次,
而是轮询三次,有3次处理请求的机会
[root@localhost ~]# while true;do curl -I 192.168.2.10 2>/dev/null|grep HTTP/1.1 ;sleep 3;done
HTTP/1.1 502 Bad Gateway
HTTP/1.1 200 OK
HTTP/1.1 502 Bad Gateway
HTTP/1.1 200 OK
HTTP/1.1 502 Bad Gateway
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 502 Bad Gateway
HTTP/1.1 200 OK
我们设置的超时时间为9s,我们是每3s请求一次。
我们可以看到后端一台服务器挂了后,请求没有直接转发给正常的服务器,
而是直接返回了502。尝试三次后,等待9s,才开始再次尝试(最后一个502)。
要注意,第二行的200响应,并不是客户端第一次的请求的响应码,而是客户端第二次新的请求。
vim /usr/local/nginx/conf/nginx.conf
upstream web {
server 192.168.2.20 weight=1 max_fails=3 fail_timeout=9s;
#先将2.20这台nginx关了。
server 192.168.2.30 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
proxy_next_upstream error http_404 http_502;
#proxy_next_upstream off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# while true;do curl -I 192.168.2.10 2>/dev/null|grep HTTP/1.1 ;sleep 3;done
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
可以看到现在没有502报错,请求都处理了。因为错误的响应码被proxy_next_upstream 获取,这次请求
被转发给下一个正常的服务器了。
所以看到都是200,但是你应该清楚,哪个200是响应的上个服务器没有处理请求,哪个200是正常的响应。
vim /usr/local/nginx/conf/nginx.conf
upstream web {
ip_hash;
server 192.168.2.20 weight=1 max_fails=3 fail_timeout=9s;
server 192.168.2.30 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
proxy_next_upstream error http_404 http_502;
#proxy_next_upstream off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# while true;do curl 192.168.2.10;sleep 2;done
this is 2.30 page
this is 2.30 page
this is 2.30 page
this is 2.30 page
this is 2.30 page
this is 2.30 page
[root@localhost ~]# curl 192.168.2.20
this is 2.20 page
#可以看到2.20的服务是正常的,但是却不转发给2.20了,请求固定在了2.30的服务器上。
vim /usr/local/nginx/conf/nginx.conf
upstream web {
hash $request_uri consistent;
server 192.168.2.20;
server 192.168.2.30;
}
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
# 下载模块:
wget https://github.com/gnosek/nginx-upstream-fair/archive/master.zip
# 解压:
unzip master.zip
# 修改源码bug:
sed -i 's/default_port/no_port/g' ngx_http_upstream_fair_module.c
# 预编译:
./configure --prefix=/usr/local/nginx --add-module=../echo-nginx-module --withhttp_stub_status_module --add-module=../nginx-upstream-fair-master
# 编译/安装:
make && make install
# 配置:
upstream web {
fair;
server 192.168.2.20 weight=1 max_fails=3 fail_timeout=9s;
server 192.168.2.30 weight=1;
}
upstream web {
server 192.168.2.20 weight=1 max_fails=3 fail_timeout=9s;
server 192.168.2.30 weight=1 backup;
}
# 2.30的服务器做备用服务器,只有在2.30得服务器不能提供服务时,才会自动顶上,否则,默认是不提供服务的。
- 前端服务器:192.168.2.10
- 后端服务器1:192.168.2.20
- 后端服务器2:192.168.2.30
#预编译:
./configure --prefix=/usr/local/nginx --add-module=../echo-nginx-module --withhttp_stub_status_module --with-stream
# 编译/安装:
make && make install
make upgrade
# 在main全局配置stream:
events {
worker_connections 1024;
}
stream {
upstream web {
# 必须要指定ip加port
server 192.168.2.20:80;
server 192.168.2.30:80;
}
server {
listen 80;
# 连接上游服务器超时间,超过则选择另外一个服务器
proxy_connect_timeout 3s;
# tcp连接闲置时间,超过则关闭
proxy_timeout 10s;
proxy_pass web;
}
log_format proxy '$remote_addr $remote_port $protocol $status
[$time_iso8601] '
'"$upstream_addr" "$upstream_bytes_sent"
"$upstream_connect_time"' ;
access_log /usr/local/nginx/logs/proxy.log proxy;
}
在后端服务器192.168.2.20配置如下(配置文件不需要修改即可)
vim /usr/local/nginx/html/index.html
this is 2.20 page
在后端服务器192.168.2.30配置如下(配置文件不需要修改即可)
vim /usr/local/nginx/html/index.html
this is 2.30 page
- 在后端2.30上访问前端IP
[root@localhost ~]# curl 192.168.2.10
this is 2.20 page
- 查看后端2.20日志
[root@bogon nginx]# tail -1 /usr/local/nginx/logs/host.access.log
192.168.2.10 - - [26/Apr/2020:03:16:37 +0800] "GET / HTTP/1.1" 200 18 "-" "curl/7.29.0" "-"
-查看前端2.10日志
[root@bogon nginx]# tail -1 /usr/local/nginx/logs/proxy.log
192.168.2.30 45420 TCP 200 [2020-04-25T19:23:35+08:00] "192.168.2.20:80" "76""0.001"
#在前端2.10配置:
[root@bogon nginx]# vim conf/nginx.conf
events {
worker_connections 1024;
}
stream {
upstream web {
server 192.168.2.20:22;
# server 192.168.2.30:80;
}
server {
listen 6666;
proxy_connect_timeout 3s;
proxy_timeout 10s;
proxy_pass web;
}
log_format proxy '$remote_addr $remote_port $protocol $status [$time_iso8601] ' '"$upstream_addr" "$upstream_bytes_sent""$upstream_connect_time"' ;
access_log /usr/local/nginx/logs/proxy.log proxy;
}
[root@bogon nginx]# /usr/local/nginx/sbin/nginx -s reload
#在另外一台服务器访问:
[root@localhost ~]# ssh 192.168.2.10 -p 6666
[email protected]'s password:
Last login: Sun Apr 26 03:31:45 2020 from www.ys.com
#可以看到已经登上2.20服务器上了
[root@bogon ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:2c:72:99 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.20/24 brd 192.168.2.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::7e7a:cabd:3b11:a545/64 scope link noprefixroute
valid_lft forever preferred_lft forever