博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实践NGINX的反向代理与负载均衡
阅读量:7249 次
发布时间:2019-06-29

本文共 3065 字,大约阅读时间需要 10 分钟。

实践NGINX的反向代理与负载均衡

安装nginx过程

[root@lb01 opt]# yum install  pcre-devel openssl-devel -y[root@lb01 opt]# wget -q http://nginx.org/download/nginx-1.10.2.tar.gz [root@lb01 opt]# useradd nginx -s /sbin/nologin -M [root@lb01 opt]# tar xf nginx-1.10.2.tar.gz -C /usr/src/[root@lb01 opt]# cd /usr/src/nginx-1.10.2/[root@lb01 nginx-1.10.2]# ./configure --prefix=/usr/local/nginx-1.10.2 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module[root@lb01 nginx-1.10.2]#make;make install [root@lb01 nginx-1.10.2]# cd /usr/local/[root@lb01 local]# ln -s /usr/local/nginx-1.10.2/ /usr/local/nginx[root@lb01 local]# echo 'export PATH=/usr/local/nginx/sbin:$PATH'>>/etc/profile[root@lb01 local]# source /etc/profile[root@lb01 local]# nginx[root@lb01 local]# lsof -i:80

配置nginx负载服务器文件

[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf[root@lb01 conf]# vim nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;                                      upstream server_pools {     --在http区块里upstream模块,将web节点的IP或着域名放置池中        server 10.0.0.11:80 weight=1 max_fails=3 fail_timeout=10;   --weight 权重        server 10.0.0.14:8081 weight=1 max_fails=3 fail_timeout=10;  --max_fails失败的尝试次数        server 10.0.0.14:8082 weight=1 max_fails=3 fail_timeout=10;  --fail_timeout 失败后的再次尝试时间    }     server {        listen 80;        server_name localhost;        location / {            proxy_pass http://server_pools;  --proxy模块调用upstream模块池里面的web节点,            proxy_set_header Host $host;    --该参数在访问后端服务器的时候 会带上hosts信息。定义虚拟主机的信息标签            proxy_set_header X-Forwarded-For $remote_addr;   --代理的时候在会显示真实客户客户端IP地址        }    }配置完成了检测语法重启服务[root@lb01 conf]# nginx -tnginx: the configuration file /usr/local/nginx-1.10.2/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.10.2/conf/nginx.conf test is successful[root@lb01 conf]# nginx -s stop[root@lb01 conf]# nginx

查看web服务器的首页文件(下面3台web服务器是之前实验做好了的)

查看nginx服务器上的首页文件[root@nginx ~]# cat /usr/local/nginx/html/www.anuo1.com/index.html anuo nginx web --111111查看tomcat多实例服务器的首页文件[root@tomcat ~]# cat /usr/local/tomcat1/webapps/ROOT/index.jsp  anuo tomcat web111111 [root@tomcat ~]# cat /usr/local/tomcat2/webapps/ROOT/index.jsp  anuo tomcat web2

测试访问

[root@lb01 ~]# ifconfig eth0    --查看确认负载均衡服务器的IPeth0      Link encap:Ethernet  HWaddr 00:0C:29:9F:CF:C6            inet addr:10.0.0.13  Bcast:10.0.0.255  Mask:255.255.255.0……进行测试:[root@lb01 conf]# elinks -dump 10.0.0.13        --elinke -dump 命令是直接将URL的内容输出至标准输出   anuo tomcat web111111[root@lb01 conf]# elinks -dump 10.0.0.13   anuo tomcat web2[root@lb01 conf]# elinks -dump 10.0.0.13   anuo nginx web --111111

nginx还有一些参数说明

ip_hash 参数 可以解决动态网页的session共享问题,但有时会导致请求负载均衡分配不均least_conn 参数会根据后端节点的连接数来决定分配情况,哪个机器连接数少就分发。 就是看谁闲发送给谁fair 参数  根据后端服务器的响应时间来分配请求,响应时间短的优先分配。
调度算法  定义轮询调度算法 rr 默认调度算法   平均分配  定义权重调度算法 wrr  定义静态调度算法 ip-hash  定义最小的连接数-least_conn

转载于:https://blog.51cto.com/13744837/2117673

你可能感兴趣的文章
jQuery源码分析之jQuery的定义
查看>>
一些经典面试题分析(上)
查看>>
[JS相关的记录01] 那什么来面对你,面向对象编程(__proto__,prototype,constructor以及原型链)...
查看>>
夏日葵电商:搭建一个商城系统,N+功能方案揭秘!
查看>>
Akka系列(一):Akka简介与Actor模型
查看>>
yii2获得从数据库获得数据的方法并处理
查看>>
Android开发百度地图(一)之初体验
查看>>
微服务指南走北(四):你不愿意做微服务架构的十个理由
查看>>
CSS代码重构与优化之路
查看>>
使用 sigprocmask 和 sigpending 在程序正文中捕获和处理信号
查看>>
Bodymovin插件的使用
查看>>
详细深入分析 Java ClassLoader 工作机制
查看>>
关于设计模式
查看>>
对一个“老”架构的重新思考
查看>>
DoubanFMPlayer, A mimic of Douban.fm player
查看>>
埃森哲、亚马逊和万事达卡抱团推出的区块链项目有何神通?
查看>>
2019年自动驾驶5大趋势预测:第一台Level 5 无人车问世
查看>>
后APP时代的破局之路 :阿里技术“三大容器五大方案”亮相,百川开放全面升级...
查看>>
工欲善其事-必先利其器之终端
查看>>
64位的Mac OS X也有Windows.Forms了
查看>>