| 主机 | IP | 身份 |
|---|---|---|
| lb4 | 172.16.1.6,10.0.0.6 | 四层负载均衡 |
| lb01 | 172.16.1.4,10.0.0.4 | 七层负载均衡 |
| lb02 | 172.16.1.5,10.0.0.5 | 七层负载均衡 |
lb4和lb02搭建Nginx
# 配置yum源 [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true # 安装Nginx [root@lb02 ~]# yum install nginx -y [root@lb4 ~]# yum install nginx -y # 创建用户 [root@lb02 ~]# groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M [root@lb4 ~]# groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M # 配置nginx [root@lb02 ~]# vim /etc/nginx/nginx.conf user www; [root@lb4 ~]# vim /etc/nginx/nginx.conf user www; # 启动Nginx [root@lb4 ~]# systemctl start nginx && systemctl enable nginx && systemctl status nginx [root@lb02 ~]# systemctl start nginx && systemctl enable nginx && systemctl status nginx
将lb01配置同步到lb02
[root@lb01 ~]# scp /etc/nginx/conf.d/* 172.16.1.5:/etc/nginx/conf.d/ [root@lb01 ~]# scp /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/
测试lb02的负载均衡
[root@lb02 ~]# nginx -t && systemctl restart nginx #配置hosts测试 10.0.0.5 linux.wp.com
四层负载均衡语法
Syntax: stream { ... }
Default: —
Context: main
#示例:四层负载均衡stream模块跟http模块在同一级别,不能配置在http里面
stream {
upstream backend {
server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
}
配置nginx主配置文件
[root@lb4 ~]# vim /etc/nginx/nginx.conf
#注释http层所有内容
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
#添加一个包含文件
include /etc/nginx/conf.c/*.conf;
#http {
# include /etc/nginx/mime.types;
# default_type application/octet-stream;
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# access_log /var/log/nginx/access.log main;
# sendfile on;
# #tcp_nopush on;
# keepalive_timeout 65;
# #gzip on;
# include /etc/nginx/conf.d/*.conf;
#}
配置四层负载均衡
#创建目录
[root@lb4 ~]# mkdir /etc/nginx/conf.c
#配置
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
upstream lbserver {
server 10.0.0.4:80;
server 10.0.0.5:80;
}
server {
listen 80;
proxy_pass lbserver;
proxy_connect_timeout 1s;
proxy_timeout 3s;
}
}
# 启动Nginx
[root@lb4 ~]# nginx -t && systemctl start nginx
# 配置hosts访问
10.0.0.6 linux.lb4.com
四层负载均衡配置日志
#四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置是在http以外的;
#如果需要日志则需要配置在stream下面
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
access_log /var/log/nginx/proxy.log proxy;
upstream lbserver {
server 10.0.0.4:80;
server 10.0.0.5:80;
}
server {
listen 80;
proxy_pass lbserver;
proxy_connect_timeout 1s;
proxy_timeout 3s;
}
}
#查看所有web服务器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log
请求负载均衡的5555端口,跳转到web01的22端口
#简单配置
stream {
server {
listen 5555;
proxy_pass 172.16.1.7:22;
}
}
#一般配置
stream {
upstream ssh_7 {
server 10.0.0.7:22;
}
server {
listen 5555;
proxy_pass ssh_7;
}
}
# 测试
[D:\~]$ ssh root@10.0.0.6:5555
成功跳转
请求负载均衡的6666端口,跳转至172.16.1.51:3306
stream {
upstream db_51 {
server 172.16.1.51:3306;
}
server {
listen 6666;
proxy_pass db_51;
}
}
数据库从库的负载均衡
stream {
upstream dbserver {
server 172.16.1.51:3306;
server 172.16.1.52:3306;
server 172.16.1.53:3306;
server 172.16.1.54:3306;
server 172.16.1.55:3306;
server 172.16.1.56:3306;
}
server {
listen 5555;
proxy_pass dbserver;
}
}
到此这篇关于Nginx四层负载均衡配置的文章就介绍到这了,更多相关Nginx四层负载均衡内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!