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

3
我正在使用 Multer 在我的 ExpressJS 应用程序上上传文件,所有的上传都正常工作,直到我将应用程序部署为反向代理的 nginx。我似乎弄不清楚出了什么问题。我之前也在使用 Vimeo 图书馆进行 Vimeo 上载,但现在也停止了。

在我的本地系统上,以下所有内容仍然完美地工作,并且在开始使用 NGINX 上的域而不是 NodeJS IP:PORT 之后,在在线上也一直完美地工作。

我还同时安装了 Let's Encrypt SSL,我不确定这是否会在这里引起麻烦。

以下是我的代码,用于上传图像到文件系统和视频到 Vimeo 服务器使用 Vimeo API。
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(如果有)的日志吗? - Cisco
@FranciscoMateo 我刚刚检查了nginx的错误日志和访问日志,没有发现任何错误。我没有任何节点日志。我唯一看到的错误是交易正在进行中,但由于找不到图像,它们被呈现为损坏的状态。 - Aayush
我上面的内容可以吗? - Aayush
1
@Aayush,你找到解决方案了吗? - Ivan Vovk
1
我也在使用Nginx反向代理,但在生产环境中无法使用multer,而在本地却可以正常工作。 - Forrest Wilkins
2个回答

0

最近我遇到了这个问题,花了几天时间试图解决它。

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

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

这是我的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)}`
    )
  },
})

0
但问题是什么呢?对我来说,file.originalname是不完整的,我只得到了扩展名,在开发模式下运行得很好。只有在使用Nginx作为反向代理和使用Let's Encrypt SSL的生产环境中才遇到了这个问题。

目前您的回答表达不够清晰。请[编辑]以添加更多细节,帮助其他人理解您的回答如何解决所提出的问题。您可以在帮助中心找到更多关于如何撰写好答案的信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接