使用nginx实现动静分离的负载均衡集群
架构图
本次要实现的架构图:
工作中我们希望这样:
静态文件处理:可以使用nginx 或apache
动文件处理: apache ,tomcat
图片文件处理: squid
我们可以使用nginx实现动静分离的负载均衡集群:
nginx负载均衡详解
Nginx 的 upstream 负载的5种方式,目前最常用 前3 种方式:
1)、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
2)、weight
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。
3)、ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
4)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5)、url_hash(第三方) url哈西
按访问url的hash结果来分配请求,使同样的url定向到同一个后端服务器,后端服务器为缓存时比较有效
我们先来实现nginx实现负载均衡和动静分离:
1. 编译安装nginx
1) 安装依赖工具
[root@itlaoxin163 ~]# yum -y install gcc gcc-c++ autoconf automake
[root@itlaoxin163 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
itlaoxin是网名互联网老辛的简写
[root@itlaoxin163 ~]# ll nginx-1.8.0.tar.gz
-rw-r--r--. 1 root root 832104 12月 22 2016 nginx-1.8.0.tar.gz
[root@itlaoxin163 ~]#
2)解压:
[root@itlaoxin163 ~]# tar xf nginx-1.8.0.tar.gz -C /usr/local/src/
[root@itlaoxin163 ~]# cd !$
cd /usr/local/src/
[root@itlaoxin163 src]# pwd
/usr/local/src
[root@itlaoxin163 src]# cd nginx-1.8.0/
[root@itlaoxin163 nginx-1.8.0]# pwd
/usr/local/src/nginx-1.8.0
3)开始编译:
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
对参数的解释:
参数:
–with-http_dav_module 启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
–with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
–with-http_addition_module 启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
–with-http_sub_module 启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
–with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
–with-http_mp4_module 启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)
4)安装
[root@itlaoxin163 nginx-1.8.0]# make && make install
5) 生成运行nginx的用户
[root@itlaoxin163 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx
[root@itlaoxin163 nginx-1.8.0]# id !$
id nginx
uid=8000(nginx) gid=8000(nginx) 组=8000(nginx)
[root@itlaoxin163 nginx-1.8.0]#
6)启动nginx
如果你不知道nginx配置文件和启动脚本在哪,可以搜一下
[root@itlaoxin163 ~]# find / -name nginx.conf
/usr/local/nginx/conf/nginx.conf
[root@itlaoxin163 ~]# /usr/local/nginx/sbin/nginx [root@itlaoxin163 ~]# netstat -antup |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9327/nginx: master
[root@itlaoxin163 ~]#
7) 查看nginx执行效果
[root@itlaoxin163 ~]# systemctl stop firewalld.service
[root@itlaoxin163 ~]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Fri, 02 Apr 2021 00:55:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 02 Apr 2021 00:46:51 GMT
Connection: keep-alive
ETag: "6066697b-264"
Accept-Ranges: bytes
2. 配置nginx成为分发器
1)先备份
[root@itlaoxin163 ~]# cd /usr/local/nginx/c
client_body_temp/ conf/
[root@itlaoxin163 ~]# cd /usr/local/nginx/c
client_body_temp/ conf/
[root@itlaoxin163 ~]# cd /usr/local/nginx/conf/
[root@itlaoxin163 conf]# cp nginx.conf nginx.conf.bak
[root@itlaoxin163 conf]#
2) 把nginx设置成分发器,实现动静分离
为了方便大家复制:
location / {root html;index index.html index.htm;if ($request_uri ~* \.html$){proxy_pass http://htmlservers;} if ($request_uri ~* \.php$){proxy_pass http://phpservers;} proxy_pass http://picservers;
3) 定义负载均衡设备的IP
在nginx配置文件最后一行}前添加一下内容:
upstream htmlservers { server 192.168.1.162:80; server 192.168.1.164:80;}upstream phpservers{server 192.168.1.162:80;server 192.168.1.164:80;}upstream picservers {server 192.168.1.162:80;server 192.168.1.164:80;}
4) 测试配置文件是否ok
[root@itlaoxin163 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@itlaoxin163 conf]#
5) 重启nginx
[root@itlaoxin163 conf]# /usr/local/nginx/sbin/nginx -s reload
在ITlaoxin62服务器和63服务器上进行配置
ITlaoxin62:
配置web服务器:
[root@ITlaoxin162 html]# yum install httpd php -y
生成静态测试文件:
[root@ITlaoxin162 html]#echo 192.168.1.162 > /var/www/html/index.html
[root@itlaoxin162 ~]# vim /var/www/html/test.php
写入内容:
echo "我是162服务器";
<?php
phpinfo();
?>
启动apache```bash
[root@itlaoxin162 html]# service httpd restart
Redirecting to /bin/systemctl restart httpd.service
ITlaoxin164
[root@itlaoxin164 ~]# yum install httpd php -y
[root@itlaoxin164 ~]# echo 192.168.1.164 > /var/www/html/index.html
[root@itlaoxin164 ~]# cd /var/www/html/
[root@itlaoxin164 html]# vim laoxin.php
[root@itlaoxin164 html]#
echo "我是164服务器";
<?php
phpinfo();
?>
启动
[root@itlaoxin164 html]# service httpd restart
Redirecting to /bin/systemctl restart httpd.service
[root@itlaoxin164 html]#
到目前为止,nginx负载均衡就结束了,接下来就是测试的时间
测试转发动态页面:
使用nginx实现动静分离的负载均衡集群相关推荐
- keepalive+nginx实现负载均衡高可用_超详细的LVS+keepalived+nginx实现高性能高可用负载均衡集群教程...
概述 前面已经介绍了前两部分内容,下面主要介绍在nginx服务器方面的配置和测试整个集群是否可用. 在realserver端配置VIP 1.两台nginx服务器都要执行下面脚本: #vi /etc/r ...
- nginx进阶-动静分离,负载均衡
目录结构 nginx动静分离,负载均衡简单使用 动静分离,负载均衡 1.官网: http://nginx.org 2.解压: tar -zxvf nginx-1.13.9.tar.gz -C /usr ...
- Nginx + Tomcat 动静分离实现负载均衡
0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 1 # 定义Nginx运行的用户 和 用户组 如 ...
- nginx+keepalived 高可用兼负载均衡集群
Nginx是一个高性能的web服务器,同时也是一个优秀的反向代理服务器,本文利用两台Dell R720 构建一个高可用兼负载均衡的Linux web集群. 原理 通过nginx分别搭建两个web服务器 ...
- Keepalived+Nginx实现高可用负载均衡集群
转载自 https://www.cnblogs.com/mrlapulga/p/6857294.html 一 环境介绍 1.操作系统 CentOS Linux release 7.2.1511 (Co ...
- Nginx动静分离实现负载均衡
转载自 Nginx动静分离实现负载均衡 前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. Nginx.conf配置 1 # 定义Ngin ...
- nginx动静分离配置_Nginx 动静分离与负载均衡的实现
一.前提 企业中,随着用户的增长,数据量也几乎成几何增长,数据越来越大,随之也就出现了各种应用的瓶颈问题. 问题出现了,我们就得想办法解决,一般网站环境,均会使用LAMP或者LNMP,而我们对于网站环 ...
- Nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分...
Nginx反向代理.动静分离.负载均衡及rewrite隐藏路径详解 (Nginx Apache MySQL Redis) 楓城浪子原创,转载请标明出处! 更多技术博文请见个人博客:https://fe ...
- Nginx运维之一 反向代理、动静分离、负载均衡
Nginx运维之一 反向代理.动静分离.负载均衡 Nginx简述 Nginx 优势功能 Tengine 反向代理 正向代理 反向代理 动静分离 负载均衡 Nginx简述 Nginx是lgor Syso ...
最新文章
- Android CheckBox 修改选择框
- 女生零基础学软件测试难不难
- SoapUI进行REST请求,POST方法提交到数据库的数据乱码问题
- 【ZJOI 2008】树的统计 Count
- hadoop安装与配置
- 如何在 ASP.Net Core 中对接 WCF
- python 双层for循环_day05-Python运维开发基础(双层循环、pass/break/continue、for循环)...
- transition.tween
- 浏览器内存泄漏问题的跟踪与解决(转)
- 智慧工厂用到的技术_CCF VC物联网应用技术专业工作组走进苏宁物流智慧仓储工厂参观学习...
- Spring的注解@Autowired和@Resource的区别
- Java排序算法——冒泡排序(Bubble Sort)
- 没拿到一毛钱股份的老公和CEO身份曝光,发文妻子的选妃照也被挖出来了……
- TestFlight Beta 相关问答
- 为什么说盲盒市场充满机遇
- Life begins in Beijin
- 百度amis技巧汇总页
- vue 单元测试报错之 undefined is not a constructor (evaluating 'expect(vmComponent.count).toBe(1)')
- 使用方法论报告第 2 部分:方法违例对于 QoR 的影响
- 利用开源工具搭一套汉英翻译系统(一):预处理工具
热门文章
- Drupal主题开发指南(5.x)
- mock模拟数据,get、post请求
- 爱心宠物诊所系统(禹州实训项目)
- 库伯勒立撤倍加福三合一拉线编码器配置工具
- 黑马程序员_Java基础_this,super,static,final关键字
- <<多模态预训练—泛读系列(一)>>ViLBERT—(NeurIPS-2019),VL-BERT—(ICLR-2020),VisualBERT—(ACL-2020)
- 【免费直播】零基础Office速通,助您走向Word/Excel/PPT高手之路
- 企业微信推送天气、课表、纪念日、每日一句等(含源码和详细步骤)
- linux3.4.0 按键驱动程序分析(pandaboard omap4460)
- 暴风云视频平台SDK使用介绍(四)-- 视频播放(Android)