简体   繁体   中英

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.

I have found the answer. In postman I was calling

example.com/api/products

But as you see in Nginx config, the server listens on HTTPS. So it works with thihs HTTPS Postman call.

https://example.com/api/products

I believe this is an nginx issue and not a Node or Postman issue. Regardless of the HTTP method ( GET or POST ), a request to /api/products will result in a match on location /api . I think you'll need to use the = modifier to do an exact match.

See this Digital Ocean article on matching location blocks.

Hope that helps!

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