Nginx 负载均衡 配置全过程

Nginx平台搭建:Nginx+mysql+php-fpm搭建高性能Nginx平台

前端Nginx:192.168.93.137

后端web1:192.168.93.138

后端web2:192.168.93.139

前端nginx配置:

http {
      ……
        client_max_body_size 300m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 16k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;

       upstream  www.lihuipeng.com  {
               #server   192.168.93.137:80;
               server   192.168.93.138:80;
               server   192.168.93.139:80;
       }


       upstream 
www.lihuipeng007.com  {
               #server   192.168.93.137:80;
               server   192.168.93.138:80;
               server   192.168.93.139:80;
       }


      server
       {
               listen  80;
               server_name 
www.lihuipeng.com;

               location / {
                        proxy_pass       
http://www.lihuipeng.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  lihuipeng  '$remote_addr - $remote_user [$time_local] $request '
                                 '"$status" $body_bytes_sent "$http_referer" '
                                 '"$http_user_agent" "$http_x_forwarded_for"';
               access_log  /home/logs/www.lihuipeng.log  lihuipeng;
       }

      server
       {
               listen  80;
               server_name 
www.lihuipeng007.com;

               location / {
                        proxy_pass       
http://www.lihuipeng007.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  lihuipeng007  '$remote_addr - $remote_user [$time_local] $request '
                                 '"$status" $body_bytes_sent "$http_referer" '
                                 '"$http_user_agent" "$http_x_forwarded_for"';
               access_log  /home/logs/www.lihuipeng.log  lihuipeng007;
       }

}

通过upstream  名字 {}定义后端web的负载机器,然后在虚拟主机中通过 proxy_pass http://名字; 来使用upstream,再自定义一下日志格式,以获取用户的IP

后端web配置:

         server
        {
                listen       80;
                server_name www.lihuipeng.com;
                index index.html index.php;
                root  /home/www/www.lihuipeng.com;
                access_log  /home/logs/access_www.lihuipeng.com.log;

                if (-d $request_filename){
                        rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
                }

                error_page   500 502 503 504 404 403 http://www.lihuipeng.com;

                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                        expires 30d;
                }

                location ~ .*\.(js|css)?$ {
                        expires 6h;
                }

                location ~ .*\.(log|txt)$
                {
                        deny all;
                }

                location ~ .*\.(php)?$
                {
                        fastcgi_pass  127.0.0.1:9000;
                        fastcgi_index index.php;
                        include fcgi.conf;
                }
        }
 

        server
        {
                listen       80;
                server_name www.lihuipeng007.com;
                index index.html index.php;
                root  /home/www/www.lihuipeng007.com;
                access_log  /home/logs/access_www.lihuipeng007.com.log;

                if (-d $request_filename){
                        rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
                }

                error_page   500 502 503 504 404 403 http://www.lihuipeng007.com;

                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                        expires 30d;
                }

                location ~ .*\.(js|css)?$ {
                        expires 6h;
                }

                location ~ .*\.(log|txt)$
                {
                        deny all;
                }

                location ~ .*\.(php)?$
                {
                        fastcgi_pass  127.0.0.1:9000;
                        fastcgi_index index.php;
                        include fcgi.conf;
                }
        }

这样一个简单的nginx负载均衡就完成!

PS:Nginx负载的五种模式,也就是upstream的模式

1 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自
动剔除。
2 weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

upstream www.lihuipeng.com {
server 192.168.93.138 weight=10;
server 192.168.93.139 weight=10;
}

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

upstream www.lihuipeng.com{
ip_hash;
server 192.168.93.138:80;
server 192.168.93.139:80;
}

4 fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5 url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务
器为缓存时比较有效。

upstream www.lihuipeng.com{
server 192.168.93.138:80;
server 192.168.93.139:80;
hash $request_uri;
hash_method crc32;
}

用得比较多得应该是第3、5这两种吧!

后端的web文件同步有很多选择了,可以做nfs、rsync等!

你可能感兴趣的:(nginx,负载均衡,职场,休闲)