本文共 3065 字,大约阅读时间需要 10 分钟。
安装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