ExpressJS文件上传在NGINX反向代理部署后停止



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

下面的所有内容在我的本地系统上仍然完美运行,并且是 在线完美工作,直到我开始使用 NGINX 上的域而不是 NodeJS IP:PORT

我还在同一过程中安装了让我们加密SSL,我是 不确定这是否会在这里造成麻烦。

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

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 Server (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)}`
)
},
})

最新更新