Wishlist 0 ¥0.00

在 Windows 上为 Nginx 配置图形管理界面的实践指南

在现代服务器管理中,Nginx 作为高性能的 Web 服务器和反向代理工具,广泛应用于各种应用场景。然而,对于 Windows 用户来说,Nginx 的命令行配置可能不够直观,尤其是在需要快速设置反向代理(如为 Linkding 应用配置)时。本文基于我与一位 Windows 用户的交流,整理了在 Windows 环境下为 Nginx 配置图形管理界面的完整实践过程,涵盖问题分析、解决方案、替代方案及故障排查,旨在帮助读者高效管理 Nginx。

背景与目标

用户在 Windows 系统上运行 Nginx 1.28.0(安装路径 D:\program\nginx),通过 NSSM(Non-Sucking Service Manager)成功配置为系统服务,目标是为运行在 http://localhost:8000 的 Linkding 应用设置反向代理,使其通过 http://localhost 访问。由于手动编辑 nginx.conf 配置复杂,用户希望引入图形管理界面,简化反向代理配置和 Nginx 管理。

初始问题包括:

  • NSSM 服务状态显示 Stopped,但 Nginx 进程(PID 2304 和 7636)仍在运行,导致 http://localhost 显示“Welcome to nginx!”页面。
  • 用户尝试安装 Nginx UI,但未找到 Windows 二进制文件(nginx-ui-windows-amd64.exe),且发现的 nginx-ui-macos-64.tar.gznginx-ui-darwin-amd64.tar.gz 是 macOS 版本。
  • 用户明确要求非 Docker 方案,排除 Nginx Proxy Manager 的默认部署方式。

最终目标:

  • 解决 Nginx 进程残留问题,确保服务状态一致。
  • 在 Windows 上部署图形管理工具(非 Docker),配置 Linkding 反向代理。
  • 提供多种替代方案,满足不同需求。

问题分析

1. Nginx 进程残留

通过以下命令检查服务和进程状态:

Get-Service nginx -ErrorAction SilentlyContinue
Get-Process nginx -ErrorAction SilentlyContinue

输出显示:

Status   Name               DisplayName
------   ----               -----------
Stopped  nginx              Nginx Web Server

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    148      10     1612       8020       0.03   2304   2 nginx
    144      12     2060       8432       0.08   7636   2 nginx

分析:

  • NSSM 服务停止,但 Nginx 进程仍在运行,说明可能是手动运行了 nginx.exe 或 NSSM 未正确终止进程。
  • 80 端口被占用,导致 http://localhost 显示默认页面。
  • 解决方法:终止残留进程,释放 80 端口,重新启动 NSSM 服务。

2. Nginx UI 二进制缺失

用户尝试安装 Nginx UI(https://github.com/0xJacky/nginx-ui),但未找到 Windows 版本的 nginx-ui-windows-amd64.exe。发现的 nginx-ui-macos-64.tar.gznginx-ui-darwin-amd64.tar.gz 是 macOS 版本:

  • 文件解析
    • macos-64darwin-amd64 均针对 macOS(Darwin 内核),64 位架构,压缩为 .tar.gz 格式。
    • 不适用于 Windows,需寻找 .exe 文件或编译。
  • 原因
    • Nginx UI 最新版本(v2.0.0-beta.38)可能未发布 Windows 二进制。
    • 早期版本(v1.3.3 等)可能包含,但需回溯检查。
    • Windows 支持较弱,需手动编译(Go 和 Node.js)。

3. 非 Docker 需求

用户明确要求非 Docker 方案,排除 Nginx Proxy Manager 的默认部署(需 Docker Desktop)。分析:

  • Nginx Proxy Manager 非 Docker 部署在 Windows 上复杂,需手动配置 Nginx、Node.js 和数据库,无官方支持。
  • 需寻找 Windows 原生的图形管理工具,如 WinNMP、NGINX-GUI,或优化手动配置。

解决方案

1. 解决 Nginx 进程残留

为确保 Nginx 服务状态与进程一致,执行以下步骤:

  1. 终止残留进程

    Stop-Process -Name nginx -Force -ErrorAction SilentlyContinue
    Get-Process nginx -ErrorAction SilentlyContinue
    

    确认无 Nginx 进程输出。

  2. 检查 80 端口

    netstat -ano | findstr :80
    

    确保无 0.0.0.0:80 LISTENING 输出。若有其他进程(如 IIS),终止:

    net stop w3svc
    
  3. 启动 NSSM 服务

    net start nginx
    Get-Service nginx
    

    预期:

    Status   Name               DisplayName
    ------   ----               -----------
    Running  nginx              Nginx Web Server
    
  4. 验证

    • 检查进程:
      Get-Process nginx
      
    • 访问 http://localhost,确认是否显示默认页面(后续配置反向代理)。
  5. 优化 NSSM 配置
    net stop nginx 仍失败,重新配置 NSSM:

    D:\nssm\win64\nssm.exe remove nginx
    D:\nssm\win64\nssm.exe install nginx
    

    设置:

    • Path:D:\program\nginx\nginx.exe
    • Startup directory:D:\program\nginx
    • Startup type:Automatic
    • Shutdown:启用“Terminate process”。

2. 尝试 Nginx UI

尽管未找到 Windows 二进制,提供了以下方法:

  1. 查找二进制

    • 访问 https://github.com/0xJacky/nginx-ui/releases。
    • 检查最新版本(v2.0.0-beta.38)及早期版本(v1.3.3)的 Assets,寻找 nginx-ui-windows-amd64.exenginx-ui.exe
    • 若无,搜索 GitHub Issues 或提交问题询问。
  2. 编译 Nginx UI

    • 安装 Go(1.23.x)、Node.js(20.x)、Git。
    • 克隆仓库:
      New-Item -ItemType Directory -Path D:\program\nginx-ui-source
      cd D:\program\nginx-ui-source
      git clone https://github.com/0xJacky/nginx-ui.git .
      
    • 安装依赖:
      npm install
      
    • 编译:
      go generate
      go build -tags=jsoniter -ldflags "-X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(Get-Date -UFormat %s)'" -o nginx-ui.exe -v main.go
      
    • 运行:
      Move-Item nginx-ui.exe D:\program\nginx-ui\nginx-ui.exe
      cd D:\program\nginx-ui
      .\nginx-ui.exe
      
  3. 配置 Nginx UI

    • 创建 app.ini
      [server]
      run_mode = prod
      http_port = 9000
      http_addr = 0.0.0.0
      
      [nginx]
      config_dir = D:\program\nginx\conf
      binary = D:\program\nginx\nginx.exe
      log_dir = D:\program\nginx\logs
      
      [app]
      jwt_secret = my-secret-123
      
    • 访问 http://localhost:9000,注册并登录。

3. 配置 Linkding 反向代理

无论使用哪种工具,反向代理配置一致:

  1. 确认 Linkding 运行

    netstat -ano | findstr :8000
    

    若无输出,启动 Linkding:

    cd <linkding_directory>
    .\venv\Scripts\activate
    python manage.py runserver
    
  2. 编辑 nginx.conf

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
        }
    }
    
  3. 验证和重启

    cd D:\program\nginx
    .\nginx.exe -t
    net stop nginx
    net start nginx
    

    访问 http://localhost,预期显示 Linkding。

4. 替代图形管理工具

由于 Nginx UI 的 Windows 二进制难以获取,推荐以下 Windows 原生工具:

WinNMP

  • 特点:Windows 下的 Nginx、MySQL、PHP 管理工具,提供图形界面。
  • 安装
  • 配置:通过界面编辑反向代理配置,保存并重启 Nginx。
  • 优点:简单易用,兼容现有 Nginx。
  • 局限:功能基础,SSL 配置需手动。

NGINX-GUI

手动配置 + 辅助工具

  • 方法
  • 优点:无需额外软件,灵活。
  • 局限:无图形界面,依赖手动操作。

5. Docker 可选方案

虽然用户要求非 Docker,但 Nginx Proxy Manager 是最成熟的图形管理工具,Docker 在 Windows 上运行稳定。若考虑:

  • 安装 Docker Desktop:https://www.docker.com/products/docker-desktop/。
  • 部署 Nginx Proxy Manager:
    version: '3.8'
    services:
      app:
        image: 'jc21/nginx-proxy-manager:latest'
        restart: unless-stopped
        ports:
          - '80:80'
          - '443:443'
          - '81:81'
        volumes:
          - ./data:/data
          - ./letsencrypt:/etc/letsencrypt
    
    cd D:\docker\nginx-proxy-manager
    docker-compose up -d
    
  • 访问 http://localhost:81,配置 Linkding 反向代理。

故障排查

  1. Nginx 进程残留

    • 终止进程:
      Stop-Process -Name nginx -Force
      
    • 检查端口:
      netstat -ano | findstr :80
      
  2. Linkding 无法访问

    • 确认 8000 端口:
      netstat -ano | findstr :8000
      
    • 检查日志:
      Get-Content D:\program\nginx\logs\error.log -Tail 10
      
  3. 图形工具问题

    • WinNMP:确认 Nginx 路径和端口。
    • NGINX-GUI:检查 Java 版本。
    • Nginx UI 编译:验证 Go 和 Node.js 版本,分享错误日志。
  4. 权限问题

    • 以管理员身份运行 PowerShell:
      icacls D:\program\nginx /grant Users:F
      

总结与建议

通过与用户的交流,我们解决了 Nginx 进程残留问题,探索了多种 Windows 原生的图形管理工具,并提供了手动配置的优化方法。主要收获:

  • Nginx UI:Windows 二进制难以获取,编译复杂,不推荐初学者。
  • WinNMP 和 NGINX-GUI:适合 Windows,简单易用,推荐优先尝试。
  • 手动配置:结合 NSSM 和辅助工具,灵活但需经验。
  • Docker:Nginx Proxy Manager 是最成熟方案,若资源允许可考虑。

建议

  1. 优先尝试 WinNMP,下载安装后配置反向代理,测试 http://localhost
  2. 若需轻量方案,使用 NGINX-GUI,确保 Java 环境。
  3. 手动配置作为备用,借助 VS Code 和在线工具。
  4. 若未来考虑 Docker,Nginx Proxy Manager 提供最佳体验。

用户可根据需求选择方案,执行以下命令验证:

net stop nginx
netstat -ano | findstr :80
netstat -ano | findstr :8000
net start nginx
Get-Content D:\program\nginx\logs\error.log -Tail 10

本文总结了 Windows 下 Nginx 图形管理的实践经验,希望为读者提供清晰的指导。如有问题,欢迎反馈!

No comments

About Us

Since 1996, our company has been focusing on domain name registration, web hosting, server hosting, website construction, e-commerce and other Internet services, and constantly practicing the concept of "providing enterprise-level solutions and providing personalized service support". As a Dell Authorized Solution Provider, we also provide hardware product solutions associated with the company's services.
 

Contact Us

Address: No. 2, Jingwu Road, Zhengzhou City, Henan Province

Phone: 0086-371-63520088 

QQ:76257322

Website: 800188.com

E-mail: This email address is being protected from spambots. You need JavaScript enabled to view it.