传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java、PHP、Python、Ruby等。今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上传的文件保存到临时文件,然后在交由后台页面处理,并且把文件的原名,上传后的名称,文件类型,文件大小set到页面。
GitHub:?https://github.com/vkholodkov/nginx-upload-module/tree/2.2
Site:?Upload | NGINX
一、安装模块
#### nginx依赖
yum -y install pcre-devel openssl-devel zlib-devel
### 获取所需的模块源码,并自行解压
wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz
wget http://www.openssl.org/source/openssl-1.0.2k.tar.gz
###源码编译nginx,添加uoload模块
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --with-http_stub_status_module --with-openssl=/home/opt/openssl-1.0.2k --add-module=/home/opt/nginx-upload-module-2.2
make && make install
###安装nginx服务命令
ln -s /usr/sbin/nginx /usr/bin/nginx
二、启动服务
配置nginx.conf文件
cat /etc/nginx/nginx.conf
user root;
#user www-data www-data;
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;
# include /etc/nginx/conf.d/*.conf;
#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_http_version 1.0;
gzip_comp_level 3;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
include /etc/nginx/conf.d/*.conf;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
#gzip on;
server {
listen 8080;
server_name localhost;
error_page 405 =200 $uri;
#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 /upload {
upload_pass /info.php; ##本文中使用静态文件实现上传文件功能验证
# upload_pass @handle_upload; ##如果这里使用的service而非静态文件实现的上传文件步骤,则使用@,在后续配置出@handle_upload服务的http://ip:port
upload_store /data/uptmp;
upload_limit_rate 1024k;
upload_set_form_field "${upload_field_name}_name" $upload_file_name;
upload_set_form_field "${upload_field_name}_content_type" $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;#md5
upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
upload_pass_form_field "^submit$|^description$";
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
# location @handle_upload {
# proxy_pass http://127.0.0.1:8080;
# }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
因为在测试中,使用的php文件,所以nginx需要配置启动php服务。php配置的服务见上nginx.conf中的location ~ \.php${}配置,nginx默认集成php服务,现在需要在环境启动php-fpm服务,构建php文件运行环境。
php-fpm默认运行是socket,但这里给php服务配置的9000端口,配置过程如下:
###php-fpm服务安装与启动
yum install php-fpm
systemctl start php-fpm
###修改php-fpm服务的配置文件
####查找php.ini文件位置,这里找到在/etc/php.ini,根据该文件位置找其他配置文件
php --ini |grep Loaded
vim /etc/php-fpm.d/www.conf
####在大概64行修改如下:
62 ; accepted from any ip address.
63 ; Default Value: any
64 listen = 127.0.0.1:9000
65 listen.allowed_clients = 127.0.0.1
###重启服务
systemctl restart php-fpm
?启动nginx
###bginx服务启动
nginx -c /etc/nginx/nginx.conf
###nginx的配置文件做了修改,重新加载命令
nginx -s reload
三、测试
在本地安装的nginx目录:/usr/local/nginx/html,放入访问html文件和服务脚本info.php文件
[root@test ~]# cat /usr/local/nginx/html/up.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test upload</title>
</head>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name="upload" method="POST" enctype="multipart/form-data" action="/upload">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
[root@ambari03 ~]# cat /usr/local/nginx/html/info.php
<?php
header("Content-Type:text/html; charset=utf-8");
print_r($_POST);
?>
访问说,在网页输入:http://nginx_ip:8080/up.html,提交文件后,输出文件的名称即表示成功。
|