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

来源:互联网 发布:全景补地软件 编辑:程序博客网 时间:2024/06/10 00:13

接上篇: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.gzcd 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:22122tracker_server=192.168.1.212:22122url_have_group_name = truestore_path0=/home/fastdfs/storage
复制http.conf、mine.types到/etc/fdfs
cp /home/software/fastdfs-5.05/conf/http.conf mime.types /etc/fdfs/
/home/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /home/fastdfs/storage/data/ /home/fastdfs/storage/data/M00
配置nginx访问storage文件
vim 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.jpggroup1/M00/00/00/wKgB1VnbSxSAeXaBAATBl8xofxY228.jpg[root@localhost home]#
tracker2
[root@localhost home]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/ht.jpggroup1/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
tracker2
两个都可以正常访问,记下来我们配置nginx配置文件,让tracker将对自己的80端口的请求代理到两个storage上去。
vim /usr/local/nginx/conf/nginx.con
删除原有内容,增加如下内容
#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










原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 咳嗽20天老不好怎么办 吃过退烧药出汗怎么办 5岁儿童发烧39度怎么办 小孩烧到39度怎么办 儿童7岁发烧39度怎么办 发烧没药怎么办怎样退烧快 小孩发烧怎么办怎样退烧快 发烧头疼怎么办最快最有效 发烧头晕怎么办最快最有效 婴儿发烧怎么办最快最有效 孩子一直37度8怎么办 一岁半宝宝37度5怎么办 发烧打了针35度怎么办 小孩发烧吃了鱼怎么办 八个月婴儿发烧39度怎么办 婴儿反复发烧39度怎么办 宝宝发烧了怎么办如何退烧 宝宝烧到38.8度怎么办 小孩发烧到39度怎么办 宝贝发烧到40度怎么办 孩子发烧39度8怎么办 儿子发烧39度该怎么办 孩子发烧39度7怎么办 7个月婴儿发烧怎么办 感冒了头发很油怎么办 5岁宝宝发烧39度怎么办 婴儿烧到39.5度怎么办 1岁多宝宝39.5度怎么办 宝宝发烧40多度怎么办 7岁宝宝发烧了怎么办 宝宝反复发烧39度怎么办 一岁半宝宝反复发烧39度怎么办 七岁发烧38度怎么办 小孩一直37度1怎么办 婴儿一直37度多怎么办 1岁半高烧39度怎么办 反复发烧39度多怎么办 孩子不爱喝水怎么办%3f 8岁儿童不爱喝水怎么办 儿子14岁了不爱说话怎么办 我孩子长得老慢怎么办