簡體   English   中英

Nginx 反向代理,帶 SSL 和 Asp.NET 核心 ZDB974238714CA8DE634A7CE1D083A4

[英]Nginx Reverse Proxy With SSL and Asp.NET Core API

我目前正在嘗試為客戶端接口-服務器交互構建一個 API。 I have decided to use ASP.NET Core for the API with Nginx as the hosting platform (On Ubuntu 18.04). 由於 ASP.NET 使用 Kestrel,我們設置了一個反向代理來將請求從 Nginx 轉發到 Kestrel——托管 API 的東西。 我們在 NGINX 服務器上設置了 SSL,但在 Kestrel 服務器上沒有設置。

簡單來說就是不知道如何在Kestrel Server上設置SSL,在NGINX側再一層SSL。 我怎樣才能做到這一點?

Model: Client --> GET Request over HTTPS --> NGINX with SSL --> HTTP Kestrel Server and vice versa

Output:SSL_PROTOCOL_ERROR

臨時解決方案:使用 HTTP 與鏈路中的端口 5000。-- 沒有錯誤,但是,數據不安全。

最佳解決方案:使用 HTTPS 鏈路中沒有端口 5000。 數據是安全的。

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();

}

有關在 dotnet 核心中執行 SSL 的文檔

這很簡單,因為 Kestrel 未配置為處理 HTTPS 請求。 快速瀏覽MS Docs會向您展示如何操作。

您可以使用listenOptions及其各種擴展來指定您的 SSL 證書和其他配置

webBuilder.ConfigureKestrel(serverOptions =>
{
    serverOptions.ConfigureHttpsDefaults(listenOptions =>
    {
        // certificate is an X509Certificate2
        listenOptions.ServerCertificate = certificate;
    });
});

此外,如果您從Program.cs使用CreateDefaultBuilder ,那么您可以從appsettings.json配置 SSL/HTTPS,因為CreateDefaultBuilder默認調用Configure(context.Configuration.GetSection("Kestrel"))來加載 Kestrel 配置。 突出顯示 Kestrel 配置(來自 MS Docs)的示例配置文件如下所示:

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "HttpsInlineCertFile": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "<path to .pfx file>",
          "Password": "<certificate password>"
        }
      },
      "HttpsInlineCertStore": {
        "Url": "https://localhost:5002",
        "Certificate": {
          "Subject": "<subject; required>",
          "Store": "<certificate store; required>",
          "Location": "<location; defaults to CurrentUser>",
          "AllowInvalid": "<true or false; defaults to false>"
        }
      },
      "HttpsDefaultCert": {
        "Url": "https://localhost:5003"
      },
      "Https": {
        "Url": "https://*:5004",
        "Certificate": {
          "Path": "<path to .pfx file>",
          "Password": "<certificate password>"
        }
      }
    },
    "Certificates": {
      "Default": {
        "Path": "<path to .pfx file>",
        "Password": "<certificate password>"
      }
    }
  }
}

如果您需要更多信息,請務必訪問文檔

盡管如此,在我看來,雙層 SSL 保護並沒有太大優勢。 我可以立即注意到的一個缺點是加密/解密導致的一些延遲。 反向代理上的單層 SSL 應該綽綽有余。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM