负载均衡1.1

最近有个小伙伴问我,大数据用什么框架好?
我说:分布式+负载均衡支持百万级数据!
小伙伴:负载均衡我不会!
因此我就考虑要不要写个博客帮助类似那个小伙伴的朋友!
好了,废话不多说了,正式讲述代码!


前提准备:
maven
tomcat
nginx
memcached


第一步,配置nginx代理

首先,下载nginx解压,放在D盘,没有D盘的朋友可以放在其他空闲盘,
注意:路径不能存在中文

然后,打开解压文件,找到conf—》nginx.conf,用Notepad ++打开


#user  nobody;
worker_processes  2;  

#错误日志存放路径  
#error_log  logs/error.log;  
#error_log  logs/error.log  notice;  
error_log  logs/error.log  info; 

#指定pid存放文件  
pid        logs/nginx.pid; 


events {  
    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
    #use epoll;  

    #允许最大连接数  
    worker_connections  2048;  
} 


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

        #定义日志格式  
    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  
    #                  '"$status" $body_bytes_sent "$http_referer" '  
    #                  '"$http_user_agent" "$http_x_forwarded_for"';  

    #access_log  off;  
    access_log  logs/access.log;  

    client_header_timeout  3m;  
    client_body_timeout    3m;  
    send_timeout           3m;  

    client_header_buffer_size    1k;  
    large_client_header_buffers  4 4k;  

    sendfile        on;  
    tcp_nopush      on;  
    tcp_nodelay     on;  

    #keepalive_timeout  75 20;  

    include    gzip.conf;

    #负载均衡配置,#server localhost:8082; 
    upstream localhost {  
      #同一机器在多网情况下,路由切换,ip可能不同  
      #ip_hash;
      server 192.168.3.249:8080;
      server 192.168.3.249:8081; 
      server 192.168.3.249:8082;      
     }

    #代理web服务
    server {  
           listen       80;  
           server_name  localhost;   
            charset utf-8;  
            location / {
                root   html;
                index  index.html index.htm index.jsp;
                proxy_pass http://localhost/;
                proxy_set_header  X-Real-IP  $remote_addr;  
                client_max_body_size  100m;
            }
            location ~ ^/(WEB-INF)/ {   
                deny all;   
            }   
            error_page   500 502 503 504  /50x.html;  
            location = /50x.html {  
                root   html;  
            }  
   }

    # linux文件服务器
    server {
        listen       8090;
        server_name  localhost;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html)$ {
            root  /opt/job/ftp;
            index  index.html index.htm;
            expires 30d;
        }

        location / {
            root     /home/www/cms/uploads;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    # windows 文件服务器配置
    server {
        listen       8089;#端口号
        server_name  localhost;#本机

        charset utf-8;

        #access_log  logs/host.access.log  main;

    location ~ .*\.(gif|jpg|jpeg|png)$ {
        expires 24h;
            root D:/resourcesfile/images/;#指定图片存放路径
            access_log D:/resourcesfile/img_nginx.log;#图片路径
            proxy_store on;
            proxy_store_access user:rw group:rw all:rw;
            proxy_temp_path         D:/resourcesfile/images/;#图片路径
            proxy_redirect          off;

            proxy_set_header        Host 127.0.0.1;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    10m;
            client_body_buffer_size 1280k;
            proxy_connect_timeout   900;
            proxy_send_timeout      900;
            proxy_read_timeout      900;
            proxy_buffer_size       40k;
            proxy_buffers           40 320k;
            proxy_busy_buffers_size 640k;
            proxy_temp_file_write_size 640k;
            if ( !-e $request_filename)
            {
                 proxy_pass  http://127.0.0.1:8089;#代理访问地址
            }
    }  

        location / {
            root   html;
            index  index.html index.htm;

        }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}  

测试nginx是否配置成功

window+R,打开命令窗口
d: 开启盘符
start nginx 开启nginx
cd D:\nginx\nginx-1.6.3 找到nginx所在路径
nginx -t 测试nginx是否成功
nginx -s reload 重启nginx
nginx -c D:\nginx\nginx-1.6.3\conf\nginx.conf 刷新nginx.conf
这里写图片描述


解决80端口被占用
window+R,打开命令窗口
regedit打开注册表
HKEY_LOCAL_MACHINE
SYSTEM
CurrentControSet
Services
HTTP
Start
3改为0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值