Nginx之负载均衡

集群

  概念:

  大白话就是:一堆服务器做一件事

  一组若干个相互独立的计算机,利用高速通信网络组成一个较大的计算机服务系统,每个集群节点都是运行各自服务的独立服务器。

  存在地方:可能分布在全国各地。(有点夸张了,全国各地那这个服务系统超级大了)比如谷歌,淘宝,百度服务器,背后估计有成千上万台电脑组成多个集群干多件事情

  为什么要用集群?

  单点计算机很难达到高性能的特点。  

  高性能、价值有效性、可扩展性:如果当服务负载压力增加时,就可以对系统进行扩展,且不会降低服务质量   高可用性:挂了单点计算机,其它的计算机还可以正常工作7*24

负载均衡

  平均分配压力(压力均摊)

  负载均衡抽象理解(图糙理不糙)

  

小黄人好酷,分担压力泽帅

 实验准备 

准备三台计算机  当然我这里无法真正上的测试哈!两台还好计算机还好能扛下来,三台有点难。

nginx1  作为nginx负载均衡器  假如ip为192.168.13.121

nginx2 web服务  提供一个简单的页面 192.168.13.24

nginx3 web服务  提供一个简单的页面  192.168.13.79

主要是配置负载均衡器,要看效果的话可以配置下web服务两个的首页

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 upstream mchotdog{
 34 # 默认我给它轮询算法
 35 server 192.168.13.79;
 36 server 192.168.13.24; 
 37 }
 38     server {
 39         listen       80;
 40 
 41         server_name  192.168.13.121;
 42 
 43         #charset koi8-r;
 44 
 45         #access_log  logs/host.access.log  main;
 46 
 47         location / {
 48            proxy_pass http://mchotdog;
 49            # root   html;
 50            # index  index.html index.htm;
 51         }
 52 
 53         #error_page  404              /404.html;
 54 
 55         # redirect server error pages to the static page /50x.html
 56         #
 57         error_page   500 502 503 504  /50x.html;
 58         location = /50x.html {
 59             root   html;
 60         }
 61 
 62         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 63         #
 64         #location ~ .php$ {
 65         #    proxy_pass   http://127.0.0.1;
 66         #}
 67 
 68         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 69         #
 70         #location ~ .php$ {
 71         #    root           html;
 72         #    fastcgi_pass   127.0.0.1:9000;
 73         #    fastcgi_index  index.php;
 74         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 75         #    include        fastcgi_params;
 76         #}
 77 
 78         # deny access to .htaccess files, if Apache's document root
 79         # concurs with nginx's one
 80         #
 81         #location ~ /.ht {
 82         #    deny  all;
 83         #}
 84     }
 85 
 86 
 87     # another virtual host using mix of IP-, name-, and port-based configuration
 88     #
 89     #server {
 90     #    listen       8000;
 91     #    listen       somename:8080;
 92     #    server_name  somename  alias  another.alias;
 93 
 94     #    location / {
 95     #        root   html;
 96     #        index  index.html index.htm;
 97     #    }
 98     #}
 99 
100 
101     # HTTPS server
102     #
103     #server {
104     #    listen       443 ssl;
105     #    server_name  localhost;
106 
107     #    ssl_certificate      cert.pem;
108     #    ssl_certificate_key  cert.key;
109 
110     #    ssl_session_cache    shared:SSL:1m;
111     #    ssl_session_timeout  5m;
112 
113     #    ssl_ciphers  HIGH:!aNULL:!MD5;
114     #    ssl_prefer_server_ciphers  on;
115 
116     #    location / {
117     #        root   html;
118     #        index  index.html index.htm;
119     #    }
120     #}
121 
122 }

 最好先测试下再启动nginx

1 调度算法      
2 轮询        按时间顺序逐一分配到不同的后端服务器(默认)
3 weight       加权轮询,weight值越大,分配到的访问几率越高
4 ip_hash      每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
5 url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
6 least_conn    最少链接数,那个机器链接数少就分发

 在这里可以看下我主配置文件解读的配置比较全面

原文地址:https://www.cnblogs.com/Alexephor/p/11370489.html