keemahs
keemahs

Reputation: 998

nginx reverse proxy cookie forwarding

i have 3 heroku apps

  1. frontend react
  2. backend node
  3. reverse-proxy nginx

  1. calls to reverse-proxy/api/?(.*) are frowarded to backend
  2. rest all calls to reverse-proxy are forwarded to frontend

the /etc/nginx/conf.d/default.conf code

upstream frontend {
    server $FRONTEND_URL;
}

upstream backend {
    server $BACKEND_URL;
}

server {
    listen $PORT;

    location / {
        proxy_pass http://frontend;
        proxy_set_header Host $FRONTEND_URL;
    }

    location /api {
        rewrite /api/(.*) /$1 break;
        proxy_pass http://backend;
        proxy_set_header Host $BACKEND_URL;
    }

}

issue

i am using cookie for authentication but the cookie being set by backend is not being 'forwarded'

my code


now it works, changes i made:

  1. changing to secure: false in my node app did it for me (will add tls certificate later maybe)
  2. suggested fix by @mariolu

now it looks like

location /api {
    rewrite /api/(.*) /$1 break;
    proxy_pass http://backend;
    proxy_set_header Host $BACKEND_URL;
    proxy_set_header Cookie $http_cookie;
}
  1. app.set("trust proxy", true);

Upvotes: 8

Views: 17485

Answers (1)

mariolu
mariolu

Reputation: 700

You need add

proxy_set_header Cookie $http_cookie;

in location config. Variable $http_cookie is user request cookie.

Upvotes: 9

Related Questions