diff --git a/MainForm.cs b/MainForm.cs index 4880514..966946d 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -16,6 +16,8 @@ public sealed class MainForm : Form private NumericUpDown _numPort = null!; private TextBox _txtNginxExe = null!; private TextBox _txtConfigPath = null!; + private TextBox _txtPortalDomain = null!; + private NumericUpDown _numPortalPort = null!; private Label _lblStatus = null!; private Button _btnAdd = null!; @@ -27,6 +29,7 @@ public sealed class MainForm : Form private Button _btnReload = null!; private Button _btnStop = null!; private Button _btnRefreshStatus = null!; + private Button _btnSavePortal = null!; private string? _selectedDomain; @@ -66,7 +69,7 @@ public sealed class MainForm : Form BackColor = Color.FromArgb(245, 247, 250), AutoScroll = true }; - root.RowStyles.Add(new RowStyle(SizeType.Absolute, 96)); + root.RowStyles.Add(new RowStyle(SizeType.Absolute, 132)); root.RowStyles.Add(new RowStyle(SizeType.Absolute, 88)); root.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); root.RowStyles.Add(new RowStyle(SizeType.Absolute, 154)); @@ -83,21 +86,31 @@ public sealed class MainForm : Form var panel = new TableLayoutPanel { Dock = DockStyle.Fill, - ColumnCount = 4, - RowCount = 3, + ColumnCount = 5, + RowCount = 4, BackColor = Color.White, Padding = new Padding(10, 8, 10, 8) }; - panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 76)); + panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 84)); panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100)); + panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 88)); panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 104)); panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 104)); panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28)); panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28)); panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28)); + panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28)); _txtConfigPath = new TextBox { Dock = DockStyle.Fill, ReadOnly = true, BorderStyle = BorderStyle.FixedSingle }; _txtNginxExe = new TextBox { Dock = DockStyle.Fill, ReadOnly = true, BorderStyle = BorderStyle.FixedSingle }; + _txtPortalDomain = new TextBox { Dock = DockStyle.Fill, PlaceholderText = "例如:portal.example.com" }; + _numPortalPort = new NumericUpDown + { + Dock = DockStyle.Fill, + Minimum = 1, + Maximum = 65535, + Value = 80 + }; _lblStatus = new Label { Dock = DockStyle.Fill, @@ -114,18 +127,29 @@ public sealed class MainForm : Form btnBrowseExe.Click += (s, e) => BrowseNginxExe(); _btnRefreshStatus = CreateButton("刷新状态", 96); _btnRefreshStatus.Click += (s, e) => RefreshStatus(); + _btnSavePortal = CreateButton("保存入口设置", 96); + _btnSavePortal.Click += (s, e) => SavePortalSettings(); panel.Controls.Add(CreateLabel("配置文件"), 0, 0); panel.Controls.Add(_txtConfigPath, 1, 0); - panel.Controls.Add(btnOpenConfig, 2, 0); - panel.Controls.Add(btnOpenFolder, 3, 0); + panel.SetColumnSpan(_txtConfigPath, 2); + panel.Controls.Add(btnOpenConfig, 3, 0); + panel.Controls.Add(btnOpenFolder, 4, 0); panel.Controls.Add(CreateLabel("nginx.exe"), 0, 1); panel.Controls.Add(_txtNginxExe, 1, 1); - panel.Controls.Add(btnBrowseExe, 2, 1); + panel.SetColumnSpan(_txtNginxExe, 2); + panel.Controls.Add(btnBrowseExe, 3, 1); panel.SetColumnSpan(btnBrowseExe, 2); - panel.Controls.Add(CreateLabel("运行状态"), 0, 2); - panel.Controls.Add(_lblStatus, 1, 2); - panel.Controls.Add(_btnRefreshStatus, 2, 2); + panel.Controls.Add(CreateLabel("入口页域名"), 0, 2); + panel.Controls.Add(_txtPortalDomain, 1, 2); + panel.Controls.Add(CreateLabel("入口页端口"), 2, 2); + panel.Controls.Add(_numPortalPort, 3, 2); + panel.Controls.Add(_btnSavePortal, 4, 2); + panel.Controls.Add(CreateLabel("运行状态"), 0, 3); + panel.Controls.Add(_lblStatus, 1, 3); + panel.SetColumnSpan(_lblStatus, 2); + panel.Controls.Add(_btnRefreshStatus, 3, 3); + panel.SetColumnSpan(_btnRefreshStatus, 2); return panel; } @@ -232,7 +256,7 @@ public sealed class MainForm : Form _txtDescription = new TextBox { Dock = DockStyle.Fill, - PlaceholderText = "显示在 www.m3frame.com 入口卡片上的简介" + PlaceholderText = "显示在统一入口页卡片上的简介" }; _numPort = new NumericUpDown { @@ -353,6 +377,10 @@ public sealed class MainForm : Form { _txtConfigPath.Text = _manager.ConfigPath; _txtNginxExe.Text = _manager.NginxExePath; + _txtPortalDomain.Text = _manager.PortalDomain; + _numPortalPort.Value = _manager.PortalListenPort is >= 1 and <= 65535 + ? _manager.PortalListenPort + : 80; _txtNginxExe.BackColor = HasConfiguredNginxExe() ? Color.White : Color.FromArgb(255, 244, 214); @@ -377,6 +405,35 @@ public sealed class MainForm : Form _grid.DataSource = _binding; } + private void SavePortalSettings() + { + try + { + var domain = _txtPortalDomain.Text.Trim(); + if (!string.IsNullOrWhiteSpace(domain) && + (domain.Contains("://", StringComparison.Ordinal) || domain.Contains('/') || + domain.Any(char.IsWhiteSpace) || Uri.CheckHostName(domain) == UriHostNameType.Unknown)) + { + throw new ArgumentException("请输入有效的入口页域名,例如 portal.example.com;留空可关闭入口站点。"); + } + + _manager.UpdatePortalSettings(domain, (int)_numPortalPort.Value); + _manager.RenderConfig(); + RefreshBasicInfo(); + MessageBox.Show( + string.IsNullOrWhiteSpace(domain) + ? "入口站点已关闭,Nginx 配置已更新。" + : "入口页域名和监听端口已保存,Nginx 配置已更新。", + "NginxMan", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "NginxMan", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + private void RefreshStatus() { if (!HasConfiguredNginxExe()) @@ -601,7 +658,7 @@ public sealed class MainForm : Form if (domain.Contains("://", StringComparison.Ordinal) || domain.Contains('/') || domain.Any(char.IsWhiteSpace) || Uri.CheckHostName(domain) == UriHostNameType.Unknown) { - throw new ArgumentException("请输入有效域名,例如 team.m3frame.com。"); + throw new ArgumentException("请输入有效域名,例如 app.example.com。"); } if (!Uri.TryCreate(upstream, UriKind.Absolute, out var upstreamUri) || diff --git a/NginxManager.cs b/NginxManager.cs index 02e7e31..9d50a3d 100644 --- a/NginxManager.cs +++ b/NginxManager.cs @@ -33,12 +33,17 @@ public sealed class NginxManager _config = LoadOrCreateConfig(); ConfigureBundledNginx(); SaveConfig(); + RenderConfig(); } public string ConfigPath => _configPath; public IReadOnlyList Routes => _config.Routes.AsReadOnly(); + public string PortalDomain => _config.PortalDomain; + + public int PortalListenPort => _config.PortalListenPort; + public string NginxExePath { get => _config.NginxExePath; @@ -51,6 +56,24 @@ public sealed class NginxManager public bool IsRunning => Process.GetProcessesByName("nginx").Any(); + public void UpdatePortalSettings(string domain, int listenPort) + { + domain = NormalizeOptionalDomain(domain); + listenPort = NormalizePort(listenPort, "入口页监听端口"); + + if (!string.IsNullOrWhiteSpace(domain) && + _config.Routes.Any(route => + route.Domain.Equals(domain, StringComparison.OrdinalIgnoreCase) && + route.ListenPort == listenPort)) + { + throw new InvalidOperationException("入口页域名和端口不能与现有代理映射完全相同。"); + } + + _config.PortalDomain = domain; + _config.PortalListenPort = listenPort; + SaveConfig(); + } + public void AddOrUpdateRoute( string domain, string upstream, @@ -60,8 +83,10 @@ public sealed class NginxManager { domain = NormalizeDomain(domain); upstream = NormalizeUpstream(upstream); + listenPort = NormalizePort(listenPort, "监听端口"); websiteName = NormalizeRequiredText(websiteName, "网站名称"); description = NormalizeRequiredText(description, "网站简介"); + EnsureRouteDoesNotConflictWithPortal(domain, listenPort); var index = _config.Routes.FindIndex(x => x.Domain.Equals(domain, StringComparison.OrdinalIgnoreCase)); if (index >= 0) @@ -101,8 +126,10 @@ public sealed class NginxManager oldDomain = NormalizeDomain(oldDomain); newDomain = NormalizeDomain(newDomain); upstream = NormalizeUpstream(upstream); + listenPort = NormalizePort(listenPort, "监听端口"); websiteName = NormalizeRequiredText(websiteName, "网站名称"); description = NormalizeRequiredText(description, "网站简介"); + EnsureRouteDoesNotConflictWithPortal(newDomain, listenPort); var oldIndex = _config.Routes.FindIndex(x => x.Domain.Equals(oldDomain, StringComparison.OrdinalIgnoreCase)); if (oldIndex < 0) @@ -149,11 +176,6 @@ public sealed class NginxManager public string RenderConfig() { - if (_config.Routes.Count == 0) - { - throw new InvalidOperationException("至少需要 1 条域名映射才能生成配置。"); - } - var confPath = ResolvePath(_config.NginxConfPath); EnsureDirectory(Path.GetDirectoryName(confPath)); @@ -206,9 +228,10 @@ public sealed class NginxManager return DefaultConfig(); } - if (config.Routes is null || config.Routes.Count == 0) + config.Routes ??= []; + if (config.PortalListenPort is < 1 or > 65535) { - config.Routes = DefaultConfig().Routes; + config.PortalListenPort = 80; } NormalizeDataPaths(config); @@ -278,17 +301,20 @@ public sealed class NginxManager sb.AppendLine(" keepalive_timeout 65;"); sb.AppendLine(); - var portalRoot = Path.Combine(_dataDirectory, "www").Replace('\\', '/'); - sb.AppendLine(" server {"); - sb.AppendLine(" listen 80;"); - sb.AppendLine(" server_name www.m3frame.com;"); - sb.AppendLine($" root \"{portalRoot}\";"); - sb.AppendLine(" index index.html;"); - sb.AppendLine(" location / {"); - sb.AppendLine(" try_files $uri $uri/ /index.html;"); - sb.AppendLine(" }"); - sb.AppendLine(" }"); - sb.AppendLine(); + if (!string.IsNullOrWhiteSpace(_config.PortalDomain)) + { + var portalRoot = Path.Combine(_dataDirectory, "www").Replace('\\', '/'); + sb.AppendLine(" server {"); + sb.AppendLine($" listen {_config.PortalListenPort};"); + sb.AppendLine($" server_name {_config.PortalDomain};"); + sb.AppendLine($" root \"{portalRoot}\";"); + sb.AppendLine(" index index.html;"); + sb.AppendLine(" location / {"); + sb.AppendLine(" try_files $uri $uri/ /index.html;"); + sb.AppendLine(" }"); + sb.AppendLine(" }"); + sb.AppendLine(); + } foreach (var route in _config.Routes) { @@ -316,25 +342,9 @@ public sealed class NginxManager NginxConfPath = Path.Combine("data", "nginx.conf"), AccessLogPath = Path.Combine("data", "logs", "access.log"), ErrorLogPath = Path.Combine("data", "logs", "error.log"), - Routes = - [ - new RouteEntry - { - Domain = "team.m3frame.com", - Upstream = "http://127.0.0.1:803", - ListenPort = 80, - WebsiteName = "Team Workspace", - Description = "团队协作、项目推进与知识沉淀,让成员始终连接在同一节奏。" - }, - new RouteEntry - { - Domain = "git.m3frame.com", - Upstream = "http://127.0.0.1:3062", - ListenPort = 80, - WebsiteName = "Git Forge", - Description = "代码托管、版本审查与持续交付,把每一次提交变成可靠的软件。" - } - ] + PortalDomain = string.Empty, + PortalListenPort = 80, + Routes = [] }; private void ConfigureBundledNginx() @@ -382,22 +392,12 @@ public sealed class NginxManager { if (string.IsNullOrWhiteSpace(route.WebsiteName)) { - route.WebsiteName = route.Domain.ToLowerInvariant() switch - { - "team.m3frame.com" => "Team Workspace", - "git.m3frame.com" => "Git Forge", - _ => route.Domain - }; + route.WebsiteName = route.Domain; } if (string.IsNullOrWhiteSpace(route.Description)) { - route.Description = route.Domain.ToLowerInvariant() switch - { - "team.m3frame.com" => "团队协作、项目推进与知识沉淀,让成员始终连接在同一节奏。", - "git.m3frame.com" => "代码托管、版本审查与持续交付,把每一次提交变成可靠的软件。", - _ => $"访问 {route.Domain} 服务。" - }; + route.Description = $"访问 {route.Domain} 服务。"; } } } @@ -435,7 +435,37 @@ public sealed class NginxManager throw new ArgumentException("域名不能为空"); } - return domain.Trim().TrimEnd('.'); + domain = domain.Trim().TrimEnd('.'); + if (domain.Contains("://", StringComparison.Ordinal) || domain.Contains('/') || + domain.Any(char.IsWhiteSpace) || Uri.CheckHostName(domain) == UriHostNameType.Unknown) + { + throw new ArgumentException("请输入有效域名,例如 app.example.com。"); + } + + return domain; + } + + private static string NormalizeOptionalDomain(string domain) => + string.IsNullOrWhiteSpace(domain) ? string.Empty : NormalizeDomain(domain); + + private static int NormalizePort(int port, string fieldName) + { + if (port is < 1 or > 65535) + { + throw new ArgumentOutOfRangeException(nameof(port), $"{fieldName}必须在 1 到 65535 之间。"); + } + + return port; + } + + private void EnsureRouteDoesNotConflictWithPortal(string domain, int listenPort) + { + if (!string.IsNullOrWhiteSpace(_config.PortalDomain) && + _config.PortalDomain.Equals(domain, StringComparison.OrdinalIgnoreCase) && + _config.PortalListenPort == listenPort) + { + throw new InvalidOperationException("代理映射不能与入口页使用相同的域名和监听端口。"); + } } private static string NormalizeUpstream(string upstream) @@ -472,6 +502,8 @@ public sealed class NginxManagerConfig public string NginxConfPath { get; set; } = "nginx.conf"; public string AccessLogPath { get; set; } = Path.Combine("logs", "access.log"); public string ErrorLogPath { get; set; } = Path.Combine("logs", "error.log"); + public string PortalDomain { get; set; } = string.Empty; + public int PortalListenPort { get; set; } = 80; public List Routes { get; set; } = []; } diff --git a/README.md b/README.md index ec3c51f..9991a40 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,58 @@ # NginxMan -NginxMan 是一款面向 Windows 的可视化 Nginx 反向代理管理工具。程序将 Nginx 运行环境打包在单个 EXE 中,用图形界面管理域名、监听端口和上游服务,并自动维护统一的网站入口页。 +NginxMan 是一款面向 Windows 的可视化 Nginx 反向代理管理工具。程序将 Nginx 运行环境打包在单个 EXE 中,通过图形界面管理入口页、域名、监听端口和上游服务。 ## 软件目标 -NginxMan 主要解决以下问题: +NginxMan 主要用于: - 让不熟悉 Nginx 配置语法的用户也能建立反向代理。 -- 将多个本机或内网服务统一通过域名访问,例如将 `team.m3frame.com` 转发到 `http://127.0.0.1:803`。 -- 集中管理域名映射,避免手工编辑 `nginx.conf` 带来的格式错误。 -- 自动生成 `www.m3frame.com` 网站入口页,新增服务时无需再次修改网页源码或重新编译 EXE。 +- 将本机或内网中的多个服务通过不同域名和端口对外提供访问。 +- 集中管理代理映射,避免手工编辑 `nginx.conf` 带来的格式错误。 +- 根据用户配置自动生成统一的网站入口页。 - 将程序、Nginx、配置、日志和入口页集中放在一个可迁移目录中。 +程序首次运行时不会预置任何代理映射,也不会预设入口页域名。所有域名、端口、名称和简介均由用户填写。 + ## 核心功能 - **内置 Nginx**:首次运行时自动释放完整的 Windows Nginx 运行文件。 +- **入口页设置**:用户可以设置或关闭入口页域名,并为入口页指定独立的监听端口。 - **映射管理**:支持新增、修改和删除域名反向代理映射。 +- **可配置端口**:入口页和每条代理映射均可使用 `1–65535` 范围内的端口。 - **配置生成**:根据界面数据自动生成 `data/nginx.conf`。 - **进程控制**:可以启动、重新加载和停止 Nginx,并显示当前运行状态。 -- **入口页生成**:根据所有映射自动生成 `data/www/index.html`。 +- **入口页生成**:根据所有代理映射自动生成 `data/www/index.html`。 - **网页模板**:入口页样式保存在 `data/www/index.template.html`,入口卡片由程序动态填充。 -- **配置持久化**:映射保存在 `data/nginxman.json`,重启程序后继续使用。 -- **旧配置兼容**:旧版本中缺少网站名称和简介的记录会在启动时自动补齐。 +- **配置持久化**:设置保存在 `data/nginxman.json`,重启程序后继续使用。 - **自定义 Nginx**:既可以使用内置 Nginx,也可以在界面中选择其他 `nginx.exe`。 -## 映射字段 +## 配置组成 -每个网站入口包含以下信息: +### 入口页设置 -| 字段 | 作用 | 示例 | +入口页是可选的服务导航页面,配置包含: + +| 字段 | 作用 | 通用示例 | | --- | --- | --- | -| 网站名称 | 显示在入口页卡片上的名称 | `Team Workspace` | -| 域名 | 用户访问服务时使用的域名 | `team.m3frame.com` | -| 上游接口 | Nginx 实际转发到的服务地址 | `http://127.0.0.1:803` | -| 监听端口 | Nginx 对外监听的端口,通常为 80 | `80` | -| 网站简介 | 显示在入口页卡片上的说明 | `团队协作与项目管理平台` | +| 入口页域名 | 用于访问统一入口页;留空表示关闭入口站点 | `portal.example.com` | +| 入口页端口 | 入口页对外监听的端口 | `8088` | + +入口页域名和端口不会写死在源码中,也不需要通过重新编译程序来修改。 + +### 代理映射 + +每条代理映射包含: + +| 字段 | 作用 | 通用示例 | +| --- | --- | --- | +| 网站名称 | 显示在入口页卡片上的名称 | `Example Application` | +| 域名 | 用户访问目标服务时使用的域名 | `app.example.com` | +| 上游接口 | Nginx 实际转发到的服务地址 | `http://127.0.0.1:5000` | +| 监听端口 | 该域名对外监听的端口 | `8080` | +| 网站简介 | 显示在入口页卡片上的说明 | `示例业务应用` | + +监听端口并非固定为 80。界面中的 80 只是初始测试值,可以根据部署环境修改为其他可用端口。 ## 快速开始 @@ -46,47 +64,72 @@ NginxMan 主要解决以下问题: bin\NginxMan.exe ``` -首次运行时,程序会在 EXE 所在目录创建 `data` 文件夹,并准备 Nginx、配置、日志和入口页文件。 +首次运行时,程序会在 EXE 所在目录创建 `data` 文件夹,并准备 Nginx、配置、日志和入口页文件。代理映射列表初始为空。 -### 2. 新增网站入口 +### 2. 设置入口页(可选) -在界面下方的“编辑映射”区域依次填写: +在程序顶部填写: -1. 域名,例如 `docs.m3frame.com`。 -2. 上游接口,例如 `http://127.0.0.1:8080`。 -3. 监听端口,普通 HTTP 服务通常填写 `80`。 -4. 网站名称,例如 `Documentation`。 -5. 网站简介,例如 `团队文档与知识库`。 +1. 入口页域名,例如 `portal.example.com`。 +2. 入口页端口,例如 `8088`。 +3. 点击“保存入口设置”。 + +若不需要统一入口页,将入口页域名留空并保存即可。此时程序不会在 Nginx 配置中生成入口站点,但代理映射仍可正常使用。 + +### 3. 新增代理映射 + +在“编辑映射”区域依次填写: + +1. 域名,例如 `app.example.com`。 +2. 上游接口,例如 `http://127.0.0.1:5000`。 +3. 监听端口,例如 `8080`。 +4. 网站名称,例如 `Example Application`。 +5. 网站简介,例如 `示例业务应用`。 6. 点击“新增”。 -新增成功后,程序会立即完成以下操作: +新增成功后,程序会立即: - 保存 `data/nginxman.json`。 - 重新生成 `data/nginx.conf`。 - 将新入口写入 `data/www/index.html`。 -入口页内容是静态文件,保存后即可更新;如果 Nginx 已经运行,还需要点击“重新加载”,使新增的反向代理域名生效。 - -### 3. 启动或重新加载代理 - -- Nginx 尚未运行时,点击“启动代理”。 -- Nginx 已经运行且映射发生变化时,点击“重新加载”。 -- 需要关闭代理时,点击“停止代理”。 - -### 4. 配置 DNS - -每个域名都必须解析到运行 NginxMan 的服务器 IP。例如: +按上述示例配置后,代理访问地址为: ```text -docs.m3frame.com -> 服务器 IP +http://app.example.com:8080/ ``` -DNS 生效后,可通过 `http://docs.m3frame.com/` 访问对应的上游服务。 +### 4. 启动或重新加载代理 + +- Nginx 尚未运行时,点击“启动代理”。 +- Nginx 已运行且配置发生变化时,点击“重新加载”。 +- 需要关闭代理时,点击“停止代理”。 + +### 5. 配置 DNS 和防火墙 + +入口页域名及每个代理域名都需要解析到运行 NginxMan 的服务器 IP。非标准 HTTP 端口还需要在服务器防火墙或云安全组中开放。 + +例如: + +```text +portal.example.com -> 服务器 IP +app.example.com -> 服务器 IP +``` + +## 端口说明 + +- 可配置范围为 `1–65535`。 +- 80 只是方便测试普通 HTTP 的初始值,不是固定端口。 +- 访问非 80 端口时,需要在 URL 中显式写出端口号。 +- 入口页和代理映射可以使用不同端口。 +- 同一服务器上不能让两个不同程序同时占用相同的 IP 和端口组合。 +- 入口页不能与某条代理映射使用完全相同的域名和监听端口。 ## 界面按钮说明 | 按钮 | 作用 | | --- | --- | +| 保存入口设置 | 保存入口页域名与监听端口;域名留空表示关闭入口站点 | | 新增 | 使用当前表单内容创建一条新映射 | | 保存修改 | 保存列表中当前选中映射的修改 | | 删除 | 删除当前选中的映射 | @@ -102,13 +145,13 @@ DNS 生效后,可通过 `http://docs.m3frame.com/` 访问对应的上游服务 ## 数据目录 -发布后的主要目录结构如下: +发布后的主要目录结构: ```text bin\ ├─ NginxMan.exe └─ data\ - ├─ nginxman.json # 映射及程序配置 + ├─ nginxman.json # 入口页、端口及代理映射配置 ├─ nginx.conf # 自动生成的 Nginx 配置 ├─ nginx\ # 内置 Nginx 运行文件 ├─ logs\ # 访问日志和错误日志 @@ -117,19 +160,29 @@ bin\ └─ index.html # 自动生成的入口页 ``` -升级或替换 `NginxMan.exe` 时,请保留 `bin/data` 目录,以免丢失现有映射、模板和日志。 +升级或替换 `NginxMan.exe` 时,请保留 `bin/data` 目录,以免丢失用户配置、模板和日志。 ## 配置文件示例 -`data/nginxman.json` 中的每条映射包含网站信息和代理信息: +`data/nginxman.json` 的结构示例: ```json { - "domain": "docs.m3frame.com", - "upstream": "http://127.0.0.1:8080", - "listenPort": 80, - "websiteName": "Documentation", - "description": "团队文档与知识库" + "nginxExePath": "data\\nginx\\nginx.exe", + "nginxConfPath": "data\\nginx.conf", + "accessLogPath": "data\\logs\\access.log", + "errorLogPath": "data\\logs\\error.log", + "portalDomain": "portal.example.com", + "portalListenPort": 8088, + "routes": [ + { + "domain": "app.example.com", + "upstream": "http://127.0.0.1:5000", + "listenPort": 8080, + "websiteName": "Example Application", + "description": "示例业务应用" + } + ] } ``` @@ -177,17 +230,18 @@ bin\NginxMan.exe ### 无法启动 Nginx -- 检查 80 端口是否已被 IIS、Apache、其他 Nginx 或其他程序占用。 -- 检查 Windows 防火墙是否允许目标端口访问。 +- 检查所配置的监听端口是否已被 IIS、Apache、其他 Nginx 或其他程序占用。 +- 检查 Windows 防火墙或云安全组是否允许目标端口访问。 - 查看 `data/logs/error.log` 获取具体错误。 - 如使用外部 Nginx,确认界面中的 `nginx.exe` 路径正确。 ### 域名无法访问 - 确认域名 DNS 已解析到当前服务器。 +- 确认访问 URL 中的端口与监听端口一致。 - 确认上游服务正在运行,并能在服务器本机访问。 - 确认 Nginx 状态显示为“运行中”。 -- 修改映射后,如果 Nginx 已运行,请点击“重新加载”。 +- 修改配置后,如果 Nginx 已运行,请点击“重新加载”。 ### 入口页没有显示最新内容 @@ -198,5 +252,5 @@ bin\NginxMan.exe ## 使用限制 - 当前自动生成的是 HTTP 反向代理配置。 -- 如需 HTTPS,需要自行补充证书、443 监听及相关 Nginx 配置。 +- 如需 HTTPS,需要自行补充证书、TLS 监听及相关 Nginx 配置。 - 域名解析、证书申请和服务器防火墙配置不由 NginxMan 自动完成。 diff --git a/WebPortal.cs b/WebPortal.cs index e890298..77cc063 100644 --- a/WebPortal.cs +++ b/WebPortal.cs @@ -10,6 +10,7 @@ internal static class WebPortal private const string TemplateFileName = "index.template.html"; private const string OutputFileName = "index.html"; private const string ServiceCardsToken = "{{NGINXMAN_SERVICE_CARDS}}"; + private const string TemplateVersionMarker = ""; private static readonly UTF8Encoding Utf8WithoutBom = new(false); public static void ExtractTo(string dataDirectory) @@ -37,9 +38,14 @@ internal static class WebPortal throw new InvalidOperationException("Invalid embedded web resource path."); } - // Keep the extracted template editable. Future route changes only render - // index.html and never overwrite a customized index.template.html. - if (File.Exists(targetPath)) + // Preserve editable templates after the current template format has been + // installed. Older bundled templates without the version marker are + // upgraded once so legacy branding and layout do not remain in data/www. + var isCurrentTemplate = relativePath.Equals(TemplateFileName, StringComparison.OrdinalIgnoreCase) && + File.Exists(targetPath) && + File.ReadAllText(targetPath, Encoding.UTF8).Contains(TemplateVersionMarker, StringComparison.Ordinal); + if (File.Exists(targetPath) && + (!relativePath.Equals(TemplateFileName, StringComparison.OrdinalIgnoreCase) || isCurrentTemplate)) { continue; } diff --git a/nginxman.example.json b/nginxman.example.json index 88045a6..125aaa3 100644 --- a/nginxman.example.json +++ b/nginxman.example.json @@ -3,20 +3,7 @@ "nginxConfPath": "nginx.conf", "accessLogPath": "logs/access.log", "errorLogPath": "logs/error.log", - "routes": [ - { - "domain": "team.m3frame.com", - "upstream": "http://127.0.0.1:803", - "listenPort": 80, - "websiteName": "Team Workspace", - "description": "团队协作、项目推进与知识沉淀,让成员始终连接在同一节奏。" - }, - { - "domain": "git.m3frame.com", - "upstream": "http://127.0.0.1:3062", - "listenPort": 80, - "websiteName": "Git Forge", - "description": "代码托管、版本审查与持续交付,把每一次提交变成可靠的软件。" - } - ] + "portalDomain": "", + "portalListenPort": 80, + "routes": [] } diff --git a/web/index.html b/web/index.html index 60b8fe2..b0b8f4d 100644 --- a/web/index.html +++ b/web/index.html @@ -1,11 +1,12 @@ + - + - M3FRAME | 工作入口 + Service Portal | 服务入口