Skip to content

LightChang/my-nginx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NPM Static Site Manager

自動透過 Nginx Proxy Manager API 管理靜態網站託管,讓 Nginx 直接處理靜態檔案,效能更佳。

架構

┌─────────────────────────────────────────┐
│     Nginx Proxy Manager (Docker)        │
│     - 反向代理 + 靜態檔案服務           │
│     - 自動 SSL 憑證                     │
└──────────────────┬──────────────────────┘
                   │
         NPM API (Port 81)
                   │
┌──────────────────▼──────────────────────┐
│     Node.js (NPM Static Site Manager)   │
│     - 監聽 www 目錄變化                 │
│     - 自動更新 NPM 配置                 │
└─────────────────────────────────────────┘

運作方式

  1. 程式監聽 ./www 目錄
  2. 當目錄有變化時,掃描所有子目錄
  3. 比對 NPM 中的 Proxy Hosts
  4. 自動更新 advanced_config,讓 Nginx 直接服務靜態檔案

快速開始

1. 啟動 Nginx Proxy Manager

docker-compose up -d

2. 初始化 NPM

  1. 開啟 http://localhost:81
  2. 使用預設帳號登入:
    • Email: admin@example.com
    • Password: changeme
  3. 修改密碼

3. 建立站點目錄

mkdir -p www/example.com
echo "<h1>Hello World</h1>" > www/example.com/index.html

4. 在 NPM 新增 Proxy Host

  • Domain Names: example.com
  • Forward Host: localhost
  • Forward Port: 80

5. 啟動管理程式

# 直接執行
node index.js

# 或使用 PM2
pm2 start ecosystem.config.js

環境變數

變數 說明 預設值
NPM_API_URL NPM API 位址 http://localhost:81
NPM_USER NPM 登入帳號 admin@example.com
NPM_PASSWORD NPM 登入密碼 changeme
WWW_DIR 靜態檔案目錄 ./www

PM2 配置

ecosystem.config.js

module.exports = {
  apps: [
    {
      name: "npm-static-manager",
      script: "index.js",
      env: {
        NPM_API_URL: "http://localhost:81",
        NPM_USER: "admin@example.com",
        NPM_PASSWORD: "your-password",
        WWW_DIR: "./www",
      },
    },
  ],
};

目錄結構

my-nginx/
├── index.js              # 主程式
├── package.json          # 專案配置
├── docker-compose.yml    # Docker 編排
├── ecosystem.config.js   # PM2 配置 (需自行建立)
├── data/                 # NPM 資料 (自動產生)
├── letsencrypt/          # SSL 憑證 (自動產生)
└── www/                  # 靜態網站目錄
    ├── site1.example.com/
    │   └── index.html
    └── site2.example.com/
        └── index.html

注意事項

  • 需要 Node.js 18+ (使用內建 fetch)
  • www 子目錄名稱必須與 NPM Proxy Host 的 domain name 相同
  • 程式會自動等待 NPM 啟動後再連接

與舊版差異

項目 舊版 (v1) 新版 (v2)
靜態檔案處理 Express Nginx (原生)
效能
依賴套件 express 無 (純 Node.js)
配置方式 讀取 Nginx 配置檔 透過 NPM API

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors