【问题标题】:ExpressJS File Upload Stopped After NGINX Reverse Proxy DeploymentNGINX 反向代理部署后 ExpressJS 文件上传停止
【发布时间】: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


【解决方案1】:

我最近遇到了这个问题,花了几天时间试图弄清楚。

当处于生产模式时,上传文件夹的位置与处于开发模式时不同,或者位于本地主机上。

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

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

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 2010-11-07
    • 2019-09-22
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多