c# - 编辑 : NGINX reverse proxy ASP. NET Core 5.0 : localhost:5000 closes connection but www. google.com 有效

标签 c# asp.net asp.net-core nginx .net-5

我正在尝试将我的 ASP.NET Web 应用程序(使用 .NET 5.0 和 ASP.NET MVC)部署到带有 NGINX 的 debian 10 服务器。我按照 Microsoft 的教程进行了操作,但经过多次测试后找不到可行的方法。

我当前的 NGINX 配置:

server {
    listen  80 default_server;

    location / {
        proxy_pass          http://localhost:5000;
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection keep-alive;
        proxy_set_header    Host $host;
        proxy_cache_bypass  $http_upgrade;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;
    }
}

使用 curl -v localhost:5000 查询本地主机给我 HTTP 307 重定向。

查询 curl -v --insecure https://localhost:5001 返回我网页的实际内容,因此 kestrel 运行良好。

我的 Startup.cs 看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using SmapOneUpdater.Frontend.Model.Sortenumbuchung.Data;

namespace SmapOneUpdater.Frontend
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders =
                    ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });
            services.AddDbContext<SortenumbuchungContext>(options =>
                    options.UseSqlite(Configuration.GetConnectionString("SortenumbuchungContext")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseForwardedHeaders();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Overview}/{action=Index}/{id?}");
            });
        }
    }
}

连接到我的服务器的 ip 地址在一些加载后返回“无法访问此站点 x.x.x.x 意外关闭连接”。它会自动将 url 更改为 x.x.x.x:5001,这应该是错误的,因为我并没有尝试直接访问端口 5001,还是我?

尽管如此,我很确定这不是我的 NGINX 配置,因为我的浏览器以某种方式获取了他需要端口 5001 的信息,并且因为如果我将 proxy_pass 更改为 https://www.google.com它有效并将我重定向到谷歌

编辑:已解决

问题出在我的 Properties/launchSettings.json 中。我将 applicationUrl 设置为 http://localhost:5000;https://localhost:5001 似乎 ASP 然后只允许调用 localhost(或 127.0.0.1)的连接。因为我在通过 ip 调用它时无法从服务器访问红隼。之后,我不得不更改 nginx 以转发服务器 IP 地址,因此访问客户端不会尝试访问 localhost:5001,而是尝试访问具有端口 5001 的服务器 IP 地址。

我现在使用 --urls 命令在运行时环境中设置 url。 所以我的电话看起来像这样:

dotnet /path/to/application/dll --urls "http://*:5000;https://*:5001"

至于 NGINX,在阅读 TheRoadrunner 的回答后,我更改了配置以将 HTTP 请求重定向到 HTTPS。正在关注the tutorial from NGINX我还创建了一个自签名 ssl 证书:

openssl req -x509 -subj /CN=localhost -days 365 -set_serial 2 -newkey rsa:4096 -keyout /etc/nginx/cert.key -nodes -out /etc/nginx/cert.pem

我的工作 NGINX 配置如下所示:

server {
    listen 80;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;

    ssl_certificate /etc/nginx/cert.pem;
    ssl_certificate_key /etc/nginx/cert.key;

    location / {
        proxy_pass          https://192.168.4.156:5001;
        proxy_set_header    Host $http_host;
        proxy_set_header    X-Real-IP               $remote_addr;
        proxy_set_header    X-Forwarded-For         $proxy_add_x_forwarded_for;
    }
}

有趣的是,使用这个工作配置,我在浏览器中的 url 栏中的端口没有显示,它正在访问端口 5001

我是怎么发现的

也许这对某些读者来说会很有趣。我通过使用 virtualbox 创建本地 debian 实例解决了这个问题。我将它配置为我的真实服务器。但我安装了 GNOME,因为我想确定我是否真的可以使用本地浏览器查看该页面(回想起来,我可能可以使用 curl 实现这一点)。

这对我来说似乎很有趣,因为重定向在虚拟机上有效。在 firefox 中输入 localhost:80 将我​​重定向到 localhost:5001,我看到了该页面。经过一些测试后,我用 VM 的 IP 尝试了它,并且遇到了与其他机器上完全相同的问题。所以我尝试更改应用程序 URL。

我认为这很有趣,因为它看起来很明显,但文档中从未提及。作为代理/nginx/webdev 的新手,我不太清楚这一切是如何工作的。特别是因为文档提到将 HTTPS 重定向到 http://localhost:5000

最佳答案

我认为您的 nginx 配置中需要两个部分,一个用于端口 80,另一个用于 443。

类似于:

server {
    listen 80;
   
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    
    location / {
        http://localhost:5000
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    }
}

此外,您还必须处理证书以避免浏览器警告。

关于c# - 编辑 : NGINX reverse proxy ASP. NET Core 5.0 : localhost:5000 closes connection but www. google.com 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65815372/

相关文章:

c# - 如何在 ASP.NET Core Web API 中添加两个不同的 token

asp.net - web.config 程序集元素和项目文件引用元素有什么区别

html - 如何处理在 ASP.NET MVC 中返回文件的 Controller 结果

asp.net-core - dotnet cli 项目上的 xunit

c# - 模型验证 ASP.NET Core MVC 的自定义错误消息

c# - Net Core错误检查文件解析器缺少文本

c# - 数据缓存技巧/技巧/AppFabric

c# - 如何使属性描述属性在 IntelliSense 中可见?

c# - WPF 自定义网格控件

.net - 如何在我的 ASP.net 应用程序中启用数据库日志记录