我目前正在尝试为客户端接口(interface)-服务器交互构建 API。我决定将 ASP.NET Core 用于 API,并将 Nginx 作为托管平台(在 Ubuntu 18.04 上)。由于 ASP.NET 使用 Kestrel,我们设置了一个反向代理来将请求从 Nginx 转发到 Kestrel——托管 API 的东西。我们在 NGINX 服务器上设置了 SSL,但是它没有在 Kestrel 服务器上设置。
简单来说,就是不知道如何在 Kestrel Server 上设置 SSL,在 NGINX 端又多了一层 SSL。我怎样才能做到这一点?
模型:
客户端 --> 通过 HTTPS 获取请求 --> 使用 SSL 的 NGINX --> HTTP Kestrel 服务器,反之亦然
输出:SSL_PROTOCOL_ERROR
临时解决方案:在链接中使用带有端口 5000 的 HTTP。-- 没有错误,但是,数据不安全。
最佳解决方案:在链接中使用不带 5000 端口的 HTTPS。数据是安全的。
NGINX 配置:
if ($host = api.OURSITENAME.co) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name api.OURSITENAME.co;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
include /etc/nginx/proxy_params;
server_name api.OURSITENAME.co;
access_log /var/log/nginx/api.access.log;
error_log /var/log/nginx/api.error.log error;
# SSL Configuration
ssl_certificate /etc/letsencrypt/live/api.OURSITENAME.co/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.OURSITENAME.co/privkey.pem; # managed by Certbot
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
location / {
proxy_pass http://172.18.0.2:5000; <-- Docker Container. Can easily be switched out with localhost if we want to run on dotnet directly.
}
}
最佳答案
据我了解,当您使用 HTTP 直接在端口 5000 上访问您的应用程序时,您会收到 SSL 错误。即使您不使用 HTTPS。
如果您有 app.UseHsts();
和/或 app.UseHttpsRedirection();
在您的启动代码中,它将使用 HTTPS。
如果您让 nginx 处理 SSL,那么您可以从应用 Startup.cs 中删除代码
典型的启动代码:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else // Production
{
app.UseExceptionHandler("/Error");
// Remove to use HTTP only
app.UseHsts(); // HTTPS Strict mode
}
// Remove to use HTTP only
app.UseHttpsRedirection(); // Redirects HTTP to HTTPS
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
Documentation on enforcing SSL in dotnet core
关于asp.net - 带有 SSL 和 Asp.NET Core API 的 Nginx 反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61763229/