尝试将请求从服务器代理到Digital Ocean App Platform应用程序时遇到问题。
我有一个.NET 6应用程序在Digital Ocean应用程序平台上的Docker容器中运行。它运行得很好。我可以使用应用程序平台为我的应用程序指定的域从我的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错误。我相信这是来自数字海洋应用程序平台,而不是我的代理服务器,但不确定如何找到根本原因。
有没有人在使用Digital Ocean App Platform时遇到过这个问题,或者知道我做错了什么?
谢谢
3条答案
按热度按时间xxb16uws1#
我没有使用nginx,而是使用haproxy,但看到了同样的行为。我让它工作的方法是首先将
X-Forwarded-For
头部设置为Host
头部值。然后将Host
头部值设置为数字海洋应用平台主机。这让我的应用程序得到了预期的响应。
js81xvg62#
更新
我无法找到403 Permission Denied Cloudflare错误的原始错误的解决方案。我在Digital Ocean Community董事会上发布,但也没有任何运气。关于为什么Cloudflare返回403的细节并不多(返回带有403错误的空白白色页面,我在Digital Ocean社区论坛上找到了一个同样错误的问题,但也没有任何解决方案。
我想我会发布一个临时的解决方案,作为一个解决方案,直到我可以进一步解决这个问题。而不是设置
Host
头,我只是简单地添加了一个新的自定义头X-Host
,并将其设置为$host
。这会正确地传递给我在docker容器中运行的API。在我的.NET 6应用程序中,我首先检查
X-Host
头,看看它是否被设置,如果没有,则使用Host
头作为后备。我的Nginx配置现在看起来像这样...
字符串
如果这是CORS请求,您可能需要在Digital Ocean中设置CORS策略。您可以按照下面的指南进行设置。
https://docs.digitalocean.com/products/app-platform/how-to/configure-cors-policies/
数字海洋社区问题
https://www.digitalocean.com/community/questions/nginx-proxy-server-to-app-platform-app-is-getting-a-403-access-denied-cloudflare-error的
n53p2ov03#
我们最近解决了一个类似的问题(错误503)在我们的nginx反向代理(运行在数字海洋水滴)通过调整nginx配置的应用程序平台路径隐藏。具体来说,我们不得不添加proxy_ssl_server_name在位置块来解决错误。
字符串