在上一期我们说到了如何编译安装Mogilefs并且对其进行配置,最后能够放入图片并进行访问,但是最后访问的时候输入的URL非常的长,而且和key的值无关…那么对于体验来说是及其差劲的,所以今天的文章带来使用nginx作为mogilefs代理来接受并相应请求的!
nginx除了能够代理用户的请求,并且还是实现对Mogilefs进行负载均衡,不过在实现之前首先需要nginx的一个模块包,名字叫做:nginx_mogilefs_module-1.0.4.tar.gz不过如果需要下载戳我!而且nginx只能编译安装,因为这个模块好像不是内部模块。所以我们首先准备好开发环境之后对nginx进行编译安装!
编译安装nginx
首先关于下载nginx的源码和相关模块我这里就忽略了,直接进入主线:
[root@localhost ~]# yum install gcc gcc-c++ openssl-devel pcre-devel #安装实验环境和相关依赖包 [root@localhost ~]# groupadd -r nginx && useradd -r -g nginx nginx #添加相关用户 [root@localhost ~]# tar -xf nginx-1.10.1.tar.gz [root@localhost ~]# tar -xf nginx_mogilefs_module-1.0.4.tar.gz [root@localhost ~]# cd nginx-1.10.1 [root@localhost nginx-1.10.1]# ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/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_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-debug \ --add-module= #(填入你的模块名称,别忘了将模块解压缩!) [root@localhost nginx-1.10.1]# make && make install #编译的中间出现这个: /root/nginx_mogilefs_module-1.0.4/ngx_http_mogilefs_module.c: In function ‘ngx_http_mogilefs_create_spare_location’: /root/nginx_mogilefs_module-1.0.4/ngx_http_mogilefs_module.c:1532:39: error: variable ‘pclcf’ set but not used [-Werror=unused-but-set-variable] ngx_http_core_loc_conf_t *clcf, *pclcf, *rclcf; ^ cc1: all warnings being treated as errors make[1]: *** [objs/addon/nginx_mogilefs_module-1.0.4/ngx_http_mogilefs_module.o] Error 1 make[1]: *** Waiting for unfinished jobs.... make[1]: Leaving directory `/root/nginx-1.10.1' make: *** [build] Error 2 #由于不确定原因,在CentOS 7.1上编译时,可能会出现上面的错误,不过解决办法就是更改objs/Makefile文件,删除-Werror来忽略此错误。删除-Werror之后不要清除编译完毕的文件,接着只用make && make install即可! [root@localhost nginx-1.10.1]# vi objs/Makefile CC = cc CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter #找到这一行就留这些就行删除-werror! #随后继续mkae && make install这样编译安装就算完成了!
配置nginx
在配置之前我首先需要添加一个nginx的启动脚本:
[root@localhost nginx-1.10.1]# vi /etc/rc.d/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/sbin/nginx”
prog=$(basename $nginx)
NGINX_CONF_FILE=”/etc/nginx/nginx.conf”
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`
options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
for opt in $options; do
if [ `echo $opt | grep ‘.*-temp-path’` ]; then
value=`echo $opt | cut -d “=” -f 2`
if [ ! -d “$value” ]; then
# echo “creating” $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
}
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
随后我们接着执行:
[root@localhost nginx-1.10.1]# chmod +x /etc/rc.d/init.d/nginx #给予执行权限! [root@localhost nginx-1.10.1]# chkconfig --add nginx #增加这个可以启动的脚本 [root@localhost nginx-1.10.1]# chkconfig nginx on #设置开机启动
接下来编辑配置文件:
[root@localhost nginx]# vi /etc/nginx/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 trackers { #定义trackers负载均衡
#server 128.168.1.200:7001 weight=1; #weight是权重!相同则为轮流
#server XXXXX:7001 weight=1;
#server XXXXXX:7001 weight=1;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
}
location /images/ {
mogilefs_tracker 128.168.1.200:7001; #填写mgilefs的信息。下面全部都是。
mogilefs_domain hobbit;
mogilefs_methods GET;
mogilefs_noverify on;
mogilefs_pass {
proxy_pass $mogilefs_path; #这边写的都是反向代理!
proxy_hide_header Content-Type;
proxy_buffering off;
}
}
}
}
定义完成以后启动nginx:
[root@localhost nginx]# service nginx start
使用key作为URL来查看图片:
抱歉,上期结束以后可能保存虚拟机有问题导致原来的key有问题,现在重新上传了图片但是更改了key!
好了到此关于使用nginx代理mogilefs就结束啦!
Comments