当文件上传到Cloudinary时,Node Express Multer Nginx-502错误的网关

时间:2019-05-04 00:17:51

标签: node.js express nginx multer cloudinary

我已经将我的node.js express应用程序部署到了DigitalOcean,在那里我使用Nginx和PM2运行我的应用程序。除了我尝试将个人资料图片上传到网站之外,其他所有东西都工作正常。该图像未存储在数据库中,而是被发送到Cloudinary,在此托管我的图像和视频。

当我尝试上传图像时,该应用程序立即转到502 Bad Gateway,并且当我在浏览器中单击“返回”时,似乎PM2已重新启动,原因是我将不得不再次登录。

此功能在我的本地主机上运行良好,因此我认为我的nginx配置或类似的地方可能有问题。

我的Nginx网站可用的默认外观如下:

root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name (DOMAIN) (DOMAIN);

        location / {
          proxy_pass http://127.0.0.1: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;
          proxy_buffer_size 1024k;
          proxy_buffers 4 1024k;
          proxy_busy_buffers_size 1024k;
        }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/(DOMAIN)/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/(DOMAIN)/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = (DOMAIN)) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = (DOMAIN)) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 default_server;
        listen [::]:80 default_server;

        server_name (DOMAIN) (DOMAIN);
    return 404; # managed by Certbot
}

我的nginx配置看起来像这样:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##
        send_timeout 10m;
        client_header_timeout 10m;
        client_body_timeout 10m;
        client_max_body_size 100m;
        large_client_header_buffers 8 256k;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

当我尝试将图像上传到Cloudinary时,我已经检查了nginx的错误日志,它说连接过早关闭。这是日志:

2019/05/03 23:33:57 [error] 1798#1798: *5 upstream prematurely closed connection while reading response header from upstream, client: (IP LOCAL MACHINE), server: (DOMAIN), request: "POST /profile/5cc44420618f3606623288ec/settings/imageupload HTTP/1.1", upstream: "http://127.0.0.1:8080/profile/5cc44420618f3606623288ec/settings/imageupload", host: "(DOMAIN)", referrer: "https://(DOMAIN)/profile/5cc44420618f3606623288ec/settings

2019/05/03 23:33:57 [error] 1798#1798: *5 connect() failed (111: Connection refused) while connecting to upstream, client: (IP LOCAL MACHINE), server: (DOMAIN), request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "(DOMAIN)", referrer: "https://(DOMAIN)/profile/5cc44420618f3606623288ec/settings/imageupload

这是我的节点应用程序发出的POST路由:

router.post("/profile/:id/settings/imageupload", middleware.checkIfUserIsUser, upload.single('avatar'), function(req, res) {
    execForUser(req, res, function(foundUser) {
        if (foundUser.avatarId) {
            cloudinary.v2.uploader.destroy(foundUser.avatarId, function(err) {
                if (err) {
                    createError("E30: Error happened while deleting old profile picture from Cloudinary.");
                    req.flash("error", "Oops! Something wen't wrong while trying to update your profile picture. Please try again.");
                    return res.redirect("back");
                }
                cloudinary.v2.uploader.upload(req.file.path, { moderation: "aws_rek" }, function(error, result) {
                    if (error) {
                        req.flash("error", "Oops! Seems like this image doesn't live up to our standards. Please check that this is not an adult image or contains nudity.");
                        return res.redirect("back");
                    }
                    User.findByIdAndUpdate(req.params.id, { $set: { avatar: result.secure_url, avatarId: result.public_id } }, { new: true }, function(err, updatedUser) {
                        if (err) {
                            createError("E31: Error happened while saving new profile picture to profile.");
                            req.flash("error", "Oops! Something went wrong while trying to save your new profile picture. Please try again.");
                            res.redirect("back");
                        }
                        else {
                            res.redirect("/profile/" + req.params.id + '/settings');
                        }
                    });
                });
            });
        }
        else {
            cloudinary.v2.uploader.upload(req.file.path, { moderation: "aws_rek" }, function(error, result) {
                if (error) {
                    req.flash("error", "Oops! Seems like this image doesn't live up to our standards. Please check that this is not an adult image or contains nudity.");
                    return res.redirect("back");
                }
                User.findByIdAndUpdate(req.params.id, { $set: { avatar: result.secure_url, avatarId: result.public_id } }, { new: true }, function(err, updatedUser) {
                    if (err) {
                        createError("E31: Error happened while saving new profile picture to profile.");
                        req.flash("error", "Oops! Something wen't wrong while trying to save your profile picture. Please try again.");
                        res.redirect("back");
                    }
                    else {
                        res.redirect("/profile/" + req.params.id + '/settings');
                    }
                });
            });
        }
    });
});

我真的不敢一个人解决这个问题。在我的本地计算机上,甚至在我部署到heroku时,它都可以完美运行。但是现在我在digitalocean上使用了nginx,那么我遇到了502。我应该在我的配置文件中进行某种设置以允许上传文件吗?我是Nginx的新手,如果这是我犯的一个愚蠢的错误,请原谅我。

应该工作的方式是,我单击“上传”,它与Cloudinary联系,以检查图像是否包含裸露,如果没有,它会上传并发送回一个我可以用来引用该图像的URL。

0 个答案:

没有答案