Nginx Config Creator (Enterprise Grade)
This skill automates the creation of Nginx/OpenResty reverse proxy configurations. It is designed for both ease of use and safety, incorporating environment awareness and a critical safety-check mechanism.
Features
- - Environment Awareness: Simplifies commands by reading configuration from environment variables.
- Safety Check: Includes a '熔断' (fuse) mechanism. It tests the configuration before applying it and automatically rolls back if the test fails, preventing web server downtime.
Pre-requisites (Recommended)
For maximum convenience, it is recommended to set the following environment variables on the host system:
- -
NGINX_CONFIG_PATH: The absolute path to the Nginx conf.d directory. - INLINECODE2 : The name of the running Nginx/OpenResty Docker container.
If these are not set, they must be provided as command-line arguments.
Core Action: scripts/create-and-reload.sh
This script performs the entire operation.
Inputs (Command-Line Arguments)
- -
--service-name: (Required) The short name for the service (e.g., grafana). - INLINECODE6 : (Required) The root domain name (e.g.,
example.com). - INLINECODE8 : (Required) The local port the service is running on (e.g.,
3000). - INLINECODE10 : (Optional) The path to Nginx's
conf.d directory. Overrides the NGINX_CONFIG_PATH environment variable. - INLINECODE13 : (Optional) The name of the Nginx Docker container. Overrides the
NGINX_CONTAINER_NAME environment variable.
Output
- - On Success: Prints a step-by-step log of its actions and a final success message.
- On Failure: Prints a descriptive error message to stderr and exits. If the failure occurs during the Nginx configuration test, the full error from
nginx -t is displayed.
Execution Workflow
- 1. Parse Arguments & Environment: The script gathers all necessary paths and names from command-line arguments and environment variables.
- Generate Config: It creates the
.conf file in the target directory. - Test Config (Safety Check): It executes
nginx -t inside the specified container. - Decide & Act:
- If the test passes, it proceeds to reload Nginx via
nginx -s reload.
- If the test fails, it
automatically deletes the generated file (rolls back) and reports the error.
- 5. Report Result: Informs the user of the final outcome.
Example Usage
Scenario 1: Environment variables are pre-set
CODEBLOCK0
Scenario 2: No environment variables (providing all info via arguments)
CODEBLOCK1
Failure Strategy
- - Missing Arguments: The script will exit with an error if required arguments/environment variables are missing.
nginx -t Fails: The skill is designed to be safe. It will not attempt to reload a broken configuration. It will clean up after itself and show you the exact error, ensuring the live web server is never affected.
Nginx 配置创建器(企业级)
本技能可自动创建 Nginx/OpenResty 反向代理配置。它兼顾易用性与安全性,集成了环境感知机制和关键的安全检查机制。
功能特性
- - 环境感知:通过读取环境变量简化命令操作。
- 安全检查:包含熔断机制。在应用配置前进行测试,若测试失败则自动回滚,防止 Web 服务器宕机。
前置条件(推荐)
为获得最大便利性,建议在主机系统上设置以下环境变量:
- - NGINXCONFIGPATH:Nginx conf.d 目录的绝对路径。
- NGINXCONTAINERNAME:正在运行的 Nginx/OpenResty Docker 容器名称。
如果未设置这些变量,则必须通过命令行参数提供。
核心操作:scripts/create-and-reload.sh
该脚本执行完整的操作流程。
输入(命令行参数)
- - --service-name:(必需)服务的简短名称(例如 grafana)。
- --domain:(必需)根域名(例如 example.com)。
- --port:(必需)服务运行的本地端口(例如 3000)。
- --config-path:(可选)Nginx conf.d 目录的路径。覆盖 NGINXCONFIGPATH 环境变量。
- --container-name:(可选)Nginx Docker 容器的名称。覆盖 NGINXCONTAINERNAME 环境变量。
输出
- - 成功时:打印逐步操作日志及最终成功消息。
- 失败时:向标准错误输出打印描述性错误消息并退出。如果失败发生在 Nginx 配置测试阶段,将显示 nginx -t 的完整错误信息。
执行流程
- 1. 解析参数与环境变量:脚本从命令行参数和环境变量中收集所有必要的路径和名称。
- 生成配置:在目标目录中创建 .conf 文件。
- 测试配置(安全检查):在指定容器内执行 nginx -t。
- 决策与执行:
- 如果测试通过,继续通过 nginx -s reload 重新加载 Nginx。
- 如果测试失败,
自动删除已生成的文件(回滚) 并报告错误。
- 5. 报告结果:告知用户最终结果。
使用示例
场景一:已预设环境变量
bash
为后续便利设置环境变量
export NGINX
CONFIGPATH=/path/to/your/nginx/conf.d
export NGINX
CONTAINERNAME=your
nginxcontainer
现在命令非常简单:
bash skills/nginx-config-creator/scripts/create-and-reload.sh \
--service-name grafana \
--domain example.com \
--port 3000
场景二:无环境变量(通过参数提供所有信息)
bash
bash skills/nginx-config-creator/scripts/create-and-reload.sh \
--service-name grafana \
--domain example.com \
--port 3000 \
--config-path /path/to/your/nginx/conf.d \
--container-name yournginxcontainer
失败处理策略
- - 缺少参数:如果缺少必需的参数/环境变量,脚本将退出并报错。
- nginx -t 失败:本技能设计为安全可靠。它不会尝试重新加载损坏的配置。它会自行清理并显示确切的错误信息,确保生产 Web 服务器不受影响。