nginx + 用于调试的代码服务器反向代理在非浏览器 POST 请求上返回 401

问题描述 投票:0回答:2

[编辑于 20230428 添加有关 nginx 的信息]

我正在 React/Node 项目中设置 Stripe。为了支持支付工作流程,服务器必须监听两种类型的 POST 请求:

  1. /create-payment-intent
    。这个请求来自浏览器。我的工作没有问题。

  2. /webhook
    。这些请求来自 Stripe 服务器,我无法让它工作。我的服务器响应 http 错误 401,但我不知道为什么。

关于我的开发环境的更多背景信息:我在代码服务器上远程开发。这意味着当我调试时,传入的请求有两个反向代理可以通过:

  1. nginx 将

    https://<my-code-server-domain>
    的请求转发到运行在
    localhost:8082

    上的代码服务器
  2. 代码服务器将

    https://<my-code-server-domain>/proxy/3024/*
    的请求转发到运行在
    localhost:3024

    上的节点应用程序

例如:

https://<my-code-server-domain>/proxy/3024/create-payment-intent

https://<my-code-server-domain>/proxy/3024/webhook

这是在服务器上定义 POST 请求处理程序的方式:

const app = express()
app.post("/webhook",  express.raw({type: 'application/json'}), async (req, res) => {
    let event = req.body;
    ...

断点显示请求处理程序根本没有执行,因此必须在 express 调用处理程序之前返回请求。

怀疑是 cors 问题,因为接收浏览器 POST 请求有效,但接收非浏览器 POST 请求失败,我尝试添加“cors”NPM 模块并尝试手动设置更多的 liberate cors 标头,但我一直收到错误 401。

为了测试 Stripe webhook 事件,有一个可执行文件连接到 Stripe 服务器并将事件转发到您指定的任何 URL。我已经尝试在我的代码服务器开发服务器本身以及另一台 PC 上运行这个可执行文件,这没有任何区别。

我不确定如何从这里开始......我应该在哪里调试这个问题?代码服务器反向代理的设置可能有问题吗?它甚至可以是操作系统级别的东西吗?

在 nginx access.log 中,我可以看到对 /proxy/3024/create-payment-intent 的请求是如何用 200 应答的,对 /proxy/3024/webhook 的请求是如何用 401 应答的:

134.54.10.136 - - [28/Apr/2023:06:53:52 +0000] "POST /proxy/3024/create-payment-intent HTTP/1.1" 200 79 "https://nerd.gelovenleren.net/proxy/3022/edit/en/834a980b-ad65-4734-94ec-e1153f901a90/?payment_intent=pi_3N1kt9CPmkmZOx8V1AHUoWRa&payment_intent=pi_3N1kxmCPmkmZOx8V1GoNP6Fp&payment_intent_client_secret=...payment_intent_client_secret=...&redirect_status=succeeded&redirect_status=succeeded" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"
37.97.145.69 - - [28/Apr/2023:06:53:53 +0000] "POST /proxy/3024/webhook HTTP/1.1" 401 24 "-" "Stripe/1.0 (+https://stripe.com/docs/webhooks)"

在 nginx error.log 中,我找到了这段话,它正在处理 webhook 请求,其中提到了“401 Unauthorized”,但我无法解释它。这是 nginx 自己生成的东西,还是由于将请求转发到代码服务器而返回的东西??

2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http run request: "/proxy/3024/webhook?"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http upstream check client, write event:0, "/proxy/3024/webhook"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http upstream request: "/proxy/3024/webhook?"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http upstream process header
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 malloc: 000055DBE9E2AFA0:4096
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 recv: eof:0, avail:-1
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 recv: fd:29 241 of 4096
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy status 401 "401 Unauthorized"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header: "Content-Type: application/json; charset=utf-8"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header: "Content-Length: 24"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header: "ETag: W/"18-XPDV80vbMk4yY1/PADG4jYM4rSI""
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header: "Vary: Accept-Encoding"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header: "Date: Fri, 28 Apr 2023 06:53:52 GMT"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header: "Connection: close"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy header done
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 xslt filter header
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 HTTP/1.1 401 Unauthorized
Server: nginx/1.18.0 (Ubuntu)
Date: Fri, 28 Apr 2023 06:53:52 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 24
Connection: keep-alive
ETag: W/"18-XPDV80vbMk4yY1/PADG4jYM4rSI"
Vary: Accept-Encoding

2023/04/28 06:53:52 [debug] 2331377#2331377: *18 write new buf t:1 f:0 000055DBE9E30B50, pos 000055DBE9E30B50, size: 253 file: 0, size: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http write filter: l:0 f:0 s:253
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http cacheable: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http proxy filter init s:401 h:0 c:0 l:24
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http upstream process upstream
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe read upstream: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe preread: 24
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe buf free s:0 t:1 f:0 000055DBE9E2AFA0, pos 000055DBE9E2B079, size: 24 file: 0, size: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe length: 24
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 input buf #0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe write downstream: 1
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe write downstream flush in
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http output filter "/proxy/3024/webhook?"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http copy filter: "/proxy/3024/webhook?"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 image filter
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 xslt filter body
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http postpone filter "/proxy/3024/webhook?" 000055DBE9E2FF68
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 write old buf t:1 f:0 000055DBE9E30B50, pos 000055DBE9E30B50, size: 253 file: 0, size: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 write new buf t:1 f:0 000055DBE9E2AFA0, pos 000055DBE9E2B079, size: 24 file: 0, size: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http write filter: l:0 f:0 s:277
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http copy filter: 0 "/proxy/3024/webhook?"
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 pipe write downstream done
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 event timer: 29, old: 585225784, new: 585225788
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 http upstream exit: 0000000000000000
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 finalize http upstream request: 0
2023/04/28 06:53:52 [debug] 2331377#2331377: *18 finalize http proxy request

致以诚挚的问候, 维克

express stripe-payments nginx-reverse-proxy vscode-remote code-server
2个回答
0
投票

基于this page,这里是401错误是什么:

[它]表示客户端请求还没有完成,因为它缺少所请求资源的有效身份验证凭据。

所以看起来 Stripe 试图向您发送事件,但您的服务器不允许 Stripe 的请求并返回 401.

要解决此问题,您需要确保您的 webhook 端点 URL 可公开访问,如此处提到。如果需要,白名单 Stripe IP 地址.

调试此问题的一种方法是尝试向您的端点发送一个空的 POST 请求,并确保您的服务器没有响应

401
错误。
curl -X POST https://xxx/webhook


0
投票

代码服务器在进行端口转发时也需要身份验证。所以这是设计使然。在我的 GitHub 问题中查看更多信息; https://github.com/coder/code-server/issues/6174#issuecomment-1530072355

Nginx 与它无关,它只是转发从代码服务器获得的 401 错误。

解决方法是直接使用本地主机上的暴露端口。这仅在从运行代码服务器的机器发出请求时有效!

© www.soinside.com 2019 - 2024. All rights reserved.