nginx反向代理负载均衡

来源:互联网 发布:python 决策树 编辑:程序博客网 时间:2024/06/05 19:48

           Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而名。2011年6月1日,nginx 1.0.4发布。Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等目前淘宝在nginx做了二次开发:tengine.

     下面介绍一下nginx怎么实现反向代理的。首先环境是基于lnmp搭建的,这里就不过多介绍了,相信大家都知道。现在需要三台虚拟机。一台是代理服务器,还有两台是web服务器。配置很简单,只需要在代理服务器nginx配置文件中加以修改即可,当然原理还是要懂得。下面说一下具体怎么配置的以及一些关键部分。配置文件在/etc/nnginx/nginx.conf,配置文件大致分为三段吧,分别是main,events,http,server。要配置的部分集中在server段。配置如下:

server {
        listen       80;
        server_name  xiaomi.magedu.com;
        root /data/web;
        #charset koi8-r;


        #access_log  logs/host.access.log  main;
          location /mobi/ {                             #定义访问路径            
          proxy_pass http://172.17.160.78/mobile/index.php ;#将/mobi替换为/mobile/index.php,Ip地址为web服务器的地址。
}
          location ^~ /mobile {    模式匹配
          proxy_pass http://172.17.160.78;  #直接传递给/mobile也可以理解为拼接。拼接为一个url.
}
          location / {
          index    index.html index.htm index.php;
        }


       # error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }

上面只是实现反向代理的一种方法。还有其他的,下面介绍淘宝的tengine实现反向代理负载均衡。这是在nginx的基础上加强版的。首先下载tengine-2.1.1.tar.gz,这是比较新版的。然后解压,安装。步骤如下。

tar xvf tengine-2.1.1

cd tengine-2.1.1/

./configure --prefix=/usr/local/tengine 

make && make install

接着启动服务即可。但是这个不能与nginx服务同时启动,所以需要把原来的nginx服务停掉,然后再启动。因为不能使用直接命令启动,需要配置路径,这里就不配置路径,直接使用路径启动。启动方式如下:

/usr/local/tengine/sbin/nginx 

启动之后,就可以在配置文件中对其进行配置,大致和nginx相同,也是有main,events,http,server,不过这里多了新的功能,nginx负载均衡是ngx_http_upstream_module模块的功能, 需要在配置文件http块上下文中定义upstream块, 指定一组负载均衡的后端服务器, 然后在上面讲到的proxy_pass中引用, 就可以反向代理时实现负载均衡了。这里修改了http段和server段的内容。
配置如下:

http {
    include       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;
    tcp_nodelay     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;


    gzip  on;
    upstream server-cluster {     #定义upstream模块,指定负载一组负载均衡的后端服务器
    server 172.17.160.77:80;
    server 172.17.160.78:80;
    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    check_http_send "HEAD / HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}
   upstream static-cluster {     #定义upstream模块,指定负载一组负载均衡的后端服务器
   server 172.17.160.77:80;
   server 172.17.160.78:80;
   check interval=3000 rise=2 fall=5 timeout=1000 type=http;
   # check_http_send "HEAD / HTTP/1.0\r\n\r\n";
   check_http_expect_alive http_2xx http_3xx;
}
   proxy_cache_path /data/cache levels=1:2 keys_zone=web:10m max_size=1G inactive=10m;
server {
        listen       80;
        server_name  xiaomi.magedu.com;
        proxy_set_header Host $host;

        proxy_set_header X-REMODE-IP $remote_addr;
        proxy_set_header  X-Forward-for $proxy_add_x_forwarded_for;
        add_header Magedu-Cache "$upstream_cache_status form $server_addr"  ;
        root /data/web;
        #charset koi8-r;


        #access_log  logs/host.access.log  main;
        #  location /mobi {
        #    proxy_pass http://172.17.160.78/mobile/inex.php;
#}         
        location /stats {
        check_status;
}
       location =/index.php {   #url精确匹配/index.php
       #index index.php;
        proxy_pass http://server-cluster;
}




        location ~* .jpg|.png|.gif|.jpeg$ {     #url后缀匹配
        proxy_cache web;
        proxy_cache_valid 200 303 301 20m;
        proxy_pass http://static-cluster;
}
        location ~* .css|.js|.html|.xml$ {
        proxy_pass http://static-cluster;
}
         #location / {
         # index  index.html index.htm index.php;
         # proxy_pass http://server-cluster;
         # }
          location /youxi/ {
          rewrite ^(.*)$ /index.html break;

          proxy_pass http://server-cluster;
}
         location /mobi/ {
          proxy_pass http://172.17.160.77/mobile/index.php;
}
        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
        }

其他的都和 nginx一样了,在这里我就不解释那么多了。说一下我在做实验的过程中遇到的问题吧,希望对你们也有帮助。

在实现反向代理的时候,做模式匹配,由于匹配有优先级以及格式,所以我在做的过程中,出现了错误。比如在匹配/index.php时,本来应该这样写:location =/index.php,但是我是这样写的:location /index.php,结果就不能匹配。原因:在匹配具体的文件时,应该精确匹配,用=。在匹配路径的时候,可以不用精确匹配。


原创粉丝点击