如何调整Kestrel的端口

ASP.NET Core 中调整 Kestrel 服务器的端口可以通过多种方式实现。以下是详细的配置方法:


1. 通过 appsettings.json 配置文件

这是最推荐的方式,支持不同环境(Development/Production)的差异化配置。

json 复制代码
{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"  // 默认HTTP端口
      },
      "Https": {
        "Url": "https://localhost:5001" // 默认HTTPS端口
      }
    }
  }
}

修改示例(将 HTTP 改为 8080,HTTPS 改为 8443):

json 复制代码
{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:8080"
      },
      "Https": {
        "Url": "https://localhost:8443"
      }
    }
  }
}

2. 通过代码直接配置(Program.cs)

WebApplicationBuilder 构建阶段直接指定端口:

csharp 复制代码
var builder = WebApplication.CreateBuilder(args);

// 配置Kestrel端口
builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.ListenAnyIP(8080);    // 监听所有IP的8080端口(HTTP)
    serverOptions.ListenAnyIP(8443, listenOptions => 
    {
        listenOptions.UseHttps("certificate.pfx", "password"); // HTTPS配置
    });
});

var app = builder.Build();
app.Run();

3. 使用命令行参数

启动应用时通过命令行参数指定:

bash 复制代码
dotnet run --urls "http://localhost:8080;https://localhost:8443"

4. 环境变量配置

通过操作系统环境变量配置(适合容器化部署):

bash 复制代码
# Linux/macOS
export ASPNETCORE_URLS="http://localhost:8080;https://localhost:8443"

# Windows
set ASPNETCORE_URLS=http://localhost:8080;https://localhost:8443

5. 多端口监听配置

允许同时监听多个端口:

csharp 复制代码
builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.ListenAnyIP(8080);    // HTTP
    serverOptions.ListenAnyIP(8081);    // 第二个HTTP端口
    serverOptions.ListenAnyIP(8443, opts => opts.UseHttps()); // HTTPS
});

6. 动态端口绑定(随机端口)

让系统自动分配可用端口:

csharp 复制代码
builder.WebHost.UseUrls("http://*:0"); // 随机HTTP端口

通过代码获取实际分配的端口:

csharp 复制代码
var app = builder.Build();
app.Run();

// 获取实际端口(需在Run之后获取)
var port = app.Services.GetService<IServer>()
    .Features.Get<IServerAddressesFeature>()
    .Addresses.First().Split(':').Last();

7. 不同环境的差异化配置

appsettings.Development.jsonappsettings.Production.json 中分别定义:

开发环境(appsettings.Development.json)

json 复制代码
{
  "Kestrel": {
    "Endpoints": {
      "Http": { "Url": "http://localhost:5000" }
    }
  }
}

生产环境(appsettings.Production.json)

json 复制代码
{
  "Kestrel": {
    "Endpoints": {
      "Http": { "Url": "http://*:80" },
      "Https": { "Url": "https://*:443" }
    }
  }
}

验证配置是否生效

启动应用时观察控制台输出:

csharp 复制代码
Now listening on: http://localhost:8080
Now listening on: https://localhost:8443

总结

场景 推荐方式
开发环境快速修改 命令行参数或 launchSettings.json
生产环境固定端口 appsettings.json + 代码配置
容器化部署 环境变量配置
需要动态端口 代码绑定端口 0 + 获取实际端口

选择最适合你场景的方式,通常推荐使用 appsettings.json 进行集中化管理。

相关推荐
华仔啊29 分钟前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
武子康1 小时前
大数据-242 离线数仓 - DataX 实战:MySQL 全量/增量导入 HDFS + Hive 分区(离线数仓 ODS
大数据·后端·apache hive
砍材农夫2 小时前
TCP和UDP区别
后端
千寻girling2 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
千寻girling2 小时前
Python 是用来做 AI 人工智能 的 , 不适合开发 Web 网站 | 《Web框架》
人工智能·后端·算法
贾铭2 小时前
如何实现一个网页版的剪映(三)使用fabric.js绘制时间轴
前端·后端
xiaoye20183 小时前
Spring 自定义 Redis 超时:TTL、TTI 与 Pipeline 实战
后端
程序员爱钓鱼6 小时前
GoHTML解析利器:github.com/PuerkitoBio/goquery实战指南
后端·google·go
golang学习记6 小时前
从“大泥球“到模块化单体:Spring Modulith + IntelliJ IDEA 拯救你的代码
后端·intellij idea
颜酱6 小时前
一步步实现字符串计算器:从「转整数」到「带括号与优化」
javascript·后端·算法