为什么 AWS Elastic Beanstalk 不传递自定义 JWT?

Why is AWS Elastic Beanstalk not passing the custom JWT?

我已经在 AWS EB 上成功部署了 Next.js with Next-Auth 项目。一切看起来都很好。但是,我无法通过登录表单。

这是浏览器控制台显示的内容:

502 Bad Gateway: POST http://----.elasticbeanstalk.com/api/auth/callback/credentials? Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

EB 日志显示如下:

upstream sent too big header while reading response header from upstream, client: x.x.x.x, server: , request: "POST /api/auth/callback/credentials? HTTP/1.1", upstream: "http://-----/api/auth/callback/credentials?", host: "----", referrer: "http://----.elasticbeanstalk.com/"

我还在 [...nextauth].tsx 文件中推送了一些控制台日志来查找问题。

所以它在 jwt 和会话回调之间的某个地方死了。

这是我在 .ebextensions/

下的配置

.ebextensions/proxy_custom.config:

files:
  "/etc/nginx/conf.d/proxy_custom.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      proxy_buffer_size   128k;
      proxy_buffers   4 256k;
      proxy_busy_buffers_size   256k;

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

这是一个更新的配置(基于我使用的配置),您可能想单独尝试它们以查看哪种配置最适合您。

files:
  "/etc/nginx/conf.d/proxy_custom.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      large_client_header_buffers 4 32k;
      fastcgi_buffers 16 32k;
      fastcgi_buffer_size 32k;
      proxy_buffer_size   128k;
      proxy_buffers   4 256k;
      proxy_busy_buffers_size   256k;

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

确保检查您的请求的实际 header 大小并相应地调整大小。 curl -s -w \%{size_header} -o /dev/null https://example.com 通过将 example.com 替换为您的服务 url 并在需要时通过 -H 添加请求 header。这将为您提供 header 字节大小。

don't set those buffers too high and use calculations specific to your app. Arbitrarily high values won't do good to your RAM, because those buffers are used per connection.

参考:https://www.getpagespeed.com/server-setup/nginx/tuning-proxy_buffer_size-in-nginx

问题是由于 cookie 太大而无法设置。

.ebextensions 从未被应用。这是因为该应用程序在 Amazon Linux 2 下。 Amazon Linux 2.

的配置需要在 .platform

这些是完成它的行:

proxy_buffer_size   128k;
proxy_busy_buffers_size   256k;
proxy_buffers   4 256k;

来自另一个 post 的 answer 有助于缩小问题范围。