因爲博客圖片量大,篇幅太長,所以須要分上,中,下三篇博客來寫,上篇和中篇咱們已經一塊兒學習完了,這篇博客咱們一塊兒學習下剩餘部分。php
一,配置反向代理html
咱們須要在兩個跟蹤器上安裝的nginx(也就是192.168.156.5和192.168.156.6)以提供反向代理服務,目的是使用統一的一個IP地址對外提供服務。爲了不一些沒必要要的錯誤,咱們先把其它四臺虛擬機的窗口關掉。nginx
1.解壓ngx_cache_purge-2.3.tar.gz,解壓命令:tar -zxvf ngx_cache_purge-2.3.tar.gz -C / usr / local / fast /,以下圖所示(另外一臺設備就不粘貼圖片了)。緩存
![](http://static.javashuo.com/static/loading.gif)
解壓完以後咱們在在/ usr /本地/快/目錄下能夠看到多了一個ngx_cache_purge-2.3文件夾。以下圖所示。服務器
![](http://static.javashuo.com/static/loading.gif)
2.下載須要的依賴庫,在兩臺設備上依次執行下面四條命令。cookie
[html] 查看純文本 session
- yum安裝pcre
- yum安裝pcre-devel
- yum安裝zlib
- yum安裝zlib-devel
3.爲兩臺設備都安裝nginx,咱們在XShell的下方的輸入框中輸入命令:cd / usr / local / software /並敲回車,兩個窗口都會進入/ usr / local / software目錄下,而後在下面的輸入框再輸入 「LL」 來查看在/ usr /本地/軟件目錄下的文件,以下圖所示(只有輸入框左邊的圖標是多窗口的狀況下才能一次做用全部窗口,若是當前是單窗口圖標,就以下圖那樣選擇所有XSHELL)。app
![](http://static.javashuo.com/static/loading.gif)
接着,咱們在下面的輸入框中輸入:tar -zxvf nginx-1.6.2.tar.gz -C / usr / local /並按回車,會在兩個窗口同時執行解壓操做以下圖所示。
負載均衡
![](http://static.javashuo.com/static/loading.gif)
接下來咱們在下面的輸入框中輸入:cd / usr / local並按回車,兩臺設備都進入/usr/local/nginx-1.6.2目錄下。以下圖所示。tcp
![](http://static.javashuo.com/static/loading.gif)
接着,在下面的輸入框中加入模塊命令:./configure --add-module = / usr / local / fast / ngx_cache_purge-2.3,回車就會在兩臺設備上都執行添加緩存模塊並會檢查環境。
![](http://static.javashuo.com/static/loading.gif)
接着在下面的輸入框中輸入命令:make && make install,回車就會在兩臺設備上都執行編譯安裝以下圖所示。
![](http://static.javashuo.com/static/loading.gif)
下面咱們須要修改下兩臺設備在/ usr /本地/ nginx的/ conf目錄/目錄下的nginx.conf文件,你們能夠直接把下面代碼替換這個目錄下的該文件,也能夠直接到:HTTP://下載。 csdn .NET / detail / u012453843 / 9803673這個地址下載nginx.conf文件來替換。不過因爲咱們搭建環境的虛擬機IP可能不同,所以,咱們須要根據實際狀況修改下IP等信息(注意192.168.156.5和192.168.156.6這兩臺設備的在/ usr /本地/ nginx的/ CONF /目錄下的nginx.conf都要修改)
[html] 查看純文本
- #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;
-
-
- 事件{
- worker_connections 1024;
- 使用epoll
- }
-
-
- http {
- 包括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;
-
- 發送文件;
- tcp_nopush on;
- #tcp_nopush on
-
- #keepalive_timeout 0;
- keepalive_timeout 65;
-
- #gzip on;
- #設置緩存
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 300m;
-
- proxy_redirect關閉
- proxy_set_header主機$ http_host;
- proxy_set_header X-Real-IP $ remote_addr;
- proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- proxy_connect_timeout 90;
- proxy_send_timeout 90;
- proxy_read_timeout 90;
- proxy_buffer_size 16k;
- proxy_buffers 4 64k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- #設置緩存存儲路徑,存儲方式,分別內存大小,磁盤最大空間,緩存期限
- proxy_cache_path / fastdfs / cache / nginx / proxy_cache levels = 1 :2
- keys_zone = http -cache:200m max_size = 1g inactive = 30d ;
- proxy_temp_path / fastdfs / cache / nginx / proxy_cache / tmp;
- #1組的服務設置
- 上游fdfs_group1 {
- 服務器192.168.156.7:8888 weight = 1 max_fails = 2 fail_timeout = 30s ;
- 服務器192.168.156.8:8888 weight = 1 max_fails = 2 fail_timeout = 30s ;
- }
- #第2組的服務設置
- 上游fdfs_group2 {
- 服務器192.168.156.9:8888 weight = 1 max_fails = 2 fail_timeout = 30s ;
- 服務器192.168.156.10:8888 weight = 1 max_fails = 2 fail_timeout = 30s ;
- }
-
- 服務器{
- 聽8000;
- server_name localhost;
-
- #charset koi8-r
-
- #access_log logs / host.access.log main;
- #1組的負載均衡配置
- 位置/ group1 / M00 {
- proxy_next_upstream http_502 http_504錯誤超時invalid_header;
- proxy_cache http-cache;
- proxy_cache_valid 200 304 12h;
- proxy_cache_key $ uri $ is_args $ args;
- #對應組1的服務設置
- proxy_pass http:// fdfs_group1;
- 到期30d;
- }
-
- 位置/ group2 / M00 {
- proxy_next_upstream http_502 http_504錯誤超時invalid_header;
- proxy_cache http-cache;
- proxy_cache_valid 200 304 12h;
- proxy_cache_key $ uri $ is_args $ args;
- #對應組2的服務設置
- proxy_pass http:// fdfs_group2;
- 到期30d;
- }
-
- 位置〜/ purge(/.*){
- 容許127.0.0.1;
- 容許192.168.156.0/24;
- 拒絕全部
- proxy_cache_purge http-cache $ 1 $ is_args $ args;
- }
-
- 位置 / {
- 根html;
- index index.html index.htm;
- }
-
- #error_page 404 /404.html;
-
- #將服務器錯誤頁面重定向到靜態頁面/50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- 根html;
- }
-
- #代碼PHP腳本到Apache在127.0.0.1:80上監聽
- #
- #location〜\ .php $ {
- #proxy_pass http://127.0.0.1;
- #}
-
- #將PHP腳本傳遞給FastCGI服務器,偵聽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訪問.htaccess文件,若是是Apache的文檔根
- #贊成nginx的
- #
- #location〜/\.ht {
- #deny all;
- #}
- }
-
-
- #另外一個使用IP-,基於名稱和端口配置的虛擬主機
- #
- #server {
- #聽8000;
- #聽somename:8080;
- #server_name somename alias another.alias;
-
- # 位置 / {
- #root html;
- #index index.html index.htm;
- #}
- #}
-
-
- #HTTPS服務器
- #
- #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;
-
- # 位置 / {
- #root html;
- #index index.html index.htm;
- #}
- #}
-
- }
修改完nginx.conf文件以後,咱們下面須要建立/ fastdfs /緩存/ nginx的/ proxy_cache和/ fastdfs /緩存/ nginx的/ proxy_cache的/ tmp目錄,這是由於咱們在nginx.conf文件中配置緩存路徑時指定了該目錄,可是這兩個目錄目前還不存在,所以咱們須要在192.168.156.5和192.168.156.6這兩臺設備上都建立下這兩個目錄,因爲涉及到多級,所以須要遞歸建立目錄,使用命令:mkdir -p / fastdfs / cache / nginx / proxy_cache和mkdir -p / fastdfs / cache / nginx / proxy_cache / tmp,以下圖所示。
![](http://static.javashuo.com/static/loading.gif)
因爲咱們配置了兩個跟蹤器的訪問端口是8000,而咱們的防火牆是不容許訪問該端口的,所以咱們須要修改下防火牆,使其容許訪問8000端口,這個操做我在上篇和中篇都介紹過了,這裏就不囉嗦了。
下面咱們便來啓動這兩臺設備上的nginx。啓動所用的命令是/ usr / local / nginx / sbin / nginx。啓動完後以後,可使用ps -ef | grep nginx命令來查看nginx是否正常啓動,若是看到根4027 1 0 08:18?00:00:00 nginx:master進程/ usr / local / nginx / sbin / nginx這條信息,說明正常啓動了。
[html] 查看純文本
- [root @ itcast05 conf]#/ usr / local / nginx / sbin / nginx
- [root @ itcast05 conf]#ps -ef | grep nginx
- 根4027 1 0 08:18?00:00:00 nginx:主進程/ usr / local / nginx / sbin / nginx
- 沒有人4028 4027 0 08:18?00:00:00 nginx:worker進程
- 沒人4029 4027 0 08:18?00:00:00 nginx:緩存管理器進程
- 沒有人4030 4027 0 08:18?00:00:00 nginx:緩存加載器進程
- root 4032 1522 0 08:18 pts / 0 00:00:00 grep nginx
兩臺設備都啓動完nginx的以後,咱們再在192.168.156.5上上傳兩次次圖片,第一次返回的路徑是在組1下,第二次返回的路徑是在組2下。
[html] 查看純文本
- [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg
- 組1 / M00 / 00/00 / wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg
- [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg
- 組2 / M00 / 00/00 / wKicCVjkOeaAVb0dAAHk-VzqZ6w123.jpg
- [root @ itcast05 conf]#
因爲咱們在192.168.156.5和192.168.156.6上配置了代理,代理端口是8000,因此咱們能夠訪問這兩個IP的8000端口來訪問咱們剛纔上傳的圖片,以下圖所示(咱們訪問的http:// 192.168.156.5:8000/group1/M00/00/00/wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg也能訪問到該圖片)。這說明咱們配置的代理徹底沒問題。
![](http://static.javashuo.com/static/loading.gif)
咱們知道,nginx的對外提供服務有可能碰到服務掛掉的時候,這時候高可用就顯得異常重要了,所以如今咱們搭建一個nginx的和KEEPALIVED結合實現的nginx的集羣高可用的環境,你們能夠參考的http: //blog.csdn .net / u012453843 / article / details / 69668663這篇博客進行學習。
咱們如今要把KEEPALIVED實現的nginx的集羣高可用應用到咱們的FastDFS集羣當中,如今用於搭建nginx的集羣高可用的設備是192.168.156.11和192.168.156.12,咱們只須要修改下這兩臺設備的nginx的。的conf文件,配置文件以下
[html] 查看純文本
- #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;
-
-
- 事件{
- worker_connections 1024;
- }
-
-
- http {
- 包括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;
-
- 發送文件;
- #tcp_nopush on
-
- #keepalive_timeout 0;
- keepalive_timeout 65;
-
- #gzip on;
-
- 上游fastdfs_tracker {
- 服務器192.168.156.5:8000 weight = 1 max_fails = 2 fail_timeout = 30s ;
- 服務器192.168.156.6:8000 weight = 1 max_fails = 2 fail_timeout = 30s ;
- }
-
- 服務器{
- 聽80
- server_name localhost;
-
- #charset koi8-r
-
- #access_log logs / host.access.log main;
-
- location / fastdfs {
- 根html;
- index index.html index.htm;
- proxy_pass http:// fastdfs_tracker /;
- proxy_set_header主機$ 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;
- }
-
- #error_page 404 /404.html;
-
- #將服務器錯誤頁面重定向到靜態頁面/50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- 根html;
- }
- }
- }
咱們對配置文件作了兩處修改,一處是添加了負載均衡上游fastdfs_tracker,以下所示咱們是把192.168.156.5和192.168.156.6兩臺設備做爲追蹤器,如今咱們加了一層nginx來代理這兩個跟蹤器。
[html] 查看純文本
- 上游fastdfs_tracker {
- 服務器192.168.156.5:8000 weight = 1 max_fails = 2 fail_timeout = 30s ;
- 服務器192.168.156.6:8000 weight = 1 max_fails = 2 fail_timeout = 30s ;
- }
第二處修改是,添加了一個位置,而且匹配規則是路徑當中有fastdfs以下所示。
[html] 查看純文本
- location / fastdfs {
- 根html;
- index index.html index.htm;
- proxy_pass http:// fastdfs_tracker /;
- proxy_set_header主機$ 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;
- }
作好了修改以後,咱們只需從新啓動192.168.156.11和192.168.156.12這兩臺設備的nginx的便可。
[html] 查看純文本
- [root @ nginx1 conf]#/ usr / local / nginx / sbin / nginx -s reload
- [root @ nginx1 conf]#
[html] 查看純文本
- [root @ nginx2 conf]#/ usr / local / nginx / sbin / nginx -s reload
- [root @ nginx2 conf]#
這樣咱們便配置好了虛擬IP,如今咱們從192.168.156.5再上傳一張圖片,以下所示。
[html] 查看純文本
- [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg
- 組1 / M00 / 00/00 / wKicB1jqnPqARiT6AAHk-VzqZ6w956.jpg
- [root @ itcast05 conf]#
咱們如今就用虛擬IP192.168.156.110來訪咱們剛纔上傳的圖片,只是注意在地址欄中要記得輸入fastdfs(這是咱們nginx.conf文件中位置/ fastdfs {}規則規定的)以下圖所示,發現,咱們經過虛擬IP即可以訪問咱們上傳的圖片了。這樣的好處是,對用戶來講,只須要訪問這個虛擬IP就能夠了,不用關心FastDFS集羣內部的轉發機制。
![](http://static.javashuo.com/static/loading.gif)
這樣咱們的FastDFS集羣便搭建完了,搭建完後的集羣圖以下圖所示。這個集羣當中192.168.156.7,192.168.156.8,192.168.156.9,192.168.156.10這四臺設備以8888端口對外提供服務,咱們使用追蹤器跟蹤器管理這四臺存儲設備,兩個跟蹤器的nginx的對外提供的端口號是8000,也就是咱們可使用兩臺跟蹤的任何一臺設備的IP而且使用端口8000來訪問存儲在存儲上的資源文件。其實咱們徹底能夠在兩臺跟蹤設備上搭建KEEPALIVED和nginx的相結合的高可用環境而且對外提供虛擬IP192.168.156.110和端口80來訪問資源文件。只不過這裏爲了展現多層nginx的負載均衡因此纔在192.168.156.11和192.168.156.12上專門搭建了KEEPALIVED和nginx的相結合的高可用環境,由這兩臺設備對外提供虛擬IP服務,因爲端口使用了默認的80,所以咱們在使用虛擬IP192。 168.156.110訪問圖片的時候纔不用輸入端口號的。
![](http://static.javashuo.com/static/loading.gif)
備註:啓動集羣步驟
1.啓動6臺設備(192.168.156.5,192.168.156.6,192.168.156.7,192.168.156.8,192.168.156.9,192.168.156.10)的nginx的(其中192.168.156.11和192.168.156.12配置了KEEPALIVED開機自啓動,順帶會啓動nginx的,所以這兩臺設備不用啓動nginx的)
2.啓動跟蹤器(192.168.156.5和192.168.156.6,啓動命令:/etc/init.d/fdfs_trackerd start)
啓動存儲(192.168.156.7,192.168.156.8,192.168.156.9,192.168.156.10,啓動命令:/etc/init.d/fdfs_storaged start)
這樣FastDFS集羣便都啓動完了。