nginx对外提供一个ip供调用,使用负载均衡策略转发给后端服务。
需要有日志记录下转发给了哪个后端。
nginx配置
修改/etc/nginx/nginx.conf
文件。TCP代理,将这两行直接添加日志部分,放在stream下。
log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol ' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log logs/proxy.log proxy;
完整配置文件:
root@root:~# cat /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
# 增加begin
stream
{
log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol ' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log logs/proxy.log proxy;
upstream server_upstreams {
server 192.168.1.9:80;
server 192.168.1.15:80;
}
server {
listen 80;
proxy_pass server_upstreams;
}
upstream server_upstreams_1 {
server 192.168.1.9:81;
server 192.168.1.15:81;
}
server {
listen 81;
proxy_pass server_upstreams_1;
}
}
# 增加end
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
由于服务是TCP,不能使用网上其他教程的参考1
server{
add_header backendCode $upstream_status;
add_header BackendIP "$upstream_addr;" always;
}
或者参考2
server{
location {
proxysetheader backendIP $upstreamaddr;
proxysetheader backendCode $upstreamstatus;
}
}
启动nginx
/usr/sbin
路径下执行./nginx -c /etc/nginx/nginx.conf
,启动nginx。
问题1:
启动报错,提示找不到logs/proxy.log文件。
在/usr/share/nginx
路径下创建路径mkdir logs/
。再启动。
问题2:
提示80端口已被占用。
查看占用的进程,可能是之前启动的nginx没有杀死。
测试负载均衡
tail -f proxy.log
查看输出内容。多次访问,观察输出。
192.168.2.8 61087 - [25/Jan/2024:02:06:06 -0500] 200 TCP "192.168.1.9:8181" "1404" "0.000"
192.168.2.8 61444 - [25/Jan/2024:02:15:54 -0500] 200 TCP "192.168.1.15:8181" "2195" "0.000"
192.168.2.10 54053 - [25/Jan/2024:02:27:40 -0500] 200 TCP "192.168.1.9:4334" "199535" "0.192"
192.168.2.8 62295 - [25/Jan/2024:02:32:00 -0500] 200 TCP "192.168.1.9:8181" "2196" "0.144"
192.168.2.10 63152 - [25/Jan/2024:02:32:06 -0500] 200 TCP "192.168.1.9:4334" "206291" "0.008"
192.168.2.10 64738 - [25/Jan/2024:02:41:20 -0500] 200 TCP "192.168.1.15:4334, 192.168.1.9:4334" "0, 969759" "0.000, 0.008"
192.168.2.8 64202 - [25/Jan/2024:03:02:26 -0500] 502 TCP "192.168.1.15:8181, 192.168.1.9:8181" "0, 0" "0.000, 0.000"
参考资料:
nginx添加日志
负载均衡分配服务策略
3. 负载均衡分配服务器策略、nginx主从高可用
4. Nginx 负载均衡
5. nginx对后端服务器健康检查实现自动切换。检测是对后端服务器,不是服务。