繁体   English   中英

编辑: NGINX 反向代理 ASP.NET Core 5.0: localhost:5000 关闭连接但 www.google.com 工作

[英]EDIT: NGINX reverse proxy ASP.NET Core 5.0: localhost:5000 closes connection but www.google.com works

I'm trying to deploy my ASP.NET web Application (with .NET 5.0 and ASP.NET MVC) to an debian 10 server with NGINX. 我按照微软的教程进行了多次测试,但找不到可行的方法。

我目前的 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查询 localhost 会给我 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 地址在加载后返回“无法访问此站点 xxxx 意外关闭了连接”。 它会自动将 url 更改为 xxxx:5001 这应该是错误的,因为我没有尝试直接访问端口 5001,还是我?

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

编辑:已解决

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

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

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

至于 NGINX,在阅读了 TheRoadrunner 的回答后,我将配置更改为将 HTTP 请求重定向到 HTTPS。 按照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 实现了这一点)。

这对我来说似乎很有趣,因为重定向在 vm 上起作用。 在 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;
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM