简体   繁体   中英

cURL POST request to Node.js express server hosted on NGINX server returns a 502 error

I'm trying to make a POST request to one of my node api routes. I have several routes, but only one of them work. I'm not sure if this is a problem with NGINX or node or some combination. Everything looks right, I even enabled CORS! The node application is hosted on a DigitalOcean droplet.

cURL POST request: curl -H "Content-Type: application/json; charset=UTF-8" -i -X POST -d '{"text":"test reply from curl","_id":"123"}' "https://example.com/api/reply/test"

My nginx configuration:

    server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example/public;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;

    server_name example.com www.example.com;
    root /var/www/example/public;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    add_header Content-Security-Policy-Report-Only "default-src 'self'";    
    location ~ /.well-know {
        allow all;
    }

    location / {
        proxy_pass http://localhost:3210;
        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;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    }   

}

my node.js express server:

"use strict";
var express = require('express');
var request = require('request');
var url = 'mongodb://localhost:27017/';
var MongoClient = require('mongodb').MongoClient;
var bodyParser = require('body-parser')
var cors = require('cors')
var app = express();
app.set('port', process.env.PORT || 3210);
app.set('host', process.env.HOST || '127.0.0.1');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors())

app.use(express.static('./public'));

app.post('/api/reply/:board', (req,res) => {
  MongoClient.connect(`${url}${req.params.board}`, (err, db) => {
  if(err) { return console.dir(err) }
  var collection = db.collection(req.params.board);
  var reply = {'text':req.params.text}
  collection.update(
   {"_id":ObjectId(req.params._id)},
   {$push:reply}
  )
  });
});

app.post('/api/post/:board', (req, res) => {
  MongoClient.connect(`${url}${req.params.board}`, (err, db) => {
  if (err) {return console.dir(err);}
    console.log(req);
    var collection = db.collection(req.params.board);
    var post = {'text':req.body.text,'time':timestamp(),     'comments':[]}
    collection.insert(post);
    res.sendStatus(200);
  })
});

app.get('/api/posts/:board', (req,res)=> {
  MongoClient.connect(`${url}${req.params.board}`, (err,db) => {
   if(err) {return console.dir(err)}
   var collection = db.collection(req.params.board)
   collection.find().toArray( (err,posts) => {
    res.send({...posts})
   })
  })
});

app.listen(app.get('port'), app.get('host'), () => {
  console.log('express listening on port ' + app.get('port'));
});

My cURL POST request to example.com/api/reply/test returns:

HTTP/1.1 502 Bad Gateway
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 30 Mar 2017 03:15:02 GMT
Content-Type: text/html
Content-Length: 181
Connection: keep-alive

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>

HOWEVER, my GET request to https://example.com/api/posts/test is successful as well as my POST request to https://example.com/api/post/test

Why is nginx refusing my other POST requests??

There route defined for /api/reply/test does send back any response:

app.post('/api/reply/:board', (req, res) => {
    MongoClient.connect(`${url}${req.params.board}`, (err, db) => {
        if (err) {
            return console.dir(err)
        }
        var collection = db.collection(req.params.board);
        var reply = {
            'text': req.params.text
        }
        collection.update({
            "_id": ObjectId(req.params._id)
        }, {
            $push: reply
        })
    });
});

Probably you wish to add res.sendStatus(200); just after collection.update() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM