Nginx做负载均衡实例

标签: nginx 负载均衡 当架构大型应用时,PHP架构师不得不考虑WEB服务器,文件服务器,缓存服务器的选择
服务器文档

Nginx做负载均衡实例

帖子NT流人 于 2009年 12月 15日 10:40

 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP。

  用户访问http://www.s135.com,将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四台服务器。

  用户访问http://blog.s135.com,将其负载均衡到192.168.1.7服务器的8080、8081、8082端口。

  以下为配置文件nginx.conf:

引用
user www www;

worker_processes 10;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

#最大文件描述符
worker_rlimit_nofile 51200;

events
{
use epoll;

worker_connections 51200;
}

http
{
include conf/mime.types;
default_type application/octet-stream;

keepalive_timeout 120;

tcp_nodelay on;

upstream http://www.s135.com {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}

upstream blog.s135.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}

server
{
listen 80;
server_name http://www.s135.com;

location / {
proxy_pass http://www.s135.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

log_format www_s135_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/www.log www_s135_com;
}

server
{
listen 80;
server_name blog.s135.com;

location / {
proxy_pass http://blog.s135.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

log_format blog_s135_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/blog.log blog_s135_com;
}
}
在指尖流浪
1. Everything changes and ends. 所有的事情在变化,都有终结
2. Things do not always go according to plan. 事情总会出乎意料(计划)之外
3. Life is not always fair. 生活并不总是公平
4. Pain is part of life. 痛苦是生活的一部分
5. People are not loving and loyal all the time. 人们并不总是热爱和忠诚
头像
NT流人
网站管理员
 
帖子: 744
加入时间: 2008年 1月 2日 13:15

Re: Nginx做负载均衡实例

帖子NT流人 于 2009年 12月 15日 10:41

1、如果均衡的域名中有泛域名,或有几百个域名的话可以配置吗?
2、ngnix在做均衡的时候是否已经包含了cache功能?
3、ngnix---squid----apache取的的HTTP_X_FORWARDED_FOR是squid服务器的ip地址,如果想取真实客户端地址可以做到吗?

1、nginx.conf
server_name .s135.com; 即可支持***.s135.com泛域名

2、nginx负载均衡只做反向代理,有简单的缓冲,但不像Squid那样将cache存在本机。

3、我已经在nginx.conf配置文件中增加:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

编译squid时加上--enable-follow-x-forwarded-for
然后在squid.conf中输入一行:
follow_x_forwarded_for allow all

后端的Apache取日志(httpd.conf):
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %h %T" combined
取到的就是用户真实IP
在指尖流浪
1. Everything changes and ends. 所有的事情在变化,都有终结
2. Things do not always go according to plan. 事情总会出乎意料(计划)之外
3. Life is not always fair. 生活并不总是公平
4. Pain is part of life. 痛苦是生活的一部分
5. People are not loving and loyal all the time. 人们并不总是热爱和忠诚
头像
NT流人
网站管理员
 
帖子: 744
加入时间: 2008年 1月 2日 13:15

Re: Nginx做负载均衡实例

帖子NT流人 于 2009年 12月 15日 10:47

nginx是什么?
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

首先是配置十分的简单,而且功能非常强大。真是相见恨晚。
先来看看配置文件怎么写吧

worker_processes 1;
events {
worker_connections 1024;
}
http{
upstream myproject {
#这里指定多个源服务器,ip:端口,80端口的话可写可不写
server 192.168.43.158:80;
server 192.168.41.167;
}

server {
listen 8080;
location / {
proxy_pass http://myproject;
}
}
}

nginx的负载均衡有哪些功能呢?
  • 如果后面的服务器其中一台坏了,它能自动识别,更牛的是它好了之后nginx可以马上识别
  • 服务器A和B,如果A的响应时间为3,B的响应时间为1,那么nginx会自动调整访问B的概率是A的3倍,真正做到负载均衡

在这里还是想说说nginx的安装及运行
先到http://www.nginx.net/下载最新的源码包。
我下载到的是nginx-0.5.33.tar.gz
解压:tar zxvf nginx-0.5.33.tar.gz
接着:./configure
再接着:make
最后:make install
好的,安装完成了。我在make的时候报了个错,说HTTP Rewrite 模块 有问题,我就./configure --without-http_rewrite_module
然后再make,make install就可以了。
安装好了之后新建一个配置文件,把上面的配置文件内容拷进去,当然要修改你的IP,保存为比如 load_balance.conf

然后启动:
/usr/local/nginx/sbin/nginx -c load_balence.conf

如果上面的步骤走下来有问题的话,可以参考:
nginx的中文维基
http://wiki.codemongers.com/NginxChs
当然也可以到官方网站
http://www.nginx.net
http://www.nginx.com

由于nginx的作者是俄国人,所以英文的文档也不是那么完善,对于我来说nginx的最大优点还是配置简单,功能强大
我曾经配过 apache-jk,那真的不是一般人能配的。太复杂了,而且只能用来做tomcat的负载均衡。
nginx就没有这个限制,对它来说后面是什么服务器是完全透名的。
在指尖流浪
1. Everything changes and ends. 所有的事情在变化,都有终结
2. Things do not always go according to plan. 事情总会出乎意料(计划)之外
3. Life is not always fair. 生活并不总是公平
4. Pain is part of life. 痛苦是生活的一部分
5. People are not loving and loyal all the time. 人们并不总是热爱和忠诚
头像
NT流人
网站管理员
 
帖子: 744
加入时间: 2008年 1月 2日 13:15

Re: Nginx做负载均衡实例

帖子xiaokai 于 2010年 6月 4日 09:28

能设置负载比例吗?
在路上..
头像
xiaokai
 
帖子: 18
加入时间: 2010年 5月 31日 19:08

Re: Nginx做负载均衡实例

帖子NT流人 于 2010年 6月 7日 11:05

可以,upstream 中的weight就是权重
在指尖流浪
1. Everything changes and ends. 所有的事情在变化,都有终结
2. Things do not always go according to plan. 事情总会出乎意料(计划)之外
3. Life is not always fair. 生活并不总是公平
4. Pain is part of life. 痛苦是生活的一部分
5. People are not loving and loyal all the time. 人们并不总是热爱和忠诚
头像
NT流人
网站管理员
 
帖子: 744
加入时间: 2008年 1月 2日 13:15

Re: Nginx做负载均衡实例

帖子xiaokai 于 2010年 7月 9日 17:46

哦哦, 了解..
在路上..
头像
xiaokai
 
帖子: 18
加入时间: 2010年 5月 31日 19:08


回到 服务器文档

在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客

cron