nginx 负载均衡 反向代理

nginx 通过方向代理实现负载均衡,负载均衡是大流量网站要做的措施,单从字面上的意思来理解为N台服务器平均分担负载,不会因为某一台服务器负载高宕机而影响用户访问网站,负载均衡至少需要三台服务器,

既然是负载均衡,分摊服务器压力,那么一台服务器最好就负责一个网站。所以只需要配置一个server即可

1.首先是主服务器:

        upstream xxx.xxx.com{
                server 111.111.111.111:80 weight=2;#权重为2/5
                server 222.222.222.222:80 weight=3;#权重为3/5
        }
        server{
                listen 80;
                server_name xxx.xxx.com;
                location / {
                        proxy_pass http://xxx.xxx.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;
                }
        }

2.两台从服务器

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root         /var/www/html;
        index   index.html index.php;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

 负载均衡4种策略

    • 轮询:默认
          • 每个请求按照时间顺序逐一分配到不同的后台服务器,如果后台当机了,自动剔除 
            upstream xxx.xxx.com{
              server 111.111.111.111:80 fail_timeout=20s;
              
              server 222.222.222.222:80 fail_timeout=20s;
              server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
            }
    • 指定权重
            • 指定轮询纪律,weight和访问比例成正比。用于后段服务器性能不均的情况
              upstream xxx.xxx.com{  
                server 111.111.111.111:80 weight=2 fail_timeout=20s;

                server 222.222.222.222:80 weight=3 fail_timeout=20s;
                server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
              }
    • IP绑定 ip_hash
            • 每个请求按访问ip的hash结果分配,这样每个访问一个后段服务器。解决了session问题
              upstream xxx.xxxxcom{
                server 111.111.111:80 fail_timeout=20s;
                server 222.222.222:80 fail_timeout=20s;
                server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)  ip_hash; }
    • fair(第三方)
            •  按照后段服务器的响应时间来分配请求,响应时间最短的优先分配
              upstream xxx.xxx.comr { 
                  server 111.111.111:80 fail_timeout=20; 
                  server 222.222.222:80 fail_timeout=20; 
                server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)  fair; }

               

注意

systemctl restart nginx时失败;原因查看systemctl status nginx;

配置默认访问文件之间使用空格分开 index index.html index.php

配置的前面和中间的尽量使用一个空格,或者一个tal键。

当启动正常时,但访问出现500错误,可能是location / {中出现错误}

posted @ 2017-01-13 20:07  jackylee92  阅读(231)  评论(0编辑  收藏  举报