Interesting things
这将会是关于FastDFS蛋疼的集群和负载均衡系列收官之篇,不排除有总结篇!这篇就是将keepalived实现的nginx集群高可用应用到我们的FastDFS集群中。
What did you do today
- 我们需要修改192.168.12.77和192.168.12.88设备上的nginx.conf,配置如下所示:
#user nobody; worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
cmazxiaoma is handsomeworker_connections 1024;
}
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream fastdfs_tracker {
server 192.168.12.11:8000 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.12.22:8000 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
location /fastdfs {
root html;
index index.html index.htm;
proxy_pass http://fastdfs_tracker/;
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 300m;
}
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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
* 我们对配置做了一些修改。
* 一处是添加了负载均衡upstream fastdfs_tracker.把192.168.12.11和192.168.12.22两台设备作为tracker,现在我们集群了一层nginx来代理2台tracker
upstream fastdfs_tracker {
server 192.168.12.11:8000 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.12.22:8000 weight=1 max_fails=2 fail_timeout=30s;
}
* 另一处修改是添加了一个location,匹配路径中含有fastdfs。
* 我们再上传一张图片。上传成功返回group1/M00/00/00/wKgMIVpE8IKAWxlrAADRd6mMX3g207.jpg
![上传图片](http://upload-images.jianshu.io/upload_images/4636177-4fd69861aca61594.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 我们试着访问http://192.168.12.111/fastdfs/group1/M00/00/00/wKgMIVpE8IKAWxlrAADRd6mMX3g207.jpg,完美!
![我爱五月天](http://upload-images.jianshu.io/upload_images/4636177-07147ed46d678851.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
***
###Summary
> FastDFS负载均衡和集群终于搞定了。
1.虚拟ip 192.168.12.111提供入口
2.一层Nginx集群负载均衡(192.168.12.77、192.168.12.88)
3.Tracker+Nginx负载均衡(192.168.12.11)
4.Tracker + Nginx负债均衡(192.168.12.22)
5.Group1包含Stroage+Nginx(192.168.12.33), Storage+Nginx(192.168.12.44)
6.Group2包含Stroage+Nginx(192.168.12.55), Storage+Nginx(192.168.12.66)
一共2台tracker、4台storage、8台nginx。
###What to do tomorrow
> 写一篇关于FastDFS集群和负载均衡的总结。这个星期把Spring Security和二叉树两篇博客搞定,预习Zookeeper。下一个星期把Zookeeper撸一遍。