nginx主配置文件添加配置:
stream {log_format proxy '$remote_addr [$time_local] ''$protocol status:$status bytes_sent:$bytes_sent bytes_received:$bytes_received ''$session_time upstream_addr:"$upstream_addr" ''"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';include /etc/nginx/tcp_conf.d/*.conf;
}
虚拟主机的配置
server {listen 39000;proxy_pass test_backend_for_health;proxy_connect_timeout 10s;proxy_timeout 10s;access_log /var/log/nginx/test.log proxy;open_log_file_cache off;
}upstream test_backend_for_health {server 192.168.2.82:10001 max_fails=2 fail_timeout=30s weight=2;server 192.168.2.82:10002 max_fails=2 fail_timeout=30s weight=2;server 192.168.2.82:10003 max_fails=2 fail_timeout=30s weight=2;
}
应用程序的模仿
python3脚本
# coding: utf8
import socket
import sys
port=int(sys.argv[1])def simple_tcp_server(host='192.168.2.82', port=port, reply_message="Hello from server!"):# 创建一个socket对象with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:s.bind((host, port))print(f'Server started on port {port}...')s.listen(1)while True:conn, addr = s.accept()with conn:print(f'Connected by {addr}')while True:data = conn.recv(1024)if not data:breaktry:print("Received:", data.decode())except Exception as e:print(e)conn.sendall(reply_message.encode())if __name__ == "__main__":simple_tcp_server()
脚本编写好之后,打开3个终端,运行3次python脚本,参数分别为10001/10002/10003。
启动nginx之后,开始模拟做一次tcp请求
telnet localhost 39000
可以看到日志如下:
关闭其中一个python脚本,比如10003这个,多请求几次会发现,nginx的日志变成如下的样子:
也就是说nginx会先转发一次请求给后端10003,在发现10003失败之后,立即转发给了剩余两台后端应用中的一个,并且把这次请求的过程也给记录了下来,也就是$upstream_bytes_received这个参数打印的内容。