实验环境:
iptables和selinux关闭
redhat6.5
nginx:test1: 172.25.1.11
[root@test1 ~]# ls
nginx-1.14.0.tar.gz
[root@test1 ~]# tar zxf nginx-1.14.0.tar.gz
[root@test1 ~]# useradd -s /sbin/nologin nginx
[root@test1 ~]# id nginx
[root@test1 ~]# cd nginx-1.14.0
[root@test1 nginx-1.14.0]# cd src/core/
[root@test1 core]# vim nginx.h
[root@test1 core]# cd -
/root/nginx-1.14.0
[root@test1 nginx-1.14.0]# cd auto/cc/
[root@test1 cc]# vim gcc //进行注释
[root@test1 cc]# cd -
/root/nginx-1.14.0
[root@test1 nginx-1.14.0]# yum install pcre-devel openssl-devel -y //下载所需的依赖包
[root@test1 nginx-1.14.0]# ./configure --help //查看模块
[root@test1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[root@test1 nginx-1.14.0]# make && make install
[root@test1 nginx-1.14.0]# cd
[root@test1 ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@test1 ~]# nginx -t //检查配置是否有错误
[root@test1 ~]# nginx //打开服务
[root@test1 ~]# nginx -s reload //重新加载
[root@test1 ~]# nginx -s stop //停止服务
[root@test1 html]# pwd
/usr/local/nginx/html
[root@test1 html]# vim test1.html
[root@test1 html]# nginx
网页进行访问
到此,nginx服务就搭建完成了。。。
nginx的负载均衡是基于反向代理之上实现的。自带的模块有:
ngx_http_proxy_module: proxy代理模块,把请求后抛给服务器节点或upstream服务器池
ngx_http_upstream_module: 负载均衡模块,可以实现网站的负载均衡及节点的健康检查功能
如下是HTTP负载均衡模块upstream 指定设置群服务器,服务器可以指定不同的权重
nginx支持5种方式的查询:
- 轮询 Nginx默认的查询方式 ,默认权重为1。 //下面的实验用到的即为这种方式
- weight 指定分配的轮询方式,根据后端服务器的性能来做权重。
- ip_hash 每个请求按照ip的hash结果分配,这样每个IP地址就可以固定的访问后端的一台服务器,解决了集群部署环境下session共享的问题。
- fair 第三方模块,这个原理是按照响应时间的优先来分配的。需要安装upstream_fair模块
- url_hash 按照url的hash结果来分配请求,使每个url定向到同一个后端的服务器。需要安装nginx的hash软件包
nginx对后端节点健康检查的方式主要有3种:
- ngx_http_proxy_module 模块和ngx_http_upstream_module模块(自带)
- nginx_upstream_check_module模块,需要安装
- ngx_http_healthcheck_module模块,需要安装
实验环境:
test1: nginx 172.25.1.11 调度器
test2: httpd 172.25.1.12 realserver
test2: httpd 172.25.1.13 realserver
foundation: 172.25.1.250 client
nginx常用命令:
nginx -t //nginx语法检查
nginx //查看nginx
nginx -s reload //刷新nginx服务
nginx -s stop //关闭nginx服务
在编译完成的情况下继续下一步
负载均衡原理图(盗用一下哈):
反向代理原理图:
对test1进行负载均衡的配置:
[root@test1 ~]# cd /usr/local/nginx/conf/
[root@test1 conf]# vim nginx.conf //记得提前备份一下
user nginx nginx; //用户名,可以不改使用默认
worker_processes 2; //一般跟CPU数保持一致events {
worker_connections 65535; //最大连接数,修改后需要修改变量
}http {
upstream server { //upstream模块,实现负载均衡#ip_hash; //ip不变时后端的真实服务器不变
server 172.25.1.12:80; //后端的真实服务器,可以添加权重,如server 172.25.1.12:80 weight=3;默认为1
server 172.25.1.13:80;
}
include mime.types;
default_type application/octet-stream;sendfile on;
keepalive_timeout 65; //长连接超时时间,单位是秒#gzip on;
server{
listen 80; //监听端口为80,这样访问的时候就不需要输端口,直接输入访问域名就可以
server_name www.westos.org; //访问的域名,可以有多个(用空格隔开)
location / {
proxy_pass http://server; //这里的server与upstream模块那里的名字保持一致
}error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@test1 conf]# vim /etc/security/limits.conf //在限制文件中的末尾行修改最大连接数
[root@test1 conf]# nginx -t //启动nginx前检查配置是否存在问题
[root@test1 conf]# nginx //启动nginx服务
[root@test1 conf]# nginx -s reload //重新加载
realserver端:
[root@test2 ~]# yum install -y httpd //下载http服务
[root@test2 ~]# vim /var/www/html/index.html
[root@test3 ~]# yum install -y httpd //与test2执行一样的动作
[root@test2 ~]# vim /var/www/html/index.html
客户端进行访问:
访问前客户端进行解析
[root@foundation1 ~]# vim /etc/hosts
172.25.1.11 test1 www.westos.org
[root@foundation1 ~]# curl www.westos.org
当test2挂掉时,访问的是服务器test3,没有出现错误,说明nginx自带健康检查的功能。
[root@test2 ~]# /etc/init.d/httpd stop
负载均衡到此也结束啦啦啦。。。接下来是扩展nginx模块
[root@test1 ~]# nginx -s stop //先关闭nginx服务
nginx的默认发布目录在 /usr/local/nginx/html
[root@test1 ~]# cd nginx-1.14.0
[root@test1 nginx-1.14.0]# pwd
/root/nginx-1.14.0
[root@test1 nginx-1.14.0]# ./configure --help //用./configure --help查看有哪些模块
第一种情况:若需要安装的模块在命令下有,则重新编译并添加需要编译的模块
例如:需要安装 --add-module=/data/software/ngx_http_google_filter_module模块,则执行:
[root@test1 ~]# nginx -V //查看已经编译了哪些模块
[root@test1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/data/software/ngx_http_google_filter_module //已编译的和未编译的一块进行编译
[root@test1 nginx-1.14.0]# make //!!!必须要注意:不要make install,不然就被覆盖了
第二种情况:若需要安装的模块在命令下没有,则:
例如:nginx-sticky-module模块的介绍及扩展
sticky模块有什么用呢
使用nginx做负载均衡器时,经常会遇到一个问题,如何将来自同一用户的访问始终定向到一台后端设备进行响应?
nginx一般有两种办法来实现会话保持:
- ip_hash:nginx原生支持的基于IP地址来将不同的请求转发到同一台服务器进行响应,缺点就是如果前端用户都来自同一局域网,基于ip的负载方法会导致负载不均衡;
- sticky:基于cookie来进行负载转发,保证将来自同一cookie的访问始终定向到同一服务器响应,缺点就是需要编译模块,且cookie需要浏览器支持。
现在使用第一种ip_hash:
[root@test1 ~]# nginx -s stop
[root@test1 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@test1 ~]# nginx -t //查看语句是否有问题
[root@test1 ~]# nginx //启动nginx
客户端进行访问:
注意:客户端需要对test1进行解析
[root@foundation1 ~]# for i in {1..5}; do curl www.westos.org; done;
这时只要客户端ip不变,则访问到的真实服务器为同一个,都是test3
我们换个客户端试一试,启动虚拟机test4并将其看作另一个客户端:
先进行解析
[root@test4 ~]# for i in {1..5};do curl www.westos.org;done;
访问到的也一直是test3,到这里我们就可以看出ip_hash的优点和缺点了,第一种情况就完成了。
现在使用第二种sticky模块:
由于上面的nginx版本不支持sticky模块,所以我们换个版本并将上一个版本服务关闭
[root@test1 ~]# nginx -s stop
[root@test1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@test1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@test1 ~]# cd nginx-1.10.1
[root@test1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng //添加模块
由于笔者上面已经在/usr/local/nginx/conf/nginx.conf 下有nginx1.14的配置文件,所以这里更换路径为/opt/nginx,所以配置文件在/opt/nginx/conf下才生效
[root@test1 nginx-1.10.1]# make && make install
[root@test1 nginx-1.10.1]# cd /opt/nginx/conf //配置文件生效的路径
[root@test1 conf]# cp /usr/local/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? yes
//由于上个实验已经配置了负载均衡,所以我们可以将上一个nginx服务的配置文件拷贝过来直接用
[root@test1 conf]# vim nginx.conf //只修改一处就好
[root@test1 conf]# /opt/nginx/sbin/nginx -t
[root@test1 conf]# /opt/nginx/sbin/nginx //检查并启动服务
此时客户端就可以访问啦
客户端访问时,统一主机用命令访问如下:
我们为您可以看到用curl命令访问时并没有实现session共享,访问还是轮询方式,这是为是那么呢
那是因为sticky模式有一个弊端:cookie需要浏览器支持。
我们用浏览器输入www.westos.org试一下:
不管刷新多少回,访问到的始终都是test3
补充一下:
笔者这里用的模块压缩包是nginx-sticky-module-ng.tar.gz大家也可以用nginx-sticky-module-1.1.tar.gz
我个人不太建议用这个压缩包,做的时候可能会有一点问题。
用第二个的话编译可能会报错,如
make[1]: *** [objs/addon/nginx-sticky-module-1.1/ngx_http_sticky_misc.o] Error 1
make[1]: Leaving directory `/root/nginx-1.8.1'
make: *** [build] Error 2
此时修改一下配置文件:
[root@test1 nginx-1.8.1]# pwd
root/nginx-1.8.1
[root@test1 nginx-1.8.1]# vim nginx-sticky-module-1.1/ngx_http_sticky_misc.c //修改第28
281 digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in), digest->data, len, 1);
再次编译成功
[root@test1 nginx-1.8.1]# make && make install