首頁>技術>

Nginx的安裝及負載均衡配置

在安裝完分散式檔案系統之後,就可以在應用程式中進行呼叫了。注意需要在 Web端的頁面上進行訪問,還必須藉助Nginx 提供訪問服務。使用Nginx 不僅可以構建負載均衡服務還能使用快取設定。在跟蹤器和儲存節點上都必須安裝Nginx,下面分別進行說明。

在跟蹤器上安裝Nginx

在兩臺 Tracker Server機器上,按如下方法安裝和配置Nginxo安裝pcre支援庫,按順序執行下列命令:

tar xf pcre-8.40.tar.gzcd pcre-8.40./configure --prefix=/usr/local/pcremake && make install

完成後,返回安裝包存放路徑/opt,按順序執行下列命令,安裝Nginx:

tar xfnginx-1.10.3.tar.gzcd nginx-1.10.3./configure --prefix=/data/nginx \--with-pcre=/opt/pcre-8.40\--user=nginx \--group=nginx \--with-http ssl module \--with-http realip module \--with-http stub status modulemake && make install

編輯Nginx的配置檔案nginx.conf,內容如下所示:

user nginx nginx;worker processes 2;#pid /usr/local/nginx/nginx.pid;worker rlimit nofile 51200;events{use epoll;worker connections 20480;}http{includemime. types;default type application/octet-stream;log format main '$remote_ addr - $remote_user [$time_local] "$request'$status $body bytes sent "Shttp referer"''"Shttp user agent" "$http x forwarded for" "$request time"';access_log /data/nginx /logs/access.log main;upstream server_group1{server 192.168.1.23;server 192.168.1.33;}server  {listen 80;server name localhost;location /group1 {#include proxy.conf;proxy pass http://server groupl;}}}

這個配置的原理是對兩個儲存節點上HTTP服務的訪問,將由跟蹤伺服器進行負載均衡排程。例如,14.5節測試生成的圖片檔案,可以透過跟蹤伺服器使用如下所示的連結進行訪問:

http://192.168.1.22/group1/M00/00/00/wKgBGFk3kUqACu9JAAGmMpryn2s674.png

使用如下命令啟動Nginx:

/data/nginx/sbin/nginx

在儲存節點上安裝Nginx

在兩臺Storage機器上,進入存放安裝包的路徑/opt,按順序執行下列命令,安裝pcre支援庫:

tar xf fastdfs-nginx-module_v1.16.tar.gztar xf pcre-8.40.tar.gzcd pcre-8.40./configure --prefix=/data/pcremake && make install

完成後,返回存放安裝包的路徑/opt,按順序執行下列命令,安裝Nginx:

tar xf nginx-1.10.3.tar.gzcd nginx-1.10.3./configure --prefix=/data/nginx \—-with-pere-/opt/pcre-8.40\--user=nginx1--group-nginx \—-with-http_ssl module \--with-http realip module l--with-http stub status module \--add-module=/opt/fastdfs-nginx-module/srcmake && make install

完成後,按下列命令複製相關檔案:

cp /opt/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/cd /opt/fastdfs-5.10/confcp anti-steal.jpg http.conf mime.types /etc/fdfs/

完成後,使用如下命令編輯配置檔案mod_fastdfs.conf:

vi /etc/fdfs/mod fastdfs.conf

編輯下列各項內容:

#日誌目錄base path=/tmp#跟蹤伺服器tracker server=10.10.10.22:22122tracker server=10.10.10.32:22122#URL中是否有group名稱url have group name = true#storage path的個數store_path_count=2store_path0=/data/fdfs_storage/storage0store_pathl=/data/fdfs_storage/storage1

使用如下命令編輯Nginx配置檔案:

vi /data/nginx/ conf/nginx.conf

檔案的內容如下所示:

user nginx nginx;worker processes 2;#pid/usr/local/nginx/logs/nginx.pid;worker rlimit_nofile 1024;events {use epoll;worker connections 1024;}http {include mime.types;server names hash bucket size 128;client header buffer_size 32k;largeclient header buffers 432k;client max body size 20m;limit rate 1024k;default type application/octet-stream;log format main '$remote addr - $remote user [$time local] "Sreauest" 1'$status $body bytes sent "$http referer",I"Shttp user agent" "Shttp x forwarded for"';access log/data/nginx/logs/access.log main;server {listen 80;server name localhost;location ~/group[0-9]/MO0{#root /data/fdfs storage;ngx fastdfs module;}}}

儲存檔案後,使用如下命令建立兩個軟連結:

ln -s /data/fdfs storage/storage0 /data/fdfs storage/storage0/M00ln -s /data/fdfs storage/storagel /data/fdfs storage/storage1/M00

使用如下命令啟動Nginx:

/data/nginx/sbin/nginx
開機啟動

為了方便運維管理,前面安裝的各個服務都可以設定為開機啟動。

開機啟動Tracker

在兩臺Tracker Server機器上,建立服務啟動檔案:

vi/etc/rc.d/init.d/fdfs trackerd

輸入如下內容:

#!/bin/bash##fdfs trackerd Starts fdfs trackerd###chkconfig: 234599 01#description: FastDFS tracker server### BEGIN INIT INFO# Provides: $fdfs trackerd###END INIT INFO#Source function library.. /etc/init.d/functionsPRG=/usr/bin/fdfs trackerdCONF=/etc/fdfs/tracker.confif[!-f $PRG ];thenecho "file $PRG does not exist!"exit 2fiif[!-f $CONF ]; thenecho "file $CONF does not exist! "exit 2fiCMD="$PRG $CONF"RETVAL=0start( {echo -n $"Starting FastDFS tracker server: "$CMD &RETVAL=$?echoreturn $RETVAL}stop () {$CMD stopRETVAL=$?return $RETVAL}rhstatus() {status fdfs trackerd}restart(  {$CMD restart &case "$1" instart)start;;stop)stop;;status)rhstatus;;restart|reload)restart;;condrestart)restart;;* )echo $"Usage: $0 {start IstopIstatus|restart l.condrestart}"exit 1esacexit $?

按順序執行下列命令,把 Tracker設定為開機啟動:

chmod 755 /etc/rc.d/init.d/fdfs trackerdchkconfig --add fdfs_trackerdchkconfig fdfs_trackerd on

開機啟動 Storage

在兩臺Storage機器上,建立服務啟動檔案:

vi /etc/init.d/fdfsstoraged

輸入如下內容:

#!/bin/bash##fdfs storaged Starts fdfs storaged###chkconfig:2345 99 01# description: FastDFS storage server### BEGIN INIT INFO# Provides: $fdfs storaged### END INIT INFO#Source function library../etc/init.d/ functionsPRG=/usr/bin/fdfs_storagedCONF=/etc/fdfs/storage.confif [!-f $PRG ];thenecho "file $PRG does not exist! "exit 2fiif [!-f $CONF J;thenecho "file $CONF does not exist! "exit 2fiCMD="SPRG $CONF"RETVAL=0start({echo -n "Starting FastDES storage server: "$CMD &RETVAL=$?echoreturn $RETVALstop(){$CMD stopRETVAL-$?return $RETVALrhstatus() {status fdfs_storaged}restart({$CMD restart &}case "$1" instart)start;;stop)stopstatus)rhstatus;;restartlreload)restart;;condrestart)restart;;*)echo "Usage: $0{startlstoplstatus| restartl condrestart}"exit 1esacexit $?

按順序執行下列命令,把 Storage設定為開機啟動:

chmod 755 /etclrc.d/init.d/fdfs storagedchkconfig --add fdfs storagedchkconfig fdfs storaged on

開機啟動Nginx

在四臺機器中各建立一個Nginx啟動檔案:

vi /etc/init.d/nginx

輸入如下內容:#!/bin/bash

# chkconfig: -8515PATH=/data/nginxDESC="nginx daemon"NAME=nginxDAEMON=$PATH/sbin/$NAMECONFIGFILE=$PATH/conf/$NAME.confPIDFILE=$PATH/logs/$NAME.pidSCRIPTNAME=/etc/init.d/$NAMEset -e[-x "$DAEMON" 1llexit 0do start( {$DAEMON -c $CONFIGFILE I echo -n "nginx already running"}do_stop ({$DAEMON -s stop ll echo -n "nginx not running"}do reload() {$DAEMON-s reload ll echo -n "nginx can't reload")case "$1" instart)echo -n "Starting $DESC: $NAME"do_startecho ".";;stop)echo -n "Stopping $DESC:$NAME""do_stopecho ".";;reloadl graceful)echo -n "Reloading $DESC configuration. . ."do reloadecho ".";restart)echo -n "Restarting $DESC:$NAME"do stopdo_startecho ".";;*)echo "Usage: $SCRIPTNAME {startlstopl reloadlrestart}">&2exit 3;;esacexit 0

按順序執行下列命令,把 Nginx設定為開機啟動:chmod 755 /etc/rc.d/init.d/nginx

chkconfig --add nginxchkconfig nginx on
小結

本章使用開源的FastDFS搭建了一個高可用的分散式檔案系統,並透過Nginx為檔案的訪問設定了負載均衡服務,從而為微服務應用提供一個高效能的檔案伺服器。

在完成安裝並測試正常之後,即可在庫存管理專案中配置分散式檔案系統的連結地址,使用微服務與分散式檔案系統進行聯調。

23
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • ASP.NET實戰025:Server遠端出現身份驗證錯誤