对于文件功能,实际的开发中经常会遇到,一般有2中实现方式,1种是使用稳定的文件分布式服务器,即OSS(Object Storage Service)服务,对于一般的公司来说,起点比较高;另外一种就是使用服务器的目录作为临时存储,这种实现方式很不稳定,文件有被误删的可能,Nginx便是实现文件转发的一个利器。
上传模块:nginx-upload-module
git clone https://github.com/hongzhidao/nginx-upload-module.git
git clone https://github.com/masterzen/nginx-upload-progress-module.git
tar zxf 2.3.0.tar.gz
cd nginx
./configure ... --add-module=/root/thb/nginx-upload-module --add-module=/root/thb/nginx-upload-progress-module
make && make install (添加模块不需要make install)
配置nginx
worker_processes 4;
#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;
upload_progress proxied 8m;
#gzip on;
server {
listen 80;
auth_basic "Please input password"; #这里是验证时的提示信息
auth_basic_user_file /etc/nginx/passwd/testpwd;
# upload
client_max_body_size 100g; # 这个配置表示最大上传大小,但是我没有验证过是否能传 100g 的文件
# Upload form should be submitted to this location
location /upload {
# Pass altered request body to this location
upload_pass /upload.php;
# 开启resumable
upload_resumable on;
# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
# 记得修改目录的读写权限
upload_store /export/tmp/upload 1;
upload_state_store /export/tmp/upload/state;
# Allow uploaded files to be read by all
upload_store_access all:r;
# Set specified fields in request body
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;
# Inform backend about hash and size of a file
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 "^submit$|^description$";
}
location ~ \.php$ {
# fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /myfiles {
alias /export/share/upload/; # 文件存放目录,注意要以 '/' 结尾;
index index.html; # 如果文件存放目录有 index.html,会跳转到 index.html;
autoindex on; # 自动列出目录下的文件;
autoindex_exact_size off; # 文件大小按 G、M 的格式显示,而不是 Bytes;
}
}
}
编辑上传文件
cd /etc/nginx/html
vi upload.php
# cat upload.php
<?php
$header_prefix = 'file';
$slots = 6;
?>
<html>
<head>
<title>Test upload</title>
</head>
<body>
<?php
if ($_POST){
echo "<h3>Uploaded files:</h3>";
echo "<table border=\"2\" cellpadding=\"2\">";
echo "<tr><td>Name</td><td>Location</td><td>Content type</td><td>MD5</td><td>Size</td><td>Scp Command</td><td>Wget Command</tr>";
for ($i=1;$i<=$slots;$i++){
$key = $header_prefix.$i;
if (array_key_exists($key."_name", $_POST) && array_key_exists($key."_path",$_POST)) {
$tmp_name = $_POST[$key."_path"];
$name = $_POST[$key."_name"];
$content_type = $_POST[$key."_content_type"];
$md5 = $_POST[$key."_md5"];
$size = $_POST[$key."_size"];
$final_path = "/export/share/upload";
if (copy($tmp_name, "$final_path/$name")) {
echo "SUCCESS!";
} else {
echo "FAIL!";
}
$scp_cmd = "scp team@***:/export/share/upload/$name .";
$wget_cmd = "wget http://***/files/upload/$name";
echo "<tr><td>$name</td><td>$final_path</td><td>$content_type</td><td>$md5</td><td>$size</td><td>$scp_cmd</td><td>$wget_cmd</td>";
}
}
echo "</table>";
}else{?>
<h3>Select files to upload</h3>
<form name="upload" method="POST" enctype="multipart/form-data" action="/upload">
<input type="file" name="file1"><br>
<input type="file" name="file2"><br>
<input type="file" name="file3"><br>
<input type="file" name="file4"><br>
<input type="file" name="file5"><br>
<input type="file" name="file6"><br>
<input type="submit" name="submit" value="Upload">
<input type="hidden" name="test" value="value">
</form>
<?php
}
?>
</body>
</html>
增加nginx 网页登录验证
yum -y install httpd-tools
mkdir -p /etc/nginx/passwd/
htpasswd -c /etc/nginx/passwd/testpwd user1
评论区