NextChat icon indicating copy to clipboard operation
NextChat copied to clipboard

[Bug] x-forwarded-host not match 错误

Open ecator opened this issue 6 months ago • 4 comments

📦 部署方式

Docker

📌 软件版本

v2.16.0

💻 系统环境

Ubuntu

📌 系统版本

24.04.1 LTS

🌐 浏览器

Firefox

📌 浏览器版本

139.0.4 (64 位)

🐛 问题描述

docker部署,通过nginx反代后前端报下面的错误:

Image

当然聊天也不行,直接显示....

Image

后台查日志好像有下面的错误:

app-1  | `x-forwarded-host` header with value `127.0.0.1:3002` does not match `origin` header with value `这个地方是我的域名` from a forwarded Server Actions request. Aborting the action.
app-1  | Error: Invalid Server Actions request.
app-1  |     at rm (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:15:5881)
app-1  |     at rq (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:1255)
app-1  |     at /app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:3935
app-1  |     at AsyncLocalStorage.run (node:async_hooks:338:14)
app-1  |     at Object.wrap (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:13:16239)
app-1  |     at /app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:3825
app-1  |     at AsyncLocalStorage.run (node:async_hooks:338:14)
app-1  |     at Object.wrap (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:13:15487)
app-1  |     at rW (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:3752)
app-1  |     at no.render (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:7725)

nginx非常简单就是下面(一直用的这个没有问题的,至少v2.15.8是ok的):

proxy_buffering off;
  location / {
    proxy_pass http://127.0.0.1:3002;
  }

📷 复现步骤

docker部署2.16.0版本,然后通过nginx反代

🚦 期望结果

能正常使用

📝 补充信息

2.15.8没有这个问题,而且其他配置都没有动过,搜索了下好像是Next.js有这个问题?但是我看了下代码又没有这个配置,难道要加上? https://raddy.dev/blog/next-js-14-0-2-x-forwarded-host-header-with-value-localhost3000-does-not-match-origin-fix/

ecator avatar Jun 23 '25 16:06 ecator

找到一个临时解决方案,nginx增加如下配置就好了:

proxy_set_header x-forwarded-host $host;

ecator avatar Jul 18 '25 12:07 ecator

找到一个临时解决方案,nginx增加如下配置就好了:

proxy_set_header x-forwarded-host $host;

这个方案解决不了。 今天用 docker部署2.16.1版本,出现了一样的问题。 后来,恢复到 2.15.8,nginx配置未改,可以正常使用了。

wang20150419 avatar Aug 10 '25 13:08 wang20150419

Bot detected the issue body's language is not English, translate it automatically.


Find a temporary solution and add nginx to the following configuration:

proxy_set_header x-forwarded-host $host;

This solution cannot be solved. Today, when using docker to deploy version 2.16.1, the same problem occurred. Later, it was restored to 2.15.8, and the nginx configuration was not changed and it can be used normally.

Issues-translate-bot avatar Aug 10 '25 13:08 Issues-translate-bot

gemini 给了一个说法: 这是 Next.js 14 为了增强 Server Actions 的安全性而引入的一项检查,目的是防止跨站请求伪造(CSRF)攻击。当你的 Next.js 应用部署在 Nginx 反向代理后面时,你需要确保 Nginx 正确地将客户端(浏览器)的原始请求信息传递给后端的 Next.js 服务。

提出配置建议: location / { # 将请求反向代理到你的 Next.js 应用 proxy_pass http://localhost:3000;

    # --- 这是解决问题的关键配置 ---

    # 1. 将原始 Host 传递给后端
    proxy_set_header Host $host;

    # 2. 传递客户端的真实 IP 地址
    proxy_set_header X-Real-IP $remote_addr;

    # 3. 传递完整的 X-Forwarded-For 链
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # 4. 传递原始协议 (http 或 https)
    # 这对于 Next.js 生成正确的 URL 和处理安全 Cookie至关重要
    proxy_set_header X-Forwarded-Proto $scheme;

    # 5. 明确设置 X-Forwarded-Host,确保它与原始 Host 一致
    # 这是直接解决 "x-forwarded-host mismatch" 错误的核心
    proxy_set_header X-Forwarded-Host $host;
    
    # 为了支持 WebSocket(例如用于 HMR)
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

测试结果,问题依然存在。只好用 2.15.8版本了。

wang20150419 avatar Aug 11 '25 04:08 wang20150419