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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章