1、nginx负载均衡html
nginx负载均衡配置很简单,能够实现7层的负载,对一些轻量级访问量的站点仍是很实用的nginx
一、架构web
系统版本: CentOS 6.6 x86_64 nginx版本: 1.10.2 #当前最新版本 服务器: 负载均衡server 10.0.18.146 端口 80 后端web server 10.0.18.144 端口 80 10.0.18.145 端口80
二、配置过程算法
在三台服务器上配置nginx的yum源后端
#cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 而后使用yum安装nginx #yum install nginx -y
在三台服务器上配置打开文件数centos
#tail -5 /etc/security/limits.conf # End of file * soft nofile 65535 * hard nofile 65535 * soft nproc 10240 * hard nproc 10240
配置后端两台server:10.0.18.144,以下:数组
#cat /etc/nginx/nginx.conf #修改以下两项,其余不变 events { use epoll; #使用epoll worker_connections 10240; #增长链接数 } #cat /etc/nginx/conf.d/default.conf #修改server_name 其余不变 server { listen 80; server_name nginx1.test.com; ……………… } 修改默认页面以下: #cat /usr/share/nginx/html/index.html #增长18.144,其余不变 ………… <h1>Welcome to nginx 18.144!</h1> ………… 启动nginx #service nginx start
在物理机添加域名解析以下:浏览器
添加在hosts中 --->C:\Windows\System32\drivers\etc\hosts 10.0.18.144 nginx1.test.com 10.0.18.145 nginx2.test.com 10.0.18.146 balance.test.com
在浏览器访问,以下:
缓存
配置后端两台server:10.0.18.145,方法和18.144同样,再也不赘述,在浏览器访问以下:bash
三、配置负载均衡server
先查看配置文件:
#cat nginx.conf events { use epoll; worker_connections 10240; } 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 65; #gzip on; #include /etc/nginx/conf.d/*.conf; upstream backend { #添加的后端server,权重为1 server 10.0.18.144 weight=1; server 10.0.18.145 weight=1; } server { listen 80; server_name balance.test.com; location / { proxy_set_header Host $host; #设置主机头和客户端真实地址 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering on; #开启缓存 proxy_pass #方向代理地址 } } } 而后启动nginx #service nginx start
在浏览器访问http://balance.test.com进行测试:
不断刷新浏览器,是能够看到18.144和18.145轮流相应的,以下:
这样就实现了nginx的负载均衡机制,很简单!
2、nginx upstream的几种方式:
一、轮询
轮询是upstream的默认分配方式,即每一个请求按照时间顺序轮流分配到不一样的后端服务器,若是某个后端服务器down掉后,能自动剔除,以下:
upstream backend { server 192.168.1.101:80; server 192.168.1.102:8080; #后端的端口能够不同,在这里配置上就OK }
二、weight
轮询的增强版,便可以指定轮询比率,weight和访问概率成正比,主要应用于后端服务器配置不一样的场景下,以下:
upstream backend { server 192.168.1.101 weight=1; server 192.168.1.102 weight=2; #权重越大,相应web端的概率越大 } 也能够这样: upstream backend { server 192.168.1.101:80 weight=1; server 192.168.1.102:8080 weight=2; }
三、ip_hash
每一个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每一个访客会固定访问一个后端服务器,能够解决session一致问题,以下:
upstream backend { ip_hash; server 192.168.1.101; server 192.168.1.102; }
四、fair
fair顾名思义,公平地按照后端服务器的响应时间(rt)来分配请求,响应时间短即rt小的后端服务器优先分配请求,以下:
upstream backend { server 192.168.1.101; server 192.168.1.102; fair; }
五、url_hash
与ip_hash相似,可是按照访问url的hash结果来分配请求,使得每一个url定向到同一个后端服务器,主要应用于后端服务器为缓存时的场景下,以下:
upstream backend { server 192.168.1.101; server 192.168.1.102; hash $request_uri; hash_method crc32; } 其中,hash_method为使用的hash算法,须要注意的是此时,server语句中不能加weight等参数。
注意:max_fails和fail_timeout通常会关联使用,若是某台server在fail_timeout时间内出现了max_fails次链接失败,那么Nginx会认为其已经挂掉了,从而在fail_timeout时间内再也不去请求它,fail_timeout默认是10s,max_fails默认是1,即默认状况是只要发生错误就认为服务器挂掉了,若是将max_fails设置为0,则表示取消这项检查,以下:
upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; }
3、配置nginx反向代理实现web缓存服务器
nginx支持相似squid的web缓存功能,就是把web页面根据url编码哈希后保存到硬盘上,有不少资料显示,nginx的稳定性和速度不逊于Squid,并且在性能上nginx对多核cpu的利用也超过Squid。并且nginx也同时支持负载均衡,这对于在短时间内忽然顶不住访问量的网站来讲十分的便利,配置以下:
在负载均衡服务器配置:10.0.18.146 #cat nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; worker_connections 10240; } 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 65; #gzip on; #include /etc/nginx/conf.d/*.conf; proxy_temp_path /usr/local/nginx/proxy_temp_dir; #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区 proxy_cache_path /usr/local/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=30g; #设置Web缓存区名称为cache_one,内存缓存空间大小为100MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为20GB upstream backend { server 10.0.18.144:80 weight=1; server 10.0.18.145:8080 weight=1; } server { listen 80; server_name balance.test.com; location / { #若是后端的服务器返回50二、50四、执行超时等错误,自动将请求转发到upstream负载均衡池中的另外一台服务器,实现故障转移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; #对不一样的HTTP状态码设置不一样的缓存时间 proxy_cache_key $host$uri$is_args$args; #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 #设置主机头和客户端真实地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_buffering on; #禁用缓存 proxy_pass #后端server expires 1d; #缓存,至关于cookies,1d表示1天,1m表示1分钟(不肯定) } #location ~ /purge(/.*) { #使用purge,用于清除缓存,须要编译ngx_cache_purge # allow 127.0.0.1; # deny all; # proxy_cache_purge cache_one $host$1$is_args$args; #} } } 建立目录: #mkdir -pv /usr/local/nginx/{proxy_cache_dir,proxy_temp_dir} 重启nginx,确保成功! #service nginx restart
而后到18.144和18.145的网页存放路径分别建立test.html,内容以下:
18.144 #cat /usr/share/nginx/html/test.html <h1>This is a test page 18.144</h1> 18.145 #cat /usr/share/nginx/html/test.html <h1>This is a test page 18.145</h1>
而后经过负载均衡域名来访问,http://balance.test.com/test.html 显示页面以下:
而后不停刷新或者更换不一样的浏览器,一直都是18.145这台server响应,由于设置了缓存,第一次访问以后,就会将数据缓存起来,直到设置的缓存过时时间生效为止,而后这个时候到负载均衡服务器查看缓存信息:
#cd /usr/local/nginx/ #ll proxy_cache_dir/9/41/ total 4 -rw------- 1 nginx nginx 444 Nov 4 16:53 b5e4d782cfd84da92097d8ed956fb419 能够看到缓存文件已经生成了!
查看缓存内容,以下图:
能够看到缓存的是18.145这个页面!
如今将缓存手动删除,那么更换浏览器访问http://balance.test.com/test.html 会出现18.144响应的页面,以下:
#cd /usr/local/nginx #rm -rf proxy_cache_dir/* 访问页面以下:
查看缓存内容,以下图:
到这里就结束了!不足之处,请多多指教!