{"id":19317651,"url":"https://github.com/queeniecplusplus/nginx_www","last_synced_at":"2026-02-25T20:05:07.840Z","repository":{"id":104588382,"uuid":"296969447","full_name":"QueenieCplusplus/Nginx_WWW","owner":"QueenieCplusplus","description":"web server 網站伺服器","archived":false,"fork":false,"pushed_at":"2020-09-25T00:54:00.000Z","size":1211,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-07T02:03:46.266Z","etag":null,"topics":["web","webapp","website","websocket"],"latest_commit_sha":null,"homepage":"https://github.com/QueenieCplusplus/QuickGoThru/blob/master/README.md#nginx--k8s-in-2020","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/QueenieCplusplus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-19T23:40:36.000Z","updated_at":"2020-09-25T00:54:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"be099377-0f78-4035-9a2a-a195d63ed8d6","html_url":"https://github.com/QueenieCplusplus/Nginx_WWW","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/QueenieCplusplus/Nginx_WWW","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FNginx_WWW","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FNginx_WWW/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FNginx_WWW/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FNginx_WWW/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QueenieCplusplus","download_url":"https://codeload.github.com/QueenieCplusplus/Nginx_WWW/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QueenieCplusplus%2FNginx_WWW/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29837512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T19:08:47.527Z","status":"ssl_error","status_checked_at":"2026-02-25T18:59:04.705Z","response_time":61,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["web","webapp","website","websocket"],"created_at":"2024-11-10T01:15:46.586Z","updated_at":"2026-02-25T20:05:07.798Z","avatar_url":"https://github.com/QueenieCplusplus.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nginx_WWW\nweb server 網站伺服器\n\nWWW 技術解決了遠端資訊中的資料連接、資訊顯示、影像傳遞的需求，成為網路上最常見的傳播方式，web server 為提供此網路資訊瀏覽服務的終端設備。\n\n\n![nginx architecture](https://raw.githubusercontent.com/QueenieCplusplus/Nginx_WWW/master/nginx_architecture.png)\n\n# Nginx Web Server Architecture 說明：\n\n伺服器啟動後，產生一個主處理程序 Master Process =\u003e 設定檔解析、資料結構初始化、模組註冊、網路監聽\n\n主處理程序執行一系列工作後，產生多個工作處理程序 Worker Process =\u003e 處理程序初始化、模組呼叫、請求處理\n\n請求處理中（用戶請求動態網站的一過程）=\u003e『Nginx 伺服器』與『後端伺服器』之間通訊，前者將請求透過 Cache 傳到後端伺服器，後端伺服器處理請求，即進行資料處理和頁面組織，然後將結果回傳。\n\n\n# Web Server 提供資訊瀏覽/查詢功能外，尚發展四種功能：\n\n* 運算效能\n\n* 記憶體容量\n\n* 穩定性\n\n  (1)每分每秒都有大量請求需求，一台伺服器能否提供不間斷的服務，是種考驗。\n  \n  (2)如何保障網頁伺服器能在大量平行處理請求出現時候，仍然穩定執行。\n  \n  (3)長時間運轉中，降低出錯率。\n  \n  (4)產生問題之前能預先防止。\n  \n  (5)擁有備份，即便產生問題，也能避免資料遺失。\n\n* 簡易性\n\n  實施步驟簡單，部署快速。\n\n# 請求處理\n\n（網頁回應）\n\n  請求處理中（用戶請求動態網站的一過程）=\u003e『Nginx 伺服器』與『後端伺服器』之間通訊，前者將請求透過 Cache 傳到後端伺服器，後端伺  服器處理請求，即進行資料處理和頁面組織，然後將結果回傳。\n  \n  與 Single Process 不同，代理伺服器採用 Master-Slave 模型。\n  \n  \n  * multi-process(worker-process), 多處理程序\n  \n     一旦伺服器接收到使用者端（客戶端）發起之請求，便由  Master Process 運用 fork() 產生子處理程序和用戶端建立連線，\n     直到連線中斷時，則子處理程序生命週期結束。\n     \n     子處理程序的優勢是與其他子處理程序分開進行任務，彼此獨立，倘若過程中受到干擾，產生問題時，不會影響其他子處理程序，\n     不會將問題蔓延到其他子處理程序中，確保服務的穩定性。另外在生命週期結束時，也會自動的資源回收，不留下垃圾。\n     \n     然而一開始主處理程序產生子處理程序時，就會對系統造成負擔！\n     \n     \n  * 預產生處理程序\n     \n     改進如上的多處理程序，提前了產生子處理程序的時機，在用戶端抑或使用者端的請求尚未來到之前就預先產生好，直到請求到來時，\n     主處理程序可以直接分配子處理程序和用戶端連線進行互動，互動完成後，該處理程序不會結束，由主處理程序管理後再做工作任務的分配。\n  \n  * multi-thread (worker-thread), 多執行緒\n  \n     多執行緒和多處理程序方式相似，是指 server 每接受到一用戶端 client 時，會由伺服器的 Master Process 衍生出\n     thread 出來和 client 互動。\n     \n     ![multithreads](https://raw.githubusercontent.com/QueenieCplusplus/Nginx_WWW/master/multi-threads.png)\n\n     由於作業系統產生 thread 的負擔遠遠小於產生一個 process 的負擔，所以能減輕伺服器對系統資源的要求。然而也有缺點，\n     因為多執行緒位於同一處理程序內，但是彼此間會競奪記憶體空間，相互產生影響，倘若開發者在釋放資源和回收資源上沒有做好\n     相應的資源管理，則產生出錯的風險。\n\n     運行時間一長，出錯的累積數量會導致伺服器的重大影響。（微軟的 IIS 伺服器在多執行緒方面還算穩定。）\n  \n  * async, 非同步\n  \n    * 非同步阻塞\n     \n      實務上很少使用，因為 server 端的 IO 如果不能馬上獲得結果，會等待傳回結果，期間不做任何工作。（反之，client 接收方發送請求後，不必等待回應，可進行其它工作）。\n      \n    \n    * 非同步非阻塞\n    \n      client 接收方發送請求予伺服器接受請求後的傳回結果，雙方彼此都不等待結果，可以繼續進行其它工作任務，IO 操作完成後，伺服器等待完成狀態和結果通知 client 接收方，client 接收方再回應發送方 server。（甚至比同步阻塞還有效率）\n      \n      ![async block](https://raw.githubusercontent.com/QueenieCplusplus/Nginx_WWW/master/async%20block.png)\n      \n      圖片引用出處：https://luminousmen.com/post/asynchronous-programming-blocking-and-non-blocking\n      \n     * 同步阻塞\n     \n       client 端發送請求後，等待回應，不做任何其它工作 ; server 端處理請求的 IO 如果無法馬上獲得結果，會等待，直到傳回結果，\n       期間不能執行任何其它動作。（失去效率）\n       \n     * 同步非阻塞\n     \n       只要伺服器沒有獲得處理結果，伺服器端會一直等待，確定發送完結果予 client 後，才進行下一次處理請求過程。（不使用）\n     \n\n# 處理程序\n\n（處理程序間互動）\n\n  伺服器啟動後，產生一個主處理程序 Master Process =\u003e 設定檔解析、資料結構初始化、模組註冊、網路監聽\n\n  主處理程序執行一系列工作後，產生多個工作處理程序 Worker Process =\u003e 處理程序初始化、模組呼叫、請求處理\n  \n  * 外界 - Master\n  \n    主處理程序與外界透過訊號（電磁波）溝通。\n  \n    而處理程序間的互動，則依賴管線 Channel | Pipeline:\n  \n  * Master 主程序 - Workers 工作程序 (| Master-Slave)\n  \n    主處理程序藉由 fork 函數，產生了工作處理程序。 Nginx 伺服器啟動後，Master Process 根據設定檔決定產生的 Worker Process 數量，然後建立一張 Gloabal 工作處理程序表，用於儲存目前尚未退出的所有 Worker Processes。\n    \n    入表紀錄的工作處理程序與主處理程序間行程一個單向管線 Pipeline，由主處理程序指向工作處理程序：\n    內容包含了 指令、工作處理程序的id、表終索引、必要檔案描述符號。\n    \n    工作處理程序則捕捉管線中的事件，讀取之，並且解析其中指令，採取對應措施。\n  \n  * Worker - Worker 工作程序間\n  \n    同上，工作處理程序間也是透過管線互動，但是由於工作管理程序彼此間是隔開的，因此彼此溝通，尚需要透過主管理程序。\n    主處理程序透過表來區別工作處理程序的id，並且傳遞該工作處理程序的管線控制碼予其他需要與之溝通的工作處理程序。\n    \n  * Thread 執行緒\n  \n    ![single thread](https://raw.githubusercontent.com/QueenieCplusplus/Nginx_WWW/master/single-thread.png)\n  \n    When a process starts, it is assigned memory and resources. Each thread in the process shares that memory and resources. In single-threaded processes, the process contains one thread. The process and the thread are one and the same, and there is only one thing happening.\n    \n  * Event Listening Loop 事件聆聽的循環模式\n  \n    處理程序和執行緒如要持續不間斷的工作，都是透過事件聆聽模型，此模型可能是自動的，也可能是自定義的。\n    \n    設計 Run-Loop 事件時，要對事件輸入事件和對應事件作出的回應。\n    \n    設計事件聆聽迴圈的元件包含：\n    \n    * Timer, 計時器\n    \n    * Event, 輸入的事件來源\n    \n    * Observer, 監聽器（被通知者）\n    \n      可以設計給監聽器聆聽特定事件。\n  \n\n     流程如下：\n     \n     \n         Event Occurs \n         \n                    =\u003e\n\n                      Msg Log from Run-Loop \n                     \n                                          =\u003e \n\n                                            Observer rcv the Msg Log \n                                          \n                                                                 =\u003e \n\n                                                                    Execute ...(Async)\n\n\n# 快取機制\n\n（分散後端伺服器的壓力）\n\n 為加強 Nginx 對請求的回應效率，並且降低對網路的壓力，故採取快取機制，將歷史回應資料快取至本機，每次 Nginx 啟動服務後一段時間，會啟動本機快取對內容重建索引，保障快取機制提取資料的速度（快速存取）。\n \n \n * 實際上運作：\n \n   主要由快取索引重建 Cache Loader 和快取索引管理 Cache Manager 兩種處理程序完成工作，此兩程序程序維護的記憶體索引中繼資料庫，為工作處理程序對快取資料的快速查詢提供便利性。\n\n   Cache Loader 由主處理程序產生（預設為一分鐘），在 Cache Metadata 產出後自動退出。\n\n   Cache Manager 存在於主處理程序的整個生命週期中，負責對快取索引進行管理。\n\n   快取索引重建處理程序完成的主要工作是：根據對本機磁碟上的快取檔案在記憶體中建立索引中繼資料庫。\n   \n   ![cache loader](https://raw.githubusercontent.com/QueenieCplusplus/Nginx_WWW/master/cache.png)\n\n   該處理程序啟動後，對本機磁碟上儲存快取檔案的目錄結構進行掃描，檢查記憶體中已有的快取中繼資料是否正確，\n   並且更新索引中繼資料庫。\n   \n   ![cache manager](https://raw.githubusercontent.com/QueenieCplusplus/Nginx_WWW/master/cache%20table.png)\n\n   快取索引管理程序則是負責對中繼資料是否過期進行判斷。\n\n# 複寫功能\n\nhttps://github.com/QueenieCplusplus/Nginx_Backend\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueeniecplusplus%2Fnginx_www","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqueeniecplusplus%2Fnginx_www","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueeniecplusplus%2Fnginx_www/lists"}