nginx负载均衡及详细配置
接上篇nginx配置,然后再准备两台web服务器:
nginx服务器:192.168.0.241
web1:192.168.0.141
web2:192.168.0.142
一、两台web服务器先安装http:
[root@host1 ~]# yum -y install httpd
[root@host1 ~]# /etc/init.d/httpd start
[root@host1 ~]# echo "Hello,I'm 192.168.0.141" > /var/www/html/index.html
[root@host1 ~]# curl http://192.168.0.141
Hello,I'm 192.168.0.141
[root@host2 ~]# yum -y install httpd
[root@host1 ~]# /etc/init.d/httpd start
[root@host2 ~]# echo "Hello,I'm 192.168.0.142" > /var/www/html/index.html
[root@host2 ~]# curl http://192.168.0.142
Hello,I'm 192.168.0.142
#注意先看下服务器上是否有安装http,如httpd启动不起来,看下是否80端口在被占用
二、nginx服务器配置负载均衡:
[root@host3 conf]# cd /usr/local/nginx/conf/
[root@host3 conf]# vim nginx.conf
…………
http { #定义源服务器组
upstream team {
server 192.168.0.141;
server 192.168.0.142;
}
…………
…………
server {
#定义监听端口
listen ;
#定义访问域名
server_name www.load.com;
location / {
#调用服务器组
proxy_pass http://team;
root html;
index index.html;
}
}
…………
…………
}
:wq
[root@host3 conf]# nginx -s reload
#继续添加nginx服务器访问域名
[root@host3 conf]# vim /etc/hosts
…………
192.168.0.241 www.nginx1.com www.nginx2.com www.load.com
:wq
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
注意:如果访问不了看下是否防火墙限制了,8080端口如被占用可改为其他端口
三、设置权重:
#接上面的配置
[root@host3 conf]# vim nginx.conf
…………
http { upstream team {
#设置权重参数,机器的性能越好,可以设置参数越大
server 192.168.0.141 weight=;
server 192.168.0.142 weight=;
}
…………
:wq
[root@host3 conf]# nginx -s reload
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
附:nginx优化参数(http模块)
server_tokens off; #添加此行,不显示nginx具体版本号
include mime.types; #设定mime类型,类型由mime.type文件定义
sendfile on; #提升nginx读文件性能
tcp_nodelay on; #关闭tcp的缓延迟发送数据,即立即相应
keepalive_timeout 65; #连接超时时间
gzip on; #开启gzip压缩
gzip_min_length 1000; #小于1000k的不压缩,越压缩越大
gzip_comp_level 4; #压缩级别是4(1-9个级别)
gzip_types …… #允许压缩的类型,/usr/local/nginx/conf/mime.types有类型
client_header_buffer_size 1k; #设定请求缓存
large_client_header_buffers 4 4k; #最大请求缓存个数与容量
#先根据client_header_buffer分配,如果不够,再根据large值分配
#设定虚拟主机配置:
#禁止访问 .htxxx 文件
nginx负载均衡及详细配置的更多相关文章
- Nginx负载均衡的详细配置及使用案例详解.
感谢看过这一些列博文和评论的小伙伴, 我把自己所看到的学到的拿到这里来分享是想和大家一起学习进步, 想听听园友给出的意见, 也是对自己学习过程的一个总结. 技术无止境, 我们仍需努力! 1,话不多说, ...
- [项目构建 十三]babasport Nginx负载均衡的详细配置及使用案例详解.
在这里再次说明下, 这个项目是从网上 找到的一套学习资料, 自己在 空闲时间学习了这些东西. 这里面的code当然会有很多不完善的地方, 但是确实也能学到很多新东西.感谢看过这一些列博文和评论的小伙伴 ...
- Nginx负载均衡的详细配置 + Keepalived使用
1,话不多说, 这里我们来说下很重要的负载均衡, 那么什么是负载均衡呢? 由于目前现有网络的各个核心部分随着业务量的提高,访问量和数据流量的快速增长,其处理能力和计算强度也相应地增大,使得单一的服务器 ...
- Nginx记录-nginx 负载均衡5种配置方式(转载)
nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成 ...
- nginx 负载均衡5种配置方式
nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成正比, ...
- nginx负载均衡之入门配置
先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况.那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上 ...
- 干货 | Nginx负载均衡原理及配置实例
一个执着于技术的公众号 Nginx系列导读 给小白的 Nginx 10分钟入门指南 Nginx编译安装及常用命令 完全卸载nginx的详细步骤 Nginx 配置文件详解 理解正向代理与反向代理的区别 ...
- nginx负载均衡tomcat和配置ssl
目录 tomcat 组件功能 engine host context connector service server valve logger realm UserDatabaseRealm 工作流 ...
- nginx负载均衡二:配置
配置方法一(可用): upstream tomcatserver1 { server ; server 192.168.70.172; server 192.168.70.173 down; serv ...
随机推荐
- 新概念英语(1-117)Tommy's breakfast
Lesson 117 Tommy's breakfast 汤米的早餐 Listen to the tape then answer this question. What does she mean ...
- Spring知识点回顾(01)Java Config
Spring知识点回顾(01) 一.Java Config 1.服务和服务注入 2.Java 注解 :功能更强一些 3.测试验证 二.注解注入 1.服务和服务注入 2.配置加载 3.测试验证 三.总结 ...
- spring-oauth-server实践:使用授权方式四:client_credentials 模式下access_token做业务!!!
spring-oauth-server入门(1-10)使用授权方式四:client_credentials 模式下access_token做业务!!! 准备工作 授权方式四::客户端方式: 服务网关地 ...
- intelj idea 创建聚合项目(典型web项目,包括子项目util、dao、service)
需求:第三方提供了http api接口,我们需要将其数据全部取回来,存放到本地Mysql数据库. 开发工具是intelj idea,准备基于maven创建聚合项目,util作为工具包,单独作为一个工程 ...
- 详解Class
Classs是es6提供的类,相当于es5的构造函数. 写法: class Foo { constructor () { // new 的时候会调用该方法,可以通过return改变构造函数的返回值 r ...
- spark2.1操作json(save/read)
建筑物配置信息: case class BuildingConfig(buildingid: String, building_height: Long, gridcount: Long, gis_d ...
- view-xpath
https://addons.mozilla.org/en-US/firefox/ WebDriver Element Locator
- 1020关于MYCAT的安装和使用总结
第一部分 读写分离配置 转自:http://www.51testing.com/html/34/369434-3686088.html 使用Mycat 做简单的读写分离(一) 原本使用的是amoeba ...
- 一览Django框架(转载)
本文面向:有python基础,刚接触web框架的初学者. 环境:windows7 python3.5.1 pycharm专业版 Django 1.10版 pip3 一.Django简介 百度百 ...
- RPO(Relative Path Overwrite)
Conception(Relative vs Absolute) Abosolute Path: "/etc/hosts"(in Linux), "C:\Windows\ ...