通过反向代理在 Nginx http 服务器中使用 Notion API,Cloudflare 返回 403 Forbidden 错误

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

我们正在尝试在我们的 Web 应用程序中使用 Notion API。在开发中,我们使用 vite 代理,在生产中使用 Nginx 来访问前端的 Notion API。

我们的 vite 代理配置如下,在我们的开发过程中运行良好:

'/api/notion/database': {
    target: `https://api.notion.com/v1/databases/${process.env.VITE_NOTION_DATABASE_ID}`,
    changeOrigin: true,
    rewrite: (path: string) =>
        path.replace(/^\/api\/notion\/database/, '') as string,
    headers: {
        Authorization: `Bearer ${process.env.VITE_NOTION_API_KEY}`,
        'Notion-Version': '2022-06-28',
        'Content-Type': 'application/json',
    },
},

但是,当我们使用Nginx时,它会返回403 Forbidden错误或421,如下所示:

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<html>
<head><title>421 Misdirected Request</title></head>
<body>
<center><h1>421 Misdirected Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

由于错误消息中包含 Cloudflare,因此看起来错误来自 Cloudflare 而不是 Notion。当我尝试寻找解决方案时,我发现它与 WAF 和 SSL 问题有关。我们在 Nginx 中使用 HTTP 是因为我们通过外部应用程序负载均衡器而不是 Nginx 本身将 SSL 证书应用于我们的 Web 应用程序。

我们添加了

proxy_ssl_server_name on
proxy_ssl_name $proxy_host
作为一些互联网资源,但仍然没有解决问题。

这是我们的 Nginx 配置的一部分,您有任何线索来解决这个问题吗?

location /api/notion/database {
    proxy_ssl_server_name on;
    proxy_ssl_name $proxy_host;

    rewrite ^/api/notion/database(.*)$ /v1/databases/NOTION_DATABASE_ID$1?$args break;

    proxy_pass https://api.notion.com;

    proxy_set_header   Authorization "Bearer NOTION_API_KEY";
    proxy_set_header   Notion-Version "2022-06-28";
    proxy_set_header   Content-Type "application/json";

    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Host $host;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-Proto $scheme;
}
http nginx cloudflare nginx-reverse-proxy notion-api
1个回答
0
投票

好吧,我自己找到了解决方案。我与其他人分享这个解决方案。

问题与 Notion 或 Cloudflare 无关。它更接近路径重写问题。主要关键就是在重写中添加“/”并删除

proxy_set_header   Host $host;

location /api/notion/database {
    proxy_ssl_server_name on;
    proxy_ssl_name $proxy_host;

    rewrite ^/api/notion/database/(.*)$ /v1/databases/NOTION_DATABASE_ID/$1 break;

    proxy_pass https://api.notion.com;

    proxy_set_header Authorization "Bearer NOTION_API_KEY";
    proxy_set_header Notion-Version "2022-06-28";
    proxy_set_header Content-Type "application/json";
    proxy_http_version 1.1;
}
© www.soinside.com 2019 - 2024. All rights reserved.