Nginx做MogileFS的反向代理服务器
在准备用MogileFS来替代公司现有文件系统时,一直困扰于文件的显示,因为像图片这样的静态文件,如果在显示时也要用Http Response来输出,那是一件非常杯具的事情。
经过一番查找,发现Nginx有一个MogileFS的Module,使用本模块,可以直接对MogileFS的文件进行定位。
由于本人现在对Nginx十分着迷,拿到这样一个东西简直如获至宝,于是立即着手测试。
我的Nginx版本是0.8.35,下载的MogileFS模块版本是1.0.2,最开始部署时,十分不顺,因为在将模块编译安装进Nginx后,启动Nginx老是报一个“Segmentation Fault”的错误,这个错误是因为程序中使用了一个空指针引起的,看来是模块版本和Nginx版本不兼容了。但是按照模块的帮助文件中的描述,使用Nginx的0.7系列,依然无法正常启动Nginx....
最后下载了MogileFS模块的1.0.3版本,终于成功搞定,以下是本人针对自己的需求配置的文件服务器的配置:
代码
#user nobody;
worker_processes 4;
error_log logs/error.log notice;
worker_rlimit_nofile 10240;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
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;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml image/jpg image/jpeg image/gif image/png image/x-xbitmap image/pjpeg application/vnd.ms-excel application/vnd.ms-powerpoint application/msword application/x-shockwave-flash application/octet-stream;
gzip_vary on;
tcp_nodelay on;
server {
listen 80;
server_name yourdomain.com;
location / {
mogilefs_tracker 192.168.33.2:6001;
mogilefs_domain test;
mogilefs_methods get;
mogilefs_pass {
proxy_pass $mogilefs_path;
proxy_hide_header Content-Type;
proxy_buffering off;
}
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#user nobody;
worker_processes 4;
error_log logs/error.log notice;
worker_rlimit_nofile 10240;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
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;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml image/jpg image/jpeg image/gif image/png image/x-xbitmap image/pjpeg application/vnd.ms-excel application/vnd.ms-powerpoint application/msword application/x-shockwave-flash application/octet-stream;
gzip_vary on;
tcp_nodelay on;
server {
listen 80;
server_name yourdomain.com;
location / {
mogilefs_tracker 192.168.33.2:6001;
mogilefs_domain test;
mogilefs_methods get;
mogilefs_pass {
proxy_pass $mogilefs_path;
proxy_hide_header Content-Type;
proxy_buffering off;
}
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
值得注意的是,在实际使用时,要注意根据情况调整default_type,默认的值在FireFox下,会把图片当成无法打开的文件进行下载。