nginx负载均衡

nginx负载均衡

1.nginx负载均衡

  • 关闭防火墙与selinux

1.安装nginx

julia 复制代码
#安装1.26.1版本nginx
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
[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

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@localhost ~]# yum -y install nginx
#更改nginx配置文件

2.实验环境

  • 准备三台机器
  • 分别下载nginx
  • 负载均衡器:192.168.222.130
  • 两台服务端:192.168.222.131;192.168.222.132

3.更改nginx配置文件(192.168.222.130)

julia 复制代码
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
upstream takehaye {
	server 192.168.222.131:80;
	server 192.168.222.132:80;
	}
server {
	listen		80;
	server_name	localhost;
	location / {
	proxy_pass	http://192.168.222.130
	}
}
#检查nginx配置文件
[root@localhost ~]# nginx -t
#重启服务
[root@localhost ~]# systemctl restart nginx

4.更改服务端测试页面(192.168.222.131;192.168.222.132)

julia 复制代码
#192.168.222.131
[root@localhost ~]# rm -rf /usr/share/nginx/html/*
[root@localhost ~]# echo "11111111111" > /usr/share/nginx/html/index.html
[root@localhost ~]# systemctl restart nginx
#192.168.222.132
[root@localhost ~]# rm -rf /usr/share/nginx/html/*
[root@localhost ~]# echo "22222222222" > /usr/share/nginx/html/index.html
[root@localhost ~]# systemctl restart nginx

2.7层协议

1.7层协议

2.配置nginx代理服务器的配置文件

julia 复制代码
#这里要加到主配置文件
#upstream与server在http之下;upstream与server是同级
[root@localhost ~]# vim /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}



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  0;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;


    upstream takehaye {
        server 192.168.222.131:80 weight=2;
        server 192.168.222.132:80 weight=2;
   }
   server {
        listen          80;
        server_name     www.takehaye.com;
        location / {
                proxy_pass http://takehaye;
        }
   }
   upstream yhb {
        server 192.168.222.131:80 weight=3;
        server 192.168.222.132:80 weight=1;
   }
   server {
        listen          8080;
        server_name     www.yhb.com;
        location / {
        proxy_pass http://yhb;
        }
   }
}

#检查配置问价是否正确
[root@localhost ~]# nginx -t
#重启服务
[root@localhost ~]# systemctl restart nginx

3.配置Windows本地解析

#配置本地解析

路径:c:\windows\System32\drivers\etc\hosts

添加: 192.168.222.130 www.takehaye.com

192.168.222.130 www.yhb.com

3.会话保持

  • ip_hash使用哈希算法,将同意客户端的请求总是发往同一个后端服务器,除非该服务器不可用
julia 复制代码
upstream takehaye {
	server 192.168.222.131:80;
	server 192.168.222.132:80;
	ip_hash;
}
  • ip_hash使用哈希算法,将同意客户端的请求总是发往同一个后端服务器,除非该服务器不可用
julia 复制代码
upstream takehaye {
	server 192.168.222.131:80;
	server 192.168.222.132:80;
	ip_hash;
}
相关推荐
likfishdn18 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean38 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=39 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水1 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
北京耐用通信2 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
崔lc4 小时前
linux中安装部署Jenkins,成功构建springboot项目详细教程
运维·jenkins
浪九天4 小时前
Nginx系列05(负载均衡、动静分离)
nginx·运维开发·持续部署
luojiaao4 小时前
【Jenkins】个人向-Jenkinsfile如何写
运维·jenkins