JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

.NET 6 Kestrel Web 部署之配置HTTP和HTTPS

wys521 2024-10-24 16:54:56 精选教程 19 ℃ 0 评论

最近用.NET6 开发了一个简单的小程序服务端,做过小程序的应该清楚,小程序请求接口需要HTTPS协议的地址。

大家都知道,在没有在启动时指定端口或没有在appsettings.json配置Endpoints时,默认是HTPP协议的5000端口


在以前,我一般也是才用默认的配置,然后,在前面在启一个nginx来开启HTTPS协议。我想了一下,就一个简单的查询小程序,没必要又加一层负载。所以,便跟着官网来进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "HttpsInlineCertFile": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "<path to .pfx file>",
          "Password": "<certificate password>"
        }
      },
      "HttpsInlineCertAndKeyFile": {
        "Url": "https://localhost:5002",
        "Certificate": {
          "Path": "<path to .pem/.crt file>",
          "KeyPath": "<path to .key file>",
          "Password": "<certificate password>"
        }
      }
    }  
  }
}

appsettings.json中,我们可以同时为我们的Web服务配置HTTPHTTPS两个Endpoint,在配置之前,我们需要准备好所需的SSL证书,这里可以选择IIS证书或者是NGINX证书,在IIS证书的压缩包内,会有*.pfx文件和密码;NGINX证书的压缩包内则有.pem.key两个文件。


我选择的是Nginx证书,具体配置如下图,

看启动的信息,貌似是万事俱备,只欠调用了,可是当我用https访问时,它给了我一个惊喜-无法访问

难道是Kestrel不支持这样配置https。我便继续看了看官网的文档,

看样子,是我少了一行配置,还需要加上"SslProtocols": ["Tls12", "Tls13"],

OK,这样一来,便能通过httphttps来访问我们的Kestrel Web服务

最后附上完整的Endpoints配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:80"
      },
      "HttpsInlineCertAndKeyFile": {
        "Url": "https://*:443",
        "SslProtocols": ["Tls12", "Tls13"],
        "Certificate": {
          "Path": "C:\\cert\\7430172_domain_nginx\\7430172_domain.com.pem",
          "KeyPath": "C:\\cert\\7430172_domain.com_nginx\\7430172_domain.com.key"
        }
      }
    }
  }

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表