【发布时间】:2017-12-20 16:55:13
【问题描述】:
我在我的 ExpressJS 应用程序上使用 Multer 进行文件上传,在我将应用程序部署为 nginx 作为反向代理之前,所有上传都可以正常工作。我似乎无法弄清楚出了什么问题。我还在使用 Vimeo 库进行 Vimeo 上传,但也已停止。
以下所有内容在我的本地系统上仍然可以完美运行,并且 在我开始使用之前可以完美地在线工作 NGINX 上的域而不是 NodeJS IP:PORT
我还在同一过程中安装了 Lets Encrypt SSL,我是 不确定这是否会在这里造成麻烦。
以下是我使用 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;
}
如果这是一个常见问题,请告诉我?我做错了什么?
【问题讨论】:
-
你检查过 nginx 或 express(如果有的话)的日志吗?
-
@FranciscoMateo 我刚刚检查过,nginx 错误日志和访问日志没有显示类似的错误。我没有任何节点日志。我看到的唯一错误是交易正在进行,但由于找不到图像,因此图像被渲染为损坏。
-
我上面的可以吗?
-
@Aayush 你找到解决方案了吗?
-
我也在使用带有反向代理的 Nginx,当它在本地工作正常时无法在生产中使用 multer。
标签: node.js express nginx file-upload proxy