Question

POST Request redirects to GET in Nginx proxy and NodeJS

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 curl locally 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.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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

Try this:

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

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