NGINX代理服务器到数字海洋应用程序平台应用程序正在获得403访问权限-CloudFlare错误
我正在遇到一个问题,试图将服务器从我的Digital Ocean App Platform应用程序中委托请求。
我有一个.NET 6应用程序在Digital Ocean App Platform上的Docker容器中运行。这很好。我可以使用App Platform给我的应用程序给出的域,成功地从REST客户端点击我的API。
我现在要做的是将运行NGINX的服务器添加到从我的域上的代理请求到App Platform上的应用程序。
这是我的初始nginx配置。
server {
listen 443 ssl;
server_name ~^(?<subdomain>[\w-]+)\.mydomain\.com$ mydomain.com;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
location /api {
proxy_pass https://my-app-platform-app.ondigitalocean.app;
proxy_http_version 1.1;
}
}
此初始配置工作正常,我的API收到了请求,但是我的Docker容器中该请求的主机标头是Digital Ocean App Platform分配的域(my-app-platform-app.ondigitalocean.app),但希望我的域中的域服务器(mydomain.com)作为主机标头。因此,我所做的是在我的nginx配置中使用 host
标题, proxy_set_header
如下所示。
server {
listen 443 ssl;
server_name ~^(?<subdomain>[\w-]+)\.mydomain\.com$ mydomain.com;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
location /api {
proxy_set_header Host $host;
proxy_pass https://my-app-platform-app.ondigitalocean.app;
proxy_http_version 1.1;
}
}
现在,当我尝试从 mydomain.com/api
访问我的API时,我会拒绝403权限 - CloudFlare错误。我相信这来自数字海洋应用程序平台,而不是我的代理服务器,但不确定如何找到根本原因。
有没有人使用数字海洋应用程序平台遇到这个问题,或者知道我在做什么错?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新
我无法找到原始错误的解决方案,而403权限拒绝了Cloudflare错误。我在数字海洋社区委员会上发布了,但那里也没有任何运气。关于CloudFlare为什么要返回403的详细信息(返回一个空白的白页,带有403错误,没有详细信息),我在数字海洋中也无法找到任何东西。我确实在数字海洋社区董事会上找到了一个问题,但也没有任何解决方案。
我认为我会发布一个临时解决方案,该解决方案将作为解决方法,直到可以进一步解决此问题为止。而不是设置
host
标题我只是添加了一个新的自定义标题x-host
,然后将其设置为$ $ host
。这将正确传递给我在Docker容器中运行的API。在我的.NET 6应用中,我首先检查
X-host
标题,以查看是否设置并使用host
标题作为后备,如果不是。我的Nginx配置现在看起来像这样...
如果这是CORS请求,则可能必须在数字海洋中设置CORS策略。您可以按照下面的指南进行设置。
数字海洋社区问题
https://www.digitalocean.com/community/questions/nginx-proxy-server-server-to-plat--platform-pplot-is-is-petting-a-getting-a-403-access-access-denied-cloudflare-error
Update
I was unable to find a solutions to the original error with the 403 Permission Denied Cloudflare error. I posted on the Digital Ocean Community board but didn't have any luck there either. There isn't much details as to why Cloudflare is returning the 403 (returns a blank white page with 403 error, no details) nor could I find anything in Digital Ocean. I did find one questions on the Digital Ocean Community board with the same error but there wasn't any solution for it either.
I figured I'd post a temporary solution that I'm using as a workaround until I can troubleshoot this further. Instead of setting the
Host
header I simply just added a new custom headerX-Host
and set it to$host
. This gets passed properly to my API running in a docker container.In my .NET 6 app I check for the
X-Host
header first to see if it's set and use theHost
header as a fallback if it isn't.My Nginx config looks like this now...
If this is a CORS request you might have to setup a CORS policy in Digital Ocean. You can follow their guide below for setting that up.
https://docs.digitalocean.com/products/app-platform/how-to/configure-cors-policies/
Digital Ocean Community Question
https://www.digitalocean.com/community/questions/nginx-proxy-server-to-app-platform-app-is-getting-a-403-access-denied-cloudflare-error
我不是使用Nginx,而是使用Haproxy,而是看到了同样的行为。我完成的工作方式是首先将
x-forwarded-for
标题设置为主机
标题值。然后,我将主机
标题值设置为数字海洋应用平台主机。这使我的申请按预期做出了回应。
I wasn’t using nginx but rather haproxy, but saw this same behavior. The way I got it working was to first set the
X-Forwarded-For
header to theHost
header value. I then set theHost
header value to the digital ocean app platform host.This got my application to respond as expected.
最近,我们通过调整App Platform Path代理的NGINX配置来解决NGINX反向代理(在数字海洋液滴上运行)上的类似问题(错误503)。具体来说,我们必须在位置块中添加 proxy_ssl_server_name 才能解决错误。
We recently resolved a similar issue (error 503) on our nginx reverse proxy (running on a Digital Ocean Droplet) by tweaking the nginx config for the App Platform path proxying. Specifically, we had to add proxy_ssl_server_name in the location block to resolved the error.