Nginx-技術專題-技術介紹

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Nginx 介紹","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 是一個高性能的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"HTTP","attrs":{}}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 和反向代理 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"web","attrs":{}}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 服務器,同時也提供了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"IMAP/POP3/SMTP","attrs":{}}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 服務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 是由伊戈爾·賽索耶夫爲俄羅斯訪問量第二的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Rambler.ru","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 站點開發的,第一個公開版本 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"0.1.0","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 發佈於 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"2004","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 年 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"10","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 月 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"4","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 日。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"Nginx","attrs":{}}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":" 特點是佔有內存少,併發能力強。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 事實上 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 的併發能力確實在同類型的網頁服務器中表現較好,一般來說,如果我們在項目中引入了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" ,我們的項目架構可能是這樣:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/81/819abfa66c3293a1421b20667056be3e.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 這樣的架構中 , ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 所代表的角色叫做負載均衡服務器或者反向代理服務器,所有請求首先到達 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 上,再由 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 根據提前配置好的轉發規則,將客戶端發來的請求轉發到某個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Tomcat","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 上去。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼這裏涉及到兩個概念:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"負載均衡服務器","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 就是進行請求轉發,降低某一個服務器的壓力。負載均衡策略很多,也有很多層,對於一些大型網站基本上從 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DNS","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 就開始負載均衡,負載均衡有硬件和軟件之分,各自代表分別是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"F5","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" (目前 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 已經被 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"F5","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 收購),早些年,也可以使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Apache","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 來做負載均衡,但是效率不如 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" ,所以 現在主流方案是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"反向代理服務器","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 另一個概念是反向代理服務器,得先說正向代理,看下面一張圖:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6a/6ab28e8e5d2101880964fa49c9e33a61.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 在這個過程中,Google 並不知道真正訪問它的客戶端是誰,它只知道這個中間服務器在訪問它。因此,這裏的代理,實際上是中間服務器代理了客戶端,這種代理叫做正向代理。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 那麼什麼是反向代理呢?看下面一張圖:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a6/a6b596c01021c166723f175154ef4cee.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 在這個過程中,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"10086 ","attrs":{}},{"type":"text","text":"這個號碼相當於是一個代理,真正提供服務的,是話務員,但是對於客戶來說,他不關心到底是哪一個話務員提供的服務,只需要記得 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"10086 ","attrs":{}},{"type":"text","text":"這個號碼就行了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 所有的請求打到 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"10086 ","attrs":{}},{"type":"text","text":"上,再由 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"10086 ","attrs":{}},{"type":"text","text":"將請求轉發給某一個話務員去處理。因此,在這裏,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"10086 ","attrs":{}},{"type":"text","text":"就相當於是一個代理,只不過它代理的是話務員而不是客戶端,這種代理稱之爲","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"反向代理","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Nginx 優勢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Nginx 有着非常廣泛的使用,隨便舉幾點:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"Nginx 做","attrs":{}},{"type":"link","attrs":{"href":"https://yq.aliyun.com/go/articleRenderRedirect?url=http%3A%2F%2Ft.cn%2FAigDWnXB","title":null},"content":[{"type":"text","text":"靜態資源服務器","attrs":{}}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":":Java 中的資源可以分爲動態和靜態,動態需要經過 Tomcat 解析之後,才能返回給瀏覽器,例如 JSP 頁面、Freemarker 頁面、控制器返回的 JSON 數據等,都算作動態資源,動態資源經過了 Tomcat 處理,速度必然降低。","attrs":{}},{"type":"text","text":"對於靜態資源,例如圖片、HTML、JS、CSS 等資源,這種資源可以不必經過 Tomcat 解析,當客戶端請求這些資源時,之間將資源返回給客戶端就行了。此時,可以使用 Nginx 搭建靜態資源服務器,將靜態資源直接返回給客戶端。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Nginx 做負載均衡服務器","attrs":{}},{"type":"text","text":",無論是使用 Dubbo 還是 Spirng Cloud ,除了使用各自自帶的負載均衡策略之外,也都可以使用 Nginx 做負載均衡服務器。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"支持高併發、內存消耗少、成本低廉、配置簡單、運行穩定等。","attrs":{}}]}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Nginx 安裝","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於基本上都是在 Linux 上使用 Nginx,因此松哥這裏主要向大家展示 CentOS 7 安裝 Nginx:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"首先下載 Nginx","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然後解壓下載的目錄,進入解壓目錄中,在編譯安裝之前,需要安裝兩個依賴:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"然後開始編譯安裝:","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"裝好之後,默認安裝位置在 :","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"進入到該目錄的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sbin","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 目錄下,執行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 即可啓動 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" :","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/78/784214f3346c9294cd7872612a41f91e.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Nginx 啓動成功之後,在瀏覽器中直接訪問 Nginx 地址:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/85/85b4cbb0a14a94af6bbfbf2d34b851b3.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看到如上頁面,表示 Nginx 已經安裝成功了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果修改了 Nginx 配置,則可以通過如下命令重新加載 Nginx 配置文件:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Nginx搭建靜態資源web服務器","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Nginx默認會訪問index.html頁面.","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 例如,如下圖配置, 當訪問","attrs":{}},{"type":"link","attrs":{"href":"https://yq.aliyun.com/go/articleRenderRedirect?url=http%3A%2F%2F172.16.204.5%3A90%2F","title":null},"content":[{"type":"text","text":"http://172.16.204.5:90/","attrs":{}}]},{"type":"text","text":"時, 默認會訪問安裝目錄下的mine_html目錄中的index.html文件.","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8e/8ed02d9de581b516261a37c33df3fcec.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"設置Nginx的gzip壓縮:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/71/71e2ef376c4d0e3cdb3623fdc2dac298.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Nginx支持訪問文件樹","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"效果如下(如果訪問的url路徑下有index.html, 默認還是會跳到index.html頁面):","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c17ebce9f82f3a60f7814766a941932c.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要達到上圖中的效果,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Nginx","attrs":{}},{"type":"text","text":"的配置文件需要添加如下設置:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ce/cee640c17d239e498a2605215a33b240.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 添加上圖中的配置後, 客戶端訪問","attrs":{}},{"type":"link","attrs":{"href":"https://yq.aliyun.com/go/articleRenderRedirect?url=http%3A%2F%2F172.16.204.5%3A90%2F","title":null},"content":[{"type":"text","text":"http://172.16.204.5:90/","attrs":{}}]},{"type":"text","text":"時, 會以樹的格式展示mine_html中的所有文件. 使用這種方式可以很好的共享靜態資源.","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"在生產環境中","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 公網的帶寬是有限的, 併發情況下各個客戶端會爭搶帶寬資源, 爲了防止訪問大的靜態資源對其他用戶造成影響, 可以設置nginx響應的帶寬. 例如如下圖設置, nginx給客戶端響應時會以每秒1k的速度響應.","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/aa/aaf05edcb3ab082426c0ec221cc90255.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Nginx緩存服務器上的靜態文件","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Nginx緩存的優點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ca/ca4f7c9d2e8de54e82b20a03567511f5.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 如圖所示,nginx緩存,可以在一定程度上,減少源服務器的處理請求壓力。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 因爲靜態文件(比如css,js, 圖片)中,很多都是不經常更新的。nginx使用proxy_cache將用戶的請求緩存到本地一個目錄。下一個相同請求可以直接調取緩存文件,就不用去請求服務器了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 畢竟,IO密集型服務的處理是nginx的強項。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何進行設置","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先上個栗子:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"http{\n proxy_connect_timeout 10;\n proxy_read_timeout 180;\n proxy_send_timeout 5;\n proxy_buffer_size 16k;\n proxy_buffers 4 32k;\n proxy_busy_buffers_size 96k;\n proxy_temp_file_write_size 96k;\n proxy_temp_path /tmp/temp_dir;\n proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;\n server {\n listen 80 default_server;\n server_name localhost;\n root /mnt/blog/;\n location / {\n }\n #要緩存文件的後綴,可以在以下設置。\n location ~ .*\\.(gif|jpg|png|css|js)(.*) {\n proxy_pass http://ip地址:90;\n proxy_redirect off;\n proxy_set_header Host $host;\n proxy_cache cache_one;\n proxy_cache_valid 200 302 24h;\n proxy_cache_valid 301 30d;\n proxy_cache_valid any 5m;\n expires 90d;\n add_header wall \"hey!guys!give me a star.\";\n }\n }\n # 無nginx緩存的blog端口\n server {\n listen 90;\n server_name localhost;\n root /mnt/blog/;\n location / {\n }\n }\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲我是在一臺服務器上做試驗,所以用了兩個端口","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"80","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"90","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"進行模擬兩臺服務器之間的交互。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"80","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"端口對接的是普通的域名(","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"http://wangxiaokai.vip","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":")訪問。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"90","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"端口負責處理","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"80","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"端口代理過來的資源訪問。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相當於","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"90","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"端口是源服務器,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"80","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"端口是nginx反向緩存代理服務器。","attrs":{}}]},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來講一下配置項:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"http層設置","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" proxy_connect_timeout 10;\n proxy_read_timeout 180;\n proxy_send_timeout 5;\n proxy_buffer_size 16k;\n proxy_buffers 4 32k;\n proxy_busy_buffers_size 96k;\n proxy_temp_file_write_size 96k;\n proxy_temp_path /tmp/temp_dir;\n proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_connect_timeout ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"服務器連接的超時時間","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_read_timeout ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"連接成功後,等候後端服務器響應時間","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_send_timeout ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"後端服務器數據回傳時間","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_buffer_size ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"緩衝區的大小","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_buffers ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"每個連接設置緩衝區的數量爲number,每塊緩衝區的大小爲size","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_busy_buffers_size ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"開啓緩衝響應的功能以後,在沒有讀到全部響應的情況下,寫緩衝到達一定大小時,nginx一定會向客戶端發送響應,直到緩衝小於此值。","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_temp_file_write_size ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"設置nginx每次寫數據到臨時文件的size(大小)限制","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_temp_path ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"從後端服務器接收的臨時文件的存放路徑","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_cache_path ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"設置緩存的路徑和其他參數。被緩存的數據如果在inactive參數(當前爲1天)指定的時間內未被訪問,就會被從緩存中移除","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"server層設置","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"反向緩存代理服務器","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" server {\n listen 80 default_server;\n server_name localhost;\n root /mnt/blog/;\n location / {\n }\n #要緩存文件的後綴,可以在以下設置。\n location ~ .*\\.(gif|jpg|png|css|js)(.*) {\n proxy_pass http://ip地址:90;\n proxy_redirect off;\n proxy_set_header Host $host;\n proxy_cache cache_one;\n proxy_cache_valid 200 302 24h;\n proxy_cache_valid 301 30d;\n proxy_cache_valid any 5m;\n expires 90d;\n add_header wall \"hey!guys!give me a star.\";\n }\n }","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_pass ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nginx緩存裏拿不到資源,向該地址轉發請求,拿到新的資源,並進行緩存","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_redirect ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"設置後端服務器“Location”響應頭和“Refresh”響應頭的替換文本","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_set_header ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"允許重新定義或者添加發往後端服務器的請求頭","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_cache ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"指定用於頁面緩存的共享內存,對應http層設置的keys_zone","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"proxy_cache_valid ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"爲不同的響應狀態碼設置不同的緩存時間","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"expires ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"緩存時間","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我設置了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"圖片","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"css","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"js","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"靜態資源進行緩存。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當用戶輸入","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"http://wangxiaokai.vip","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"域名時,解析得到","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ip:port","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"的訪問地址。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"port","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"默認爲80。所以頁面請求會被當前server截取到,進行請求處理。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當解析到上述文件名結尾的靜態資源,會到緩存區獲取靜態資源。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果獲取到對應資源,則直接返回數據。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果獲取不到,則將請求轉發給","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"proxy_pass","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"指向的地址進行處理。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"源服務器","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" server {\n listen 90;\n server_name localhost;\n root /mnt/blog/;\n location / {\n }\n }","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏直接處理","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"90","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"端口接受到的請求,到服務器本地目錄","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"/mnt/blog","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"下抓取資源進行響應。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"如何驗證緩存是否有效","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 細心的讀者應該發現,我在第二段栗子裏,留了個彩蛋 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"add_header wall \"hey!guys!give me a star.\"","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"add_header","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是用於在報頭設置自定義的信息。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以,如果緩存有效的話,那麼靜態資源返回的報頭,一定會帶上這個信息。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訪問","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"http://wangxiaokai.vip","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":"結果如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/39/391b6a486b823688dc4c51ed653e8f2a.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章