栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【集群】nginx负载均衡(四层/七层反向代理)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【集群】nginx负载均衡(四层/七层反向代理)

文章目录
  • 前言
  • 一、nginx
    • 1.1 四层反向代理
      • 1.1.1 源码编译nginx(调度器)
      • 1.1.2 配置stream模块(调度器)
      • 1.1.3 配置节点服务器
    • 1.2 七层反向代理
      • 1.2.1 源码编译nginx(调度器)
      • 1.2.2 配置http模块(调度器)
      • 1.2.3 配置节点服务器
  • 二、结语


前言

本博客内容仅为记录博主思路,仅供参考,一切以自己实践结果为准。


一、nginx 1.1 四层反向代理
  • 四层反向代理必须安装stream模块,若之前未配置,则需要重新配置开启
1.1.1 源码编译nginx(调度器)
  • 源码安装nginx
1.1.2 配置stream模块(调度器)
vim /usr/local/nginx/conf/nginx.conf				#修改nginx主配置文件
stream {
    server {
        listen 8080;								#监听端口为8080端口
        proxy_pass httpd_server;					#定义负载均衡服务名称为httpd
    }												#对应server块的大括号
    upstream httpd_server {							#配置负载均衡配置
        server 192.168.13.20:8080 weight=1;			#设置节点服务器IP/端口/权重等配置
        server 192.168.13.30:8080 weight=1;
    }												#对应upstream块的大括号
    log_format main '$remote_addr $upstream_addr-[$time_local] $status $upstream_bytes_sent';
    												#开启日志(内容自定义)
}													#对应stream块的大括号

nginx -t											#检查语法错误
systemctl restart nginx								#重启nginx服务

1.1.3 配置节点服务器
setenforce 0
systemctl stop firewalld

yum install httpd -y

systemctl start httpd
echo 'this is web1' >/var/www/html/index.html

#在另外一台节点服务器重复此操作

1.2 七层反向代理 1.2.1 源码编译nginx(调度器)

源码安装nginx

1.2.2 配置http模块(调度器)
vim /usr/local/nginx/conf/nginx.conf				#修改nginx主配置文件
http{
	upstream httpd{									#定义httpd负载调度服务(可自定义命名)
    server 192.168.13.20:80 weight=1;				#设置节点服务器IP/端口/权重等配置
    server 192.168.13.30:80 weight=1;
	}
	
	keepalive_timeout  3;							#缩小长链接时间,方便验证结果
	
	server {
	    location / {								#设置匹配规则(精确/正则/通用)
        	proxy_pass http://httpd/;				#当触发该匹配规则时,调用自定义的httpd服务
    	}											#对应location快大括号
	}												#对应server块大括号
}													#对应http块大括号

nginx -t											#检查语法错误
systemctl restart nginx								#重启nginx服务

1.2.3 配置节点服务器
setenforce 0
systemctl stop firewalld

yum install httpd -y

systemctl start httpd
echo 'this is web1' >/var/www/html/index.html

#在另外一台节点服务器重复此操作

二、结语
  • 易错点:大括号对应错误,这种错误通过nginx -t 可逐渐排查出来,为了防止此类错误,要养成良好写代码习惯,复制粘贴的时候也要注意大括号是否对应整齐
  • 注意点:想使用四层反向代理,nginx必须安装stream模块
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号