惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
T
Tenable Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
S
Secure Thoughts
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
MIT News - Artificial intelligence
T
Tor Project blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
D
Docker
J
Java Code Geeks
GbyAI
GbyAI
H
Heimdal Security Blog
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Register - Security
The Register - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - Hicome

CentOS6 安装svn Nagios的配置文件 ZT 创建 PHP 测试页 ZT 命令行手工备份Ubuntu系统的方法 还原Ubuntu系统备份的方法 ZT SHELL编程实例+条件判断总结 ZT DELL服务器结合nagios硬件监控、报警 ZT CentOS下内存使用率查看 ZT swf文件格式解析入门(文件头解析)ZT 怎样压缩swf文件? ZT 使用NDOUtils将Nagios监控信息存入数据库 ZT 电脑自动重启的原因几处理方法 ZT 通过rpm包安装、配置及卸载mysql ZT DVD+R与DVD-R有什么区别 ZT CentOS 5.5安装apache2.2.17 ZT CentOS 卸载apache ZT Trac 下设置发邮件问题 Centos 下PHP的卸载与安装 ZT mysql的一些操作命令 【M8】使用的一些小技巧 电话,短信,联系人,音乐等功能
Nginx负载均衡 ZT
Hicome · 2011-02-21 · via 博客园 - Hicome

Nginx负载均衡基础知识

Nginx的upstream目前支持5种方式的分配

1)、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2)、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2)、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

3)、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4)、url_hash(第三方)

Nginx 负载均衡实例1

upstream bbs.linuxtone.org {#定义负载均衡设备的Ip及设备状态

   server 127.0.0.1:9090 down;

   server 127.0.0.1:8080 weight=2;

   server 127.0.0.1:6060;

   server 127.0.0.1:7070 backup;

}

在需要使用负载均衡的server中增加

proxy_pass http://bbs.linuxtone.org/;

每个设备的状态设置为:

a)down 表示单前的server暂时不参与负载

b)weight 默认为1.weight越大,负载的权重就越大。

c)max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

d)fail_timeout:max_fails次失败后,暂停的时间。

e)backup:其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug client_body_temp_path 设置记录文件的目录 可以设置最多3层目录 location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

Nginx 负载均衡实例2

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,也可以用作提高Squid缓存命中率.

简单的负载均等实例:

#vi nginx.conf  //nginx主配置文件核心配置

……….

#loadblance my.linuxtone.org

      upstream  my.linuxtone.org  {

      ip_hash;

      server   127.0.0.1:8080;

      server   192.168.169.136:8080;

      server   219.101.75.138:8080;

      server   192.168.169.117;

      server   192.168.169.118;

      server   192.168.169.119;

    }

…………..

include          vhosts/linuxtone_lb.conf;

………

# vi proxy.conf

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 50m;

client_body_buffer_size 256k;

proxy_connect_timeout 30;

proxy_send_timeout 30;

proxy_read_timeout 60;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

proxy_max_temp_file_size 128m;

proxy_store on;

proxy_store_access   user:rw  group:rw  all:r;

#nginx cache               

#client_body_temp_path  /data/nginx_cache/client_body 1 2;

proxy_temp_path /data/nginx_cache/proxy_temp 1 2;

#vi  linuxtone_lb.conf

server

   {

       listen  80;

       server_name my.linuxtone.org;

       index index.php;

       root /data/www/wwwroot/mylinuxtone;

       if (-f $request_filename) {

           break;

          }

       if (-f $request_filename/index.php) {

         rewrite (.*) $1/index.php break;

       }

       error_page 403 http://my.linuxtone.org/member.php?m=user&a=login;

       location / {

          if ( !-e $request_filename) {

              proxy_pass http://my.linuxtone.org;

              break;

          }

          include /usr/local/nginx/conf/proxy.conf;

       }

}

=====

动态页面请求处理:

Nginx 本身并不支持现在流行的 JSP、ASP、PHP、PERL 等动态页面,但是它可以通过反向代理将请求发送到后端的服务器,

例如 Tomcat、Apache、IIS 等来完成动态页面的请求处理。前面的配置示例中,我们首先定义了由 Nginx 直接处理的一些静态文件请求后,

其他所有的请求通过 proxy_pass 指令传送给后端的服务器(在上述例子中是 Tomcat)。最简单的 proxy_pass 用法如下:

    location / {

         proxy_pass http://localhost:8080;

         proxy_set_header X-Real-IP $remote_addr;

 }

在本例中我们配置的是2台机器的负载均衡 所以我们的配置是 :

location /they{

        proxy_pass http://mao;# 反向代理

        #include proxy.conf;

        }

其中 mao 是我们用upstream 来定义的 例如:

upstream mao{

           server 192.168.0.208:8080 weight=2;

           server 192.168.0.235 weight=3;

     }

在 Nginx 的集群配置中,Nginx 使用最简单的平均分配规则给集群中的每个节点分配请求。

一旦某个节点失效时,或者重新起效时,Nginx 都会非常及时的处理状态的变化,以保证不会影响到用户的访问。 

转自: 

http://www.zzbaike.com/wiki/Nginx%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1

http://blog.sina.com.cn/s/blog_59833db10100g453.html