nginx学习负载均衡一

相关环境
192.168.3.40(apache)已发布
192.168.3.49(nginx)已发布
192.168.3.52(elb)


1、相关依赖包安装
yum update -y
yum -y install gcc gcc+ gcc-c++  pcre pcre-devel zlib-devel openssl openssl-devel wget vim tree lsof


2、建立nginx用户及组和软件目录
groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx
mkdir -p /data /data/nginx1.6.2
cd /data

3、下载软件并安装
[root@elb data]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@elb data]# ls
nginx-1.6.2.tar.gz
[root@elb data]# pwd
/data
[root@elb data]# tar -zxvf nginx-1.6.2.tar.gz
[root@elb nginx-1.6.2]# ./configure --help | grep upstrea    默认就安装了
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
[root@elb nginx-1.6.2]# ./configure --user=nginx --group=nginx --prefix=/data/nginx1.6.2 --with-http_stub_status_module --with-http_ssl_module
[root@elb nginx-1.6.2]#make && make install
[root@elb nginx-1.6.2]# echo $?
0
[root@elb nginx-1.6.2]#ln -s /data/nginx1.6.2 /data/nginx
[root@elb data]# cd nginx
[root@elb nginx]# ls
conf  html  logs  sbin
[root@elb sbin]# lsof -i :80
COMMAND     PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
clock-app  2057  root   21u  IPv4 121124      0t0  TCP 192.168.3.52:49828->42.99.254.162:http (CLOSE_WAIT)
nginx     43511  root    6u  IPv4 158444      0t0  TCP *:http (LISTEN)
nginx     43512 nginx    6u  IPv4 158444      0t0  TCP *:http (LISTEN
[root@elb nginx]# cd conf/
[root@elb conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default


[root@elb conf]# egrep -v "#|~$" nginx.conf.default >nginx.conf 
[root@elb conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }


}
官方文档
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
Syntax: upstream name { ... }
Default:
Context: http


将以下这段放入http标签
upstream backend {
    server backend1.example.com weight=5;                                   域名
    server 127.0.0.1:8080       max_fails=3(检查web server3次失败踢出去) fail_timeout=30s(超时时间);                
    server unix:/tmp/backend3;                                              socket                 

    server backup1.example.com  backup;                                     备份的主机    
}


[root@elb conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream backend {
    server 192.168.3.40:80      max_fails=3 fail_timeout=30s;
    server 192.168.3.49:80      max_fails=3 fail_timeout=30s;
}
    server {
        listen       80;
        server_name  www.wolf.com;
        index  index.html index.htm; 
        location / {
           proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

[root@elb conf]# ../sbin/nginx -t
nginx: the configuration file /data/nginx1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx1.6.2/conf/nginx.conf test is successful
[root@elb conf]# ../sbin/nginx -s reload


负载均衡测试~
1、不同的客户端ie打开
2、脚本curl测试                    
[root@elb conf]# for n in `seq 100`;do curl 192.168.3.52;sleep 2;done
www.wolf.org
www.wolf.com
www.wolf.org
www.wolf.com































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值