绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
.NET 反向代理-YARP 部署Https(SSL)
2022-09-22 16:44:50

  YARP 作为反向代理中间件,那就无可避免需要使用到 Https 去部署项目,那 YARP 要怎么去实现呢,本来以为 YARP 会有一套自己的实现,在翻阅了资料后发现,根本不是我想的那样,按照 YARP 官方文档的说法,是按照 .Net Core 原本的那一套去实现,好家伙,真的没想到啊,下面我贴出官方原文,大伙看一看,瞧一瞧


  IIS就不多说了,这个毕竟只能在 windows 上使用,下面我说说 在 Kestrel 怎么设置 Https 吧,按照我的惯例,直接贴配置文件

复制代码
"Kestrel": {
  "Endpoints": {
    "MySniEndpoint": {
      "Url": "https://*:5209",
      "SslProtocols": [ "Tls11", "Tls12" ],
      "Sni": {
        "test1.ysmc.net.cn": {
          "Certificate": {
            "Path": "[path]\\test1.ysmc.net.cn_server.pfx",
            "Password": "pfx密码"
          }
        },
        "test2.ysmc.net.cn": {
          "Certificate": {
            "Path": "[path]\\test2.ysmc.net.cn_server.pfx",
            "Password": "pfx密码"
          }
        }
      }
    }
  },
  //,默认配置,当没有配置的时候,默认回落到这个配置   
  "Certificates": {
    "Default": {
      "Path": "[path]\\test1.ysmc.net.cn_server.pfx",
      "Password": "pfx密码"
    }
  }
复制代码

  因为我们需要配置多个域名,所以使用到了 Sni,下面是官方对一 Sni 的部分介绍,感兴趣的小伙伴可以过去看看,传送门


 

SNI in configuration

Kestrel supports SNI defined in configuration. An endpoint can be configured with an object that contains a mapping between host names and HTTPS options. The connection host name is matched to the options and they are used for that connection.Sni

The following configuration adds an endpoint named that uses SNI to select HTTPS options based on the host name:MySniEndpoint

HTTPS options that can be overridden by SNI:

The host name supports wildcard matching:

  • Exact match. For example, matches .a.example.orga.example.org
  • Wildcard prefix. If there are multiple wildcard matches then the longest pattern is chosen. For example, matches and .*.example.orgb.example.orgc.example.org
  • Full wildcard. matches everything else, including clients that aren't using SNI and don't send a host name.*

The matched SNI configuration is applied to the endpoint for the connection, overriding values on the endpoint. If a connection doesn't match a configured SNI host name then the connection is refused.


 

下面一起看看配置后的效果吧,非常的完美


 

   整个完整的配置文件我也贴出来吧,至于证书怎么申请的,大家有域名的可以到域名服务商里申请免费1年期的,没有域名的话,可以自己改一下hosts 文件 然后自己自签名一个,都是可以的

appsettings.json

复制代码
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Kestrel": {
    "Endpoints": {
      "MySniEndpoint": {
        "Url": "https://*:5209",
        "SslProtocols": [ "Tls11", "Tls12" ],
        "Sni": {
          "test1.ysmc.net.cn": {
            "Certificate": {
              "Path": "[path]\\test1.ysmc.net.cn_server.pfx",
              "Password": "pfx密码"
            }
          },
          "test2.ysmc.net.cn": {
            "Certificate": {
              "Path": "[path]\\test2.ysmc.net.cn_server.pfx",
              "Password": "pfx密码"
            }
          }
        }
      }
    },
    "Certificates": {
      "Default": {
        "Path": "[path]\\test1.ysmc.net.cn_server.pfx",
        "Password": "pfx密码"
      }
    }
  },
  "ReverseProxy": {
    "Routes": {
      "baidu": {
        "ClusterId": "baidu",
        "Match": {
          "Hosts": [ "test1.ysmc.net.cn" ],
          "Path": "{**catch-all}"
        }
      },
      "blazor": {
        "ClusterId": "blazor",
        "Match": {
          "Hosts": [ "test2.ysmc.net.cn" ],
          "Path": "{**catch-all}"
        }
      }
    },
    "Clusters": {
      "baidu": {
        "LoadBalancingPolicy": "RoundRobin",
        "Destinations": {
          "baidu": {
            "Address": "https://www.baidu.com/"
          }
        }
      },
      "blazor": {
        "LoadBalancingPolicy": "RoundRobin",
        "Destinations": {
          "blazor": {
            "Address": "https://www.blazor.zone/"
          }
        }
      }
    }
  }
}
复制代码
分享好友

分享这个小栈给你的朋友们,一起进步吧。

.NET中大型研发必备
创建时间:2022-04-09 00:21:16
本系列文章适合有初/.NET知识的同学阅读(请在电脑上打开页面,获取更好的阅读效果)。 (1)本系列文章,旨在讲述研发一个中大型项目所需要了解的一系列“基本构件”,并提供这些“基本构件”在全网的【简单】、【快速】使用方法!!(并不深究技术原理) (2)通过阅读本系列文章,能让你在“正规”项目研发方面快速入门+进阶,并能达成“小团队构建大网站”的目的。 (3)本系列文章采用的技术,已成功应用到人工智能、产业互联网、社区电商、游戏、金融风控、智慧医疗、等项目上。
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • 红色侦察兵
    栈主

小栈成员

查看更多
  • miemieMIA
  • LCR_
  • xsy028
  • ?时光与海?
戳我,来吐槽~