Home > OS >  Nginx changes POST to GET using proxy_pass
Nginx changes POST to GET using proxy_pass

Time:09-18

I want to use Nginx create a gateway to receive requests and pass them along to a network of microservices.

What I need Nginx to do is just act as a proxy server, taking the requests, passing them along to whatever service, and returning the response without any changes.

This is my configuration for my local setup:

server {
    listen 8080;

    location /api/register/ {
        proxy_pass http://micro_auth_backend:8082;
    }

    location /api/location/ {
        proxy_pass http://localhost:8088;
    }

}

It works correctly for GET requests, but when doing a POST call, the server will always receive a GET response.

I have tried adding some more configs inside the location, such as this example below, but so far nothing has worked:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Any suggestions would be appreciated. Thank you

CodePudding user response:

you can add this code to your nginx.conf file. It works for me.

location /api/register/ {
    proxy_pass  http://localhost:8082;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

location /api/location/ {
    proxy_pass  http://localhost:8088;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

CodePudding user response:

Just removed the trailing slash on location:

location /api/register {
    proxy_pass http://micro_auth_backend:8082;
}

Now it works.

  • Related