nginx实现后端服务负载均衡

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添加日志

  1. 记录使用nginx进行tcp协议的负载均衡和故障转移
  2. Nginx访问日志(access_log)配置及信息详解

负载均衡分配服务策略
3. 负载均衡分配服务器策略、nginx主从高可用
4. Nginx 负载均衡
5. nginx对后端服务器健康检查实现自动切换。检测是对后端服务器,不是服务。

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值