简体   繁体   中英

Reverse-proxy to nodejs with Nginx returns a 502 when sending cookies

I have a nodejs express application which is behind an nginx reverse proxy. Everything works as it should, except that when I try to set cookies on a response, nginx returns a 502 page.

Here is the relevant route code:

officeAuth.getToken(req.query.code).then((data) => {
    const key = jwt.sign({access_token: data.access_token}, process.env.JWT_PRIVATE_KEY);
    const refreshKey = jwt.sign({refresh_token: data.refresh_token}, process.env.JWT_PRIVATE_KEY);
    res.cookie('token', key, {maxAge: data.expires_in * 24000, httpOnly: true});
    res.cookie('refresh', refreshKey, {maxAge: data.expires_in * 24000, httpOnly: true});
    res.redirect(process.env.APP_HOME_PAGE);
}, (err) => {
    res.status(500).send(err);
});

With this code, the nodejs log does not show any errors, and in-fact shows this request as returning a 302 as it should. However in the browser I get Nginx's 502 page.

When I remove the res.cookie statements from the code above, the redirect works fine.

Nginx config:

server {
    listen 443 ssl;

    server_name my.server.com;

    ssl_certificate /my/ssl/cert;
    ssl_certificate_key /my/ssl/key;

    location / {
        proxy_pass       http://localhost:3001;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
     }
}

Turns out my cookies were too large for nginx to handle, so I just increased the header size limit by adding:

proxy_buffers         8 16k;
proxy_buffer_size     16k;

to the location / block.

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