gpt4 book ai didi

node.js - NGINX 反向代理部署后 ExpressJS 文件上传停止

转载 作者:行者123 更新时间:2023-12-05 07:41:48 25 4
gpt4 key购买 nike

我在我的 ExpressJS 应用程序上使用 Multer 进行文件上传,在我使用 nginx 作为反向代理部署该应用程序之前,所有上传工作都正常进行。我似乎无法弄清楚出了什么问题。我还使用 Vimeo 库进行 Vimeo 上传,该库也已停止。

Everything below still works perfectly on my local system and wasworking perfectly online until I started using adomain on NGINX instead of a NodeJS IP:PORT

I have also installed Lets Encrypt SSL during the same process, I'mnot sure if that will cause trouble here.

以下是我使用 Vimeo API 将图像上传到文件系统和将视频上传到 Vimeo 服务器的代码。

var multer  = require('multer')
var upload = multer({ dest:'public/uploads/' })
var Vimeo = require('vimeo').Vimeo;
var lib = new Vimeo('somekey', 'someuser', 'somepass');


/* HANDLE IMAGE POST */
router.post('/profile', upload.single('picture'), function(req, res) {
if(req.file){
req.body.picture = req.file.filename;
}
});


/* HANDLE VIDEO POST */
router.post('/video-vimeo', upload.single('vimeovideo'), function(req, res) {
if(req.file){
req.body.videourl = req.file.filename;
}
var vimeoUrl ='/public/uploads/'+req.file.filename;
lib.streamingUpload(vimeoUrl, function (error, body, status_code, headers) {
if (error) { throw error; }
lib.request(headers.location, function (error, body, status_code, headers) {
var video = {};
video._id = req.body._id;
video.videourl = body.link;
videoModel.findByIdAndUpdate(video._id, video, function(err, dish) {
if (err) { console.log(err); }
else{ res.redirect(req.get('referer')); }
});
});
});
});

我的 nginx 设置(/etc/nginx/sites-enabled):

server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;

server_name example.com www.example.com;

location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Express 中的 NodeJS 服务器 (bin/www):

var app = require('../app');
var debug = require('debug')('example:server');
var http = require('http');

var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);

var server = http.createServer(app);

server.listen(port, 'localhost');
server.on('error', onError);
server.on('listening', onListening);

我发现一些文章建议在我的 NGINX 配置中添加以下内容(它对我不起作用):

location /public/uploads {
limit_except POST { deny all; }

client_body_temp_path /tmp/;
client_body_in_file_only on;
client_body_buffer_size 128K;
client_max_body_size 1000M;

proxy_pass_request_headers on;
proxy_set_header X-FILE $request_body_file;
proxy_set_body off;
proxy_redirect off;
proxy_pass http://localhost:8080/public/uploads;
}

请让我知道这是否是一个常见问题?我做错了什么?

最佳答案

我最近遇到了这个问题,花了几天时间想弄明白。

在生产模式下,上传文件夹与在开发模式下或本地主机上的位置不同。

尝试在开发和生产环境中运行一些控制台日志,以找出上传文件夹的位置。

这是我的 multer.diskStorage 代码

const storage = multer.diskStorage({
destination(req, file, cb) {
process.env.NODE_ENV === 'production'
? cb(null, '../uploads/')
: cb(null, 'uploads/')
},
filename(req, file, cb) {
cb(
null,
`${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
)
},
})

关于node.js - NGINX 反向代理部署后 ExpressJS 文件上传停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45122452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com