Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 773
keemahs Avatar asked Jun 22 '26 11:06

keemahs


1 Answers

You need add

proxy_set_header Cookie $http_cookie;

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

like image 97
mariolu Avatar answered Jun 25 '26 01:06

mariolu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!