码迷,mamicode.com
首页 > 其他好文 > 详细

nginx 反向代理和服务器组状态

时间:2014-12-02 00:22:59      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:nginx反向代理

实现NGINX的代理(七层负载均衡);



拓扑图:

web01            web02

|192.168.1.20     |192.168.1.21

-----------------------------

     |192.168.1.254

     nginx(反向代理);

|1.1.1.254

|

client(1.1.1.1);



要求:client1.1.1.1 可以通过反向代理1.1.1.254 访问到内网的两台web服务;web01  性能比较好,权值为二;



**由于是资源有限,用IP虚拟主机实现两台web;(web服务器apache);



实现过程:

1.首先配置出两台web服务器;

[root@localhost local]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:07:6D:A0  

          inet addr:192.168.1.20  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe07:6da0/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:30 errors:0 dropped:0 overruns:0 frame:0

          TX packets:31 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:4318 (4.2 KiB)  TX bytes:5539 (5.4 KiB)


eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:07:6D:A0  

          inet addr:192.168.1.21  Bcast:192.168.255.247  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


配置虚拟主机:

[root@localhost conf.d]# vim VirtualHost.conf 

<VirtualHost 192.168.1.20:80>

    

    DocumentRoot /www

    ServerName www.bxn.com

    CustomLog logs/www20-access_log common

</VirtualHost>

<VirtualHost 192.168.1.21:80>

    DocumentRoot /bbs

    ServerName bbs.bxn.com

    CustomLog logs/bbs21-access_log common

</VirtualHost>


[root@localhost bbs]# /etc/init.d/httpd restart


[root@localhost bbs]# elinks --dump 192.168.1.20

   192.168.1.20:www

[root@localhost bbs]# elinks --dump 192.168.1.21

   192.168.1.21:bbs


web服务配置完毕;



2.配置反向代理:

安装NGINX过程:源码安装:简略写;

groupadd nginx

useradd -M -g nginx nginx

yum install -y pcre pcre-devel

yum install -y openssl openssl-devel

./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx --with-http_stub_status_module

make && make install



反向代理配置:

[root@squid nginx]# cd /usr/local/nginx/conf/


[root@squid conf]# grep -v ‘#\|^$‘ nginx.conf >nginx2.conf


[root@squid conf]# vim nginx2.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

        

upstream webgrp {

        server 192.168.1.20 weight=2;

        server 192.168.1.10 weight=1;

        }

//upstream  是关键;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

                proxy_pass http://webgrp//还有这;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}



[root@squid sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx2.conf


[root@squid conf]# netstat -tulnp |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2590/nginx


3.在client  1.1.1.1 测试;

 elinks 1.1.1.254[root@localhost ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:19:1B:0D

          inet addr:1.1.1.1  Bcast:1.1.1.255  Mask:255.255.255.0



[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.21:bbs

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.21:bbs

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

在client可以看到已经测试成功;



*知识点:

服务器组的连接状态: 决定连接请求分发方式:


轮询:nginx默认的连接请求分发方式;

ip_hash:同一客户端连接时,只连接同一个服务器;(解决了session问题);


weight :权重:按照权重比例分发请求;

fair:谁连接请求处理的快,发给谁,默认是不支持的(需要安装第三方软件);


 使用IP_HASH方式:


配置:

 upstream webgrp {

        ip_hash;

        server 192.168.1.20 ;

        server 192.168.1.21 ;

        }


测试:

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

[root@localhost ~]# elinks --dump 1.1.1.254

   192.168.1.20:www

**可以看到都每次访问的都是同一个web服务器;







本文出自 “9379746” 博客,请务必保留此出处http://9389746.blog.51cto.com/9379746/1585185

nginx 反向代理和服务器组状态

标签:nginx反向代理

原文地址:http://9389746.blog.51cto.com/9379746/1585185

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!