FastDFS加载fastdfs-nginx-module模块,实现HTTP访问

接上篇:Keepalived+Nginx+FastDFS实现最简单的文件服务器的高可用

地址:http://blog.csdn.net/goodmylove/article/details/78183152

接下来我们来为storage安装fastdfs-nginx-module,目的是通过HTTP来访问我们上传到storage上的东西。
在所有storage上操作
进入我们的软件目录
cd /home/software
解压fastdfs-nginx-module_v1.19.zip
unzip fastdfs-nginx-module_v1.19.zip
安装Nginx依赖环境
yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
解压Nginx,并进入nginx目录
tar -zxvf nginx-1.13.0.tar.gz
cd nginx-1.13.0
编译nginx配置,这里要注意,标红字段为你自己解压的fastdfs-nginx-module的src目录的位置
./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-stream --with-pcre --add-module=/home/software/fastdfs-nginx-module-master/src --with-stream
编译安装
make && make install
添加nginx为系统服务
vim /etc/init.d/nginx
写入内容如下
#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 

esac 

保存退出
赋予可执行权限
chmod +x /etc/init.d/nginx
添加nginx用户
useradd -M -s /sbin/nglogin nginx
增加client目录
mkdir /var/tmp/nginx/client -p
复制 mod_fastdfs.conf文件至fastdfs配置目录
cp /home/software/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
修改
vim /etc/fdfs/mod_fastdfs.conf
修改如下内容
tracker_server=192.168.1.211:22122
tracker_server=192.168.1.212:22122
url_have_group_name = true
store_path0=/home/fastdfs/storage
复制http.conf、mine.types到/etc/fdfs
cp /home/software/fastdfs-5.05/conf/http.conf  /etc/fdfs/
cp /home/software/fastdfs-5.05/conf/mime.types /etc/fdfs/
/home/fastdfs/storage 文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /home/fastdfs/storage/data/ /home/fastdfs/storage/data/M00
配置nginx访问storage文件
vim /usr/local/nginx/conf/nginx.conf
删除所有内容,添加下面内容
#user nobody;
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 8888;
        server_name localhost;
        location ~/group([0-9])/M00 {
            ngx_fastdfs_module;
        }
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
            root html;
        }
    }
}
保存退出
启动nginx
service nginx start
启动 nginx 后,如果打印出 ngx_http_fastdfs_setpid=xxx ,表示 fastdfs-nginx-module 模块加载成功了, xxx 表示模块的进程 ID
再tracker上重新上传文件
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/ht.jpg
tracker1
[root@localhost home]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/ht.jpg
group1/M00/00/00/wKgB1VnbSxSAeXaBAATBl8xofxY228.jpg
[root@localhost home]#
tracker2
[root@localhost home]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/ht.jpg
group1/M00/00/00/wKgB1lnbSxWAL4aiAATBl8xofxY395.jpg
[root@localhost home]# 
通过storageIP:8888+返回路径访问上传至storage服务器上的网页
192.168.1.213:8888/group1/M00/00/00/wKgB1VnbSxSAeXaBAATBl8xofxY228.jpg
192.168.1.214:8888/group1/M00/00/00/wKgB1VnbSxSAeXaBAATBl8xofxY228.jpg
192.168.1.213:8888/group1/M00/00/00/wKgB1lnbSxWAL4aiAATBl8xofxY395.jpg
192.168.1.214:8888/group1/M00/00/00/wKgB1lnbSxWAL4aiAATBl8xofxY395.jpg
我这里就贴一个连接的侧视图就可以了,不多贴其他的
接下来我们在tracker上安装nginx,实现通过tracker的80端口向storage发送读写请求
在所有tracker上操作
进入软件目录
cd /home/software
安装以来环境
yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
解压nginx
tar -zxvf nginx-1.13.0.tar.gz
进入nginx目录
cd nginx-1.13.0
编译配置
./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-stream --with-pcre --with-stream
编译安装
make && make install
添加nginx为系统服务
vim /etc/init.d/nginx
写入内容如下
#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 

esac 

保存退出
赋予可执行权限
chmod +x /etc/init.d/nginx
添加nginx用户
useradd -M -s /sbin/nglogin nginx
增加client目录
mkdir /var/tmp/nginx/client -p
防火墙通过80端口,之前又类似操作,这里就不细写了。
重启防火墙
service iptables restart
启动nginx
service nginx start
HTTPD分别访问两个tracker,查看nginx安装是否正常。
tracker1 FastDFS加载fastdfs-nginx-module模块,实现HTTP访问_第1张图片
tracker2 FastDFS加载fastdfs-nginx-module模块,实现HTTP访问_第2张图片
两个都可以正常访问,记下来我们配置nginx配置文件,让tracker将对自己的80端口的请求代理到两个storage上去。
vim /usr/local/nginx/conf/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 {
    worker_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_read {
	   server 192.168.1.213:8888 weight=1 max_fails=2 fail_timeout=30s;
	   server 192.168.1.214:8888 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 / {
           root html;
           index index.html index.htm;
           proxy_pass http://fastdfs_tracker_read/;
           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;
        }

        #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;
        }
    }
}
#stream {
#    server {   
#        listen       22222;
#        proxy_pass fastdfs_write;
#        }
#        upstream fastdfs_write {
#        server 192.168.1.230:22122;
#        server 192.168.1.231:22122; 
#            }
#}
保存退出,重启Nginx
chkconfig nginx on
service nginx restart
之前我们测试的是通过storagedIP:8888+返回路径来访问图片,如下
192.168.1.214:8888/group1/M00/00/00/wKgB1lnbSxWAL4aiAATBl8xofxY395.jpg
现在我们用trackerIP+返回路径来访问一下,如下
192.168.1.211/group1/M00/00/00/wKgB1lnbSxWAL4aiAATBl8xofxY395.jpg
相同的,,用192.168.1.212+返回路径也是可以正常访问的。
到这里我们只是实现了对通过tracker对storage的读访问和读负载均衡,并没有写入和写负载均衡。下面是我的另外一篇博客,,用来介绍PHP和Apache和fastdfs的tracker结合,实现写入和写负载均衡。其中对应的IP改为你自己的trackerIP即可。http://blog.csdn.net/goodmylove/article/details/78165208










你可能感兴趣的:(FastDFS)