我最近开发了一个 Blazor Server 应用程序,要求用户通过我的 Azure AD 登录才能访问该应用程序。该项目使用.NET 7并通过Docker容器发布。在使用 HTTPS 在 Visual Studio 中进行开发时,我可以使用 Azure AD 完美登录我的应用程序。
过去,我只是将 Blazor 项目托管为 Docker 容器,并使用 NGINX 作为反向代理。我的 NGINX 配置使用以下设置来指向我的任何容器并强制使用 HTTPS(此容器在 192.168.1.11:8081 处公开):
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /https/tags/server.crt;
ssl_certificate_key /https/tags/server.key;
# Configure the SignalR Endpoint
location / {
# App server url
proxy_pass http://192.168.1.11:8081;
# Configuration for WebSockets
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache off;
proxy_http_version 1.1;
# Configuration for LongPolling or if your KeepAliveInterval is longer than 60 seconds
proxy_read_timeout 100s;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
以及 HTTPS 重定向的默认配置:
# Default
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
但是,通过此设置,我无法对我的 Azure AD 进行身份验证。看来该请求仍然是通过 HTTP 协议发出的:
AADSTS50011: The redirect URI 'http://www.example.com/signin-oidc' specified in the request does not match the redirect URIs configured for the application
。我只是希望对我的应用程序注册的 HTTP 请求改为 HTTPS。
我在 nginx 配置中尝试了许多不同的方法,但这些当前设置是我成功访问容器的唯一方法。我还尝试将 ForwardedHeaders 添加到我的项目的 Program.cs 中,如下所示:stackoverflow.com/azure-ad-nginx-reverse-proxy-https,但它不能解决问题。我不确定接下来要尝试什么方法,因为到目前为止我发现的其他指南都非常过时。感谢您的帮助。
我也遇到了类似的问题,经过一番搜索后发现我的问题有两个问题。第一个是更改转发标头选项。
最初我的program.cs文件中有以下内容。
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
我删除了 ForwardedHeaders.XForwardedFor 选项,如下所示,这使得使用正确的方案 https 将redirectUri 返回到我的网站。这是在此答案评论
上建议的 app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
问题解决后,我收到来自 nginx 的 502 bad gateway 错误。错误日志显示这实际上是标头大小的问题
2023/08/11 19:47:54 [error] 24#24: *1 upstream sent too big header while reading response header from upstream
对此的一些研究使我找到了一些有关 nginx 代理缓冲区大小的信息。以这个答案为灵感,我将以下几行添加到我的 nginx conf 文件中,然后我就开始工作了!有关于此的一些文档 - nginx 代理缓冲区大小
proxy_buffer_size 16k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 16k;
我希望这能对您有所帮助。