Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9170261
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: 系统运维

2012-02-01 16:18:42

Nginx 负载均衡与location应用分析
[日期:2012-01-31] 来源:Linux社区  作者:yeelone
 
 
目前正在学Nginx服务器的相关知识。以下是我的学习记录。
 文章重点记录nginx负载均衡。
 
 为了实验,我习惯性的先上拓扑图,如下:
 
 
 
 
 

我们的案例需求如下,前端nginx做负载均衡,并处理静态页面,使用location查询过滤将动态页面交由后端apache服务器集群做处理。并由nginx回显内容输出。
 
nginx负载均衡服务器安装配置:
 
 
 
[root@lvs /]# rpm -qa | grep pcre pcre-devel-6.6-2.el5_1.7 pcre-6.6-2.el5_1.7  # pcre的作用是为nginx提供兼容perl的正则表达式,使nginx支持HTTP Rewrite模块. 
[root@lvs ~]# wget
[root@lvs nginx-0.8.53]# ./configure  --with-http_stub_status_module
[root@lvs nginx-0.8.53]# make && make install# --with-http_stub_status_module 可以用来启用nginx的nginxStatus功能,以监控nginx当前状态。
 
在继续配置之前,需要先了解我们要做什么。我只想做个基本的实验来看下nginx的负载均衡是如何工作的,所以我的实验设计是这样的。
 
当我们访问192.168.182.131/index.html 时,由于是静态页面,交给nginx本身处理,如果我们访问 192.168.182.131/test.php时,配置location匹配 .php 后缀,并交由后端apache集群来处理,至于集群会以什么样的方式来处理,后面用到时再说。
 
 
 
[root@lvs conf]# pwd
/usr/local/nginx/conf
[root@lvs conf]# vim nginx.conf  user  nobody; worker_processes  1;  # 指定nginx要开启的进程数,建议一个CPU的内核就处理一个进程,所以如果是4核CPU,就指定4个进程数。 
events { 
        use epoll;
         worker_connections  1024;
}
# use epoll 是指nginx的工作模式,epoll是比较高效的工作模式,对于 #linux/unix平台,epoll是首选模式。
# worker_connections 是一个进程的最大连接数。由此即可算出最大的客户端数量:
max_client = worker_processes * worker_connections; 
http {
    server {
     listen              80;
     server_name         192.168.182.131;
     index               index.html; 
    root                /usr/local/nginx/html;
     charset             gb2312; 
    upstream MyServer { 
            ip_hash;
             server 192.168.182.132:80;
             server 192.168.182.133:80; 
         }           location ~* .*.php$ {
              proxy_pass          ; 
     }
       }
 }
负载均衡的配置是这里:
 
 
 
    upstream MyServer {
            ip_hash;
            server 192.168.182.132:80;
            server 192.168.182.133:80;
         }   
 
这里有个地方需要注意,ip_hash是什么呢?上面我说的要求我们再看一次:当我们访问192.168.182.131/index.html 时,由于是静态页面,交给nginx本身处理,如果我们访问 192.168.182.131/test.php时,配置location匹配 .php 后缀,并交由后端apache集群来处理,至于集群会以什么样的方式来处理,后面用到时再说。
 
 
 
ip_hash就是upstream的调度算法之一,以下列举nginx所支持的常用的3种调度算法:  ip_hash: 每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。  轮询: 每个请求按访问顺序分配不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响 。  weight: 指定轮询权值,weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下。 而location的应用,案例中只举了很简单的用法:
 
 
 
  location ~* .*.php$ {
             proxy_pass          ;
     }  
 
首先应用了解location的几个匹配符:
1 = 严格匹配这个查询。如果找到,停止搜索
2 ^~ 匹配路径的前缀,如果找到,停止搜索
3 ~  为区分大小写的正则匹配
4 ~* 不区分大小写的正则匹配 
所以,代码中 location ~* .*.php$ 的意思就是,将.php结尾的url链接,发送到后端服务器去处理,让后端去处理。关于location的更深入理解可以看这篇文章: http://blog.csdn.net/fengmo_q/article/details/6683377 
配置后端apache集群服务器:
 
 
 
192.168.182.132 的配置如下:
 [root@lvs3 ~]# yum install httpd -y
[root@lvs3 ~]# yum install php -y
 
不做配置
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ls test.php
以下是test.php的内容:
[root@localhost html]# cat test.php 
print "192.168.182.132";
?>
[root@localhost html]#   
 
 192.168.182.133 的配置如下:
[root@lvs3 ~]# yum install httpd -y
[root@lvs3 ~]# yum install php -y
不做配置
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ls test.php
以下是test.php的内容:
[root@localhost html]# cat test.php 
print "192.168.182.133";
?>
[root@localhost html]#  整天过程就这样完成了。算是了解了nginx的负载均衡的大致过程 。以后再深入学习。这篇学习笔记就到此为止啦。
 

 
阅读(960) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~