lu1her
lu1her

Reputation: 105

nginx as a reverse proxy for API cannot redirect full url, params, body headers

I'am putting my node js API (that I manage with pm2) behind a reverse proxy with nginx, here no problem.

But since, if I send any request it become a GET on '/'.

How can I tell to nginx to forward the full url, the request types (PUT, OPTIONS, DELETE, ...), the requests params and the request body ?

here is my simple nginx config.

server {
    listen      80;
    listen [::]:80;
    server_name api-prod.mysite.com www.api-prod.mysite.com localhost;

    location / {
        proxy_pass http://127.0.0.1:3111;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Upvotes: 0

Views: 872

Answers (1)

Arif Khan
Arif Khan

Reputation: 5069

Your nginx configuration seems good except, you should use proxy_http_version and proxy_cache_bypass because sometimes(depends on version) Nginx and nodejs using different HTTP version.

Following configuration working fine for me

server {
    listen 80;
    server_name arifjaunpur.com;
    location / {
        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;
    }
}

Upvotes: 1

Related Questions