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 ...
随机推荐
- [笔试题目]使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍?
[笔试题目] 使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍? StringBuffer 底层是依赖了一个字符数组才能存储字符 ...
- PAT1048. Find Coins(01背包问题动态规划解法)
问题描述: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
- 可空类型 Nullable<T>
Nullable<T> 内部实现了显示和隐式转换 显示转换: public static explicit operator T(T? value) { return value.Valu ...
- Python-模块使用-Day6
Python 之路 Day6 - 常用模块学习 本节大纲: 模块介绍time &datetime模块randomossysshutiljson & picleshelvexml处理ya ...
- 如何排查CPU飙升的Java问题
1. JPS 查看jvm进程 2. 显示线程列表 ps -mp pid -o THREAD,tid,time 找到了耗时最高的线程tid 3. tid转换成16进制 printf "%x\n ...
- 【原生js实现一键回到顶部】
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- leetcode算法:Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- Python之socketserver模块和验证客户端链接的合法性
验证客户端链接的合法性 分布式系统中实现一个简单的客户端链接认证功能 #_*_coding:utf-8_*_ from socket import * import hmac,os secret_ke ...
- [转]安卓新一代多渠道打包工具Walle 解决渠道包V2签名问题
转自https://www.jianshu.com/p/572b59829a08 为什么要打多个渠道的包? 大家都知道,android应用商店大大小小有几百个,作为一个有志向的app,就需要做到统计各 ...
- svn介绍和安装
什么是SVN呢,作用是什么: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS/CVS,它采取了分支管理系统,它的设计目标就是取代CVS.SVN就是用于多个人共同开 ...