🖥️ Nginx Config Generator
Nginx Config Generator - Pick static site or reverse proxy and fill params to emit a server block config. Free, browser-only.
📐 工作原理與說明
按模式取值:域名為空時用下劃線佔位;域名逐項校驗只允許字母/數字/點/連字元/下劃線/萬用字元;靜態目錄與證書路徑校驗為以 / 開頭的可列印 ASCII 且不得含分號、大括號、$ 或控制字元,含空格的路徑輸出時加雙引號並按 Nginx 語法轉義引號與反斜槓;反向代理地址用 URL 解析校驗 http/https;埠必須是 1–65535 的整數。靜態模式生成 server 塊,依次寫入監聽埠、服務名、根目錄、預設首頁,以及「依次嘗試請求路徑、路徑作為目錄、否則返回 404」的 location;反向代理模式生成 server 塊,依次寫入監聽埠、服務名,以及把請求轉發到所填上游地址的 location,並設定轉發請求頭中的主機名與客戶端真實地址。輸出時對整段配置做 HTML 轉義後在程式碼塊中展示
按場景生成 Nginx server 配置。
- 場景:靜態站點、反向代理、HTTPS、負載均衡、單頁應用(SPA)回退。
- 核心項:listen 埠、server_name、root/index、location 匹配規則與優先順序。
- HTTPS:ssl_certificate、ssl_certificate_key,並限制協議版本與加密套件。
- 反代:需設定 proxy_set_header 傳遞真實客戶端 IP,否則後端日誌全是代理 IP。
Formula & Standard References
| Item | Formula / Note | Source / Standard |
|---|---|---|
| Config hierarchy | main, events, http, server and location contexts | nginx documentation |
| Reverse proxy | proxy_pass forwards upstream; proxy_set_header passes headers | nginx documentation |
| Location matching | exact = first, then ^~ prefix, then regex, then prefix | nginx documentation |
⚠️ 使用說明與注意事項
- 反向代理的 proxy_pass 末尾是否帶 / 影響路徑拼接:帶 / 會剝離 location 字首,按需選擇。
- 配置改完用 nginx -t 校驗語法,再 nginx -s reload 生效,避免寫錯導致服務中斷。
- 生產應補 TLS(listen 443 ssl)、gzip、快取頭等;本工具只生成最小可用骨架。
📋 常用指令
| 指令 | 作用 |
|---|---|
| try_files | 靜態回退 |
| proxy_pass | 轉發上游 |
| nginx -t | 校驗語法 |