简体   繁体   English

使用Nginx向Node.js进行反向代理发送Cookie时返回502

[英]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. 我有一个在nginx反向代理后面的nodejs express应用程序。 Everything works as it should, except that when I try to set cookies on a response, nginx returns a 502 page. 一切正常,但当我尝试在响应上设置cookie时,nginx返回502页。

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. 使用此代码,nodejs日志不会显示任何错误,并且实际上会将请求显示为应有的返回302。 However in the browser I get Nginx's 502 page. 但是,在浏览器中,我得到了Nginx的502页面。

When I remove the res.cookie statements from the code above, the redirect works fine. 当我从上面的代码中删除res.cookie语句时,重定向工作正常。

Nginx config: Nginx配置:

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: 原来我的Cookie太大了,nginx无法处理,因此我通过添加以下代码来增加标题大小限制:

proxy_buffers         8 16k;
proxy_buffer_size     16k;

to the location / block. location /街区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM