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

问题描述 投票:0回答:2

我在我的 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;
}

请告诉我这是否是一个常见问题?我做错了什么?

node.js express nginx file-upload proxy
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 作为反向代理并使用 Lets Encrypt ssl 时才遇到此问题。

© www.soinside.com 2019 - 2024. All rights reserved.