簡體   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