当前位置:文档之家› 在centos上搭建nginx图片服务器(包含上传模块)

在centos上搭建nginx图片服务器(包含上传模块)

安装Nginx 和相关的插件
(Image Filter Module & Upload Module & Upload Progress Module) (1) install essential sys library
$ yum -y install gcc-c++
$ yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gd gd-devel
(2)install nginx & related module plugin
$ wget https:///masterzen/nginx-upload-progress-module/archive/v0.9.1.tar.gz
$ wget https:///vkholodkov/nginx-upload-module/archive/2.2.0.tar.gz
$ wget /download/nginx-1.3.8.tar.gz
$ tar zxvf *.tar.gz
$ cd /nginx-1.3.8/conf
(3)configure nginx.conf
$ vi nginx.conf
#user root;
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;
client_max_body_size 30M;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream backend{
ip_hash;
server 127.0.0.1:8080;
keepalive 32;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location = / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ^~ /static/ {
root /usr/share/nginx/html;
}
location ^~ /images/ {
root /usr/share/nginx/uploads/;
}
location ~* ^/resize/([\d\-]+)/([\d\-]+)/(.+)$ { alias /usr/share/nginx/uploads/images/$3;
image_filter resize $1 $2;
image_filter_buffer 8M;
}
location /upload {
upload_pass /mobile/upload;
upload_store /usr/share/nginx/uploads/images 1;
upload_store_access user:rw;
upload_set_form_field "${upload_field_name}.fieldName" $upload_field_name;
upload_set_form_field "${upload_field_name}.name" $upload_file_name;
upload_set_form_field "${upload_field_name}.contentType" $upload_content_type;
upload_set_form_field "${upload_field_name}.path" $upload_tmp_path;
upload_aggregate_form_field "${upload_field_name}.md5" $upload_file_md5;
upload_aggregate_form_field "${upload_field_name}.size" $upload_file_size;
# upload_pass_form_field "^.*$";
upload_limit_rate 0;
upload_cleanup 400 404 499 500-505;
upload_pass_args on;
}
location / {
proxy_pass http://backend;
proxy_intercept_errors on;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
}
}
(4)nginx add module
$ ./configure --add-module=../nginx-upload-module-2.2.0/ --add-module=../nginx-upload-progress-module-0.9.1/ --prefix=/usr/local/nginx
--with-http_ssl_module --with-http_image_filter_module
$ make
$ make install
$/usr/local/nginx/sbin/nginx (启动)
$/usr/local/nginx/sbin/nginx -s stop(停止)
PS:
upload_pass /mobile/upload;(tomcat后端项目处理上传信息的路径)
upload_store /usr/share/nginx/uploads/images 1;(上传图片的存储路径)
#配置拦截请求到tomcat
location / {
proxy_pass http://backend;
proxy_intercept_errors on;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
upstream backend{
ip_hash;
server 127.0.0.1:8080;
keepalive 32;
}。

相关主题