File upload 上传文件后Nginx上传进度模块出错
当我用nginx上传进度模块上传文件时,文件开始正常上传,然后我得到上传的进度,但当文件上传完成时,我得到错误(File upload 上传文件后Nginx上传进度模块出错,file-upload,nginx,File Upload,Nginx,当我用nginx上传进度模块上传文件时,文件开始正常上传,然后我得到上传的进度,但当文件上传完成时,我得到错误(({“状态”:“错误”,“状态”:175234665684});) 嗯,我的猜测是,在我的配置中指定了backendupload\u pass/upload,我没有,每次它在最后上传文件时,它都试图获取这个后端,并给我错误,因为它不存在。我试图注释掉这一行,但nginx又出现了另一个错误(“track\u uploads”指令track\u upload应该是位置中的最后一个指令,在p
({“状态”:“错误”,“状态”:175234665684});
)
嗯,我的猜测是,在我的配置中指定了backendupload\u pass/upload代码>,我没有,每次它在最后上传文件时,它都试图获取这个后端,并给我错误,因为它不存在。我试图注释掉这一行,但nginx又出现了另一个错误(“track\u uploads”指令track\u upload应该是位置中的最后一个指令,在proxy\u pass或fastcgi\u pass in/etc/nginx/sites enabled/web files.com:52之后)
1) 那么第一个问题是错误是什么
2) 第二个问题是如何在后端将这些参数从nginx传递到后端
也许有人知道怎么回事
我的nginx配置:
http {
...
upload_progress upload 2m;
server {
listen 80;
server_name mydomain;
root /home/cha0s/web-files;
index index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /home/cha0s/web-files$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
location ~ ^/files/(.*)$ {
alias /home/cha0s/$1;
internal;
}
location = /upload/share {
client_max_body_size 250m;
#Specify backend location/url and directory for file upload
upload_pass /upload;
upload_store /tmp;
# Declare variables which are passed to backend
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";
upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
# Delete uploaded file on error
upload_cleanup 400 404 499 500-505;
# Limit upload speed
upload_limit_rate 8k;
track_uploads upload 1m;
}
location = /upload/status {
report_uploads upload;
}
}
}
My test.php:
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<script>
function add() {
if (parseInt(document.getElementById('count').getAttribute('value')) < 8) {
var input = document.createElement('input');
input.setAttribute('type','file');
input.setAttribute('multiple','');
input.setAttribute('name','file[]');
document.getElementById('multiple').appendChild(input);
document.getElementById('multiple').appendChild(document.createElement('br'));
document.getElementById('count').setAttribute('value',parseInt(document.getElementById('count').getAttribute('value'))+1);
}
else {
alert('Можно загрузить не более 8 файлов за раз.');
}
}
function progress() {
var ms = new Date().getTime() / 1000;
rq = 0;
id = "";
for (i = 0; i < 32; i++) {
id += Math.floor(Math.random() * 16).toString(16);
}
document.getElementById('upload').action = "/upload/share?X-Progress-ID=" + id;
document.getElementById('status').style.display = 'block'
interval = window.setInterval(function () { fetch(id, ms); }, 1000);
return true;
}
function fetch(id, ms) {
var fetch = new XMLHttpRequest();
fetch.open("GET", "/upload/status", 1);
fetch.setRequestHeader("X-Progress-ID", id);
fetch.onreadystatechange = function () {
if (fetch.readyState == 4) {
if (fetch.status == 200) {
var now = new Date().getTime() / 1000;
var upload = eval(fetch.responseText);
if (upload.state == 'uploading') {
var diff = upload.size - upload.received;
var rate = upload.received / upload.size;
var elapsed = now - ms;
var speed = upload.received - rq; rq = upload.received;
var remaining = (upload.size - upload.received) / speed;
var uReceived = parseInt(upload.received) + ' bytes';
var uDiff = parseInt(diff) + ' bytes';
var tTotal = parseInt(elapsed + remaining) + ' secs';
var tElapsed = parseInt(elapsed) + ' secs';
var tRemaining = parseInt(remaining) + ' secs';
var percent = Math.round(100*rate) + '%';
var uSpeed = speed + ' bytes/sec';
document.getElementById('length').firstChild.nodeValue = parseInt(upload.size) + ' bytes';
document.getElementById('sent').firstChild.nodeValue = uReceived;
document.getElementById('offset').firstChild.nodeValue = uDiff;
document.getElementById('total').firstChild.nodeValue = tTotal;
document.getElementById('elapsed').firstChild.nodeValue = tElapsed;
document.getElementById('remaining').firstChild.nodeValue = tRemaining;
document.getElementById('speed').firstChild.nodeValue = uSpeed;
document.getElementById('bar').firstChild.nodeValue = percent;
document.getElementById('bar').style.width = percent
}
else {
window.clearTimeout(interval);
}
}
}
}
fetch.send(null);
}
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" id="upload" onsubmit="progress();">
<input type="hidden" id="count" value="1" />
<div id="multiple">
<input type="file" name="file[]" multiple /><br>
</div>
<input type="submit">
<a href="#" onclick="add();">add();</a>
</form>
<div id="status" style="display: none;">
<table width="100%">
<tr><th></th><th>загрузка</th><th>осталось</th><th>всего</th></tr>
<tr><td>время:</td><td id="elapsed">∞</td><td id="remaining">∞</td><td id="total">∞</td></tr>
<tr><td>размер:</td><td id="sent">0 b</td><td id="offset">0 b</td><td id="length">0 b</td></tr>
<tr><td>скорость:</td><td id="speed">n/a</td></tr>
</table>
<div style="border: 1px solid #c0c0c0;">
<div style="background: #c0c0c0; width: 0%; text-align: right;" id="bar">0%</div>
</div>
<a href="#" onclick="if (confirm('Вы точно хотите отменить загрузку?')) window.location = '/'" id="cancel">cancel_upload();</a>
</div>
</body>
</html>
函数add(){
if(parseInt(document.getElementById('count').getAttribute('value'))小于8){
var input=document.createElement('input');
setAttribute('type','file');
input.setAttribute('multiple','');
setAttribute('name','file[]);
document.getElementById('multiple').appendChild(输入);
document.getElementById('multiple').appendChild(document.createElement('br'));
document.getElementById('count').setAttribute('value',parseInt(document.getElementById('count').getAttribute('value'))+1);
}
否则{
警报('Мжзззззззззззззззззз');
}
}
功能进展(){
var ms=new Date().getTime()/1000;
rq=0;
id=“”;
对于(i=0;i<32;i++){
id+=Math.floor(Math.random()*16).toString(16);
}
document.getElementById('upload')。action=“/upload/share?X-Progress-ID=“+ID;
document.getElementById('status').style.display='block'
interval=window.setInterval(函数(){fetch(id,ms);},1000);
返回true;
}
函数获取(id,毫秒){
var fetch=new XMLHttpRequest();
fetch.open(“GET”、“/upload/status”,1);
fetch.setRequestHeader(“X-Progress-ID”,ID);
fetch.onreadystatechange=函数(){
if(fetch.readyState==4){
如果(fetch.status==200){
var now=new Date().getTime()/1000;
var upload=eval(fetch.responseText);
如果(upload.state==“upload”){
var diff=upload.size-upload.received;
var rate=upload.received/upload.size;
var经过=现在-毫秒;
var speed=upload.received-rq;rq=upload.received;
var剩余=(upload.size-upload.received)/速度;
var uReceived=parseInt(upload.received)+“bytes”;
var uDiff=parseInt(diff)+“字节”;
var ttottal=parseInt(已用时间+剩余时间)+“秒”;
var tElapsed=parseInt(经过时间)+“秒”;
var tRemaining=parseInt(剩余)+“秒”;
变量百分比=数学四舍五入(100*比率)+'%';
var uSpeed=速度+字节/秒;
document.getElementById('length').firstChild.nodeValue=parseInt(upload.size)+'bytes';
document.getElementById('sent').firstChild.nodeValue=uReceived;
document.getElementById('offset').firstChild.nodeValue=uDiff;
document.getElementById('total').firstChild.nodeValue=TTottal;
document.getElementById('eassed').firstChild.nodeValue=tElapsed;
document.getElementById(‘剩余’).firstChild.nodeValue=tRemaining;
document.getElementById('speed').firstChild.nodeValue=uSpeed;
document.getElementById('bar').firstChild.nodeValue=百分比;
document.getElementById('bar')。style.width=百分比
}
否则{
窗口清除超时(间隔);
}
}
}
}
fetch.send(空);
}
загрузкаосталосьвсего
время:∞∞∞
аззззааа:0 b0 b0 b
бббббб:不适用
0%
正如我们已经讨论过的:
upload\u pass/upload.php代码>
;-) 嗯。经过一些测试后,我确定问题与代理传递有关。。。。我唯一不明白的是这应该指向哪里。基本上,上传应该是正确的,但由于它不存在,它失败了。。。。。那么回到第一步,在/upload中应该存在什么??????因为如果删除行upload\u pass/upload并放置一些随机代理程序\u pass 127.0.0.1并尝试上载文件,它要么会给我错误500,要么只是告诉我文件不存在……您应该指定一个后端来处理上载的文件并返回响应。如果没有baackend,我该怎么做。。。。如果我不想只使用nginx将文件上传到/tmp位置adn,就这样?我应该为代理\u过程指定什么。。。。也许你可以写一个简单的后端。。。展示它应该是什么样子?如果理解正确,这个后端应该可以在url mydomain.com/upload上找到?这是可选的。它可能在url上不可用。上传模块存储库中有一个示例:下面是我的一个应用程序的示例:(Python)