POST Request redirects to GET in Nginx proxy and NodeJS

February 22, 2019 1.9k views
Nginx Node.js Ubuntu 18.04

I have built a simple NodeJS app to demo REST API request handling. Then I used Nginx to proxy my Node app. Then for testing, I used Postman to do a GET request which returns:

"message": "Handling GET request to /products"

which is good. But changing in Postman from GET to POST and then sending the request, it returns the same answer.

"message": "Handling GET request to /products"

If I use curllocally to make the requests (both GET and POST) I recieve good responses.

I use PM2 and running server.js

server.js:

const http = require('http');
const app = require('./app');
const port = 3000;

const server = http.createServer(app);

server.listen(port);

app.js:

const express = require('express');
const app = express();
const productRoutes = require('./api/routes/products');

app.use('/products', productRoutes);

module.exports = app;

products.js:

const express = require('express');
const router = express.Router();

router.get('/', (req, res, next) => {
    res.status(200).json({
        message: 'Handling GET request to /products'
    });
});

router.post('/', (req, res, next) => {
    res.status(200).json({
    message: 'Handling POST request to /products'
    });
});

module.exports = router;

ngix site config

location / {
        try_files $uri $uri/ =404;
}

location /api {
    proxy_pass http://localhost:3000;
    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;
}

location /api/products {
    proxy_pass http://localhost:3000/products;
    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;
}

after I do the Postman POST request, here is the access.log from Nginx. The GET request was not called. It is just POST, though there is shown a GET request.

"POST /api/products HTTP/1.1" 301 178 "-" "PostmanRuntime/7.6.0"
"GET /api/products HTTP/1.1" 200 47 "http://flipit.ro/api/products" "PostmanRuntime/7.6.0"
The "-" shows me that POST request is not forwarded where it should.

I am new to Nginx/NodeJS so this may have a very simple and obvious solution, but don't judge me, as I am used to Apache/PHP. Also I am pretty bad at explaining things, but I hope you understand my problem.

2 Answers

try this:

location /api {
proxypass http://localhost:3000/api;
proxy
httpversion 1.1;
proxy
setheader Upgrade $httpupgrade;
proxysetheader Connection 'upgrade';
proxysetheader Host $host;
proxycachebypass $http_upgrade;
}

location /api/products {
proxypass http://localhost:3000/api/products;
proxy
httpversion 1.1;
proxy
setheader Upgrade $httpupgrade;
proxysetheader Connection 'upgrade';
proxysetheader Host $host;
proxycachebypass $http_upgrade;
}

it's a good practice to match location with backend URI.

Am having this problem also.when i use postman or curl i get normal post requests but when i intergrate it to a payment api from another domain the PPST requests are actually coming as 404 GET requests

Have another answer? Share your knowledge.