浏览 3.8k


按点赞数排序
按时间排序
新建一个node服务,使用nginx-upload-module提供的upload_pass参数将处理过的请求转发到我们的node服务上面。分两步走:
修改nginx配置,将nginx-upload-module存储文件的路径和文件名称塞到请求的body参数中。同时将请求转发到node服务
具体配置:
location /upload {
upload_pass /res_upload;
upload_store /usr/local/nginx/html;
upload_resumable on;
upload_state_store /tmp/state;
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;
}
location /res_upload {
#default_type text/html;
#return 200 "success";
proxy_pass http://localhost:8080;
}

const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
let body = '';
req.on('data', chunk => {
body += chunk; // 获取form-data
});
req.on('end', () => {
console.log(body);
const params = parseForm(body); // json化参数
rename(params.file_path, params.file_name);
res.end('success: you can get it from: /' + params.file_name);
})
}).listen(8080)
function parseForm(data) {
const reg = /name="([\w_]+)"\s+(.+)\s/g;
const params = {};
let matched;
while((matched = reg.exec(data))){
params[matched[1]] = matched[2];
}
console.log(params);
return params;
}
function rename(source, name){
const path = require('path');
const dir = path.dirname(source);
fs.renameSync(source, path.join(dir, name));
}请提供详细配置信息。
location / {
add_header Cache-Control max-age=2592000;
root html;
index index.html index.htm;
}
我使用如上配置,服务端响应如下:
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Fri, 02 Sep 2022 09:10:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 20 Jul 2022 12:09:37 GMT
Connection: keep-alive
ETag: "62d7f081-264"
Cache-Control: max-age=2592000
Accept-Ranges: bytes
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
可以实现但是要自己开发nginx—http2—>web server 这个功能模块,其实是有必要这么做的。当前端请求非常大,nginx到后端服务的tcp没有处理完而占用的时候,就会占用一个端口。一台服务器65535个端口,除掉系统占用的。那么也就是说nginx和后端服务最大并发是6W+个请求。使用http2合并(收敛)tcp请求在一定场景下是非常有必要的。
微信公众号
加入微信群