Skip to content

Commit b5cb11d

Browse files
author
yichi
committed
update
1 parent 4ccdf57 commit b5cb11d

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed

_posts/2019-02-18-nginx-index-page.md

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
layout: post
3+
title: "一个常见需求的nginx配置踩雷"
4+
description: "nginx配置首页、静态资源代理、web服务代理"
5+
category: tech
6+
tags: ['全栈']
7+
---
8+
{% include JB/setup %}
9+
10+
最近想给做的node应用服务提供nginx静态资源代理层。之前没做是不要约束大家的路径,后来想想不想被约束的开发可以不用,但是作为基础服务这个能力还是要有的,还有想用的人呢。况且,nginx代理静态资源可比node性能高多了,特别是大文件。
11+
12+
本以为很快就能配完测好上线了,好歹我也还是比较熟nginx的,结果一动手,发现踩了一片雷。。。
13+
14+
需求非常常见:
15+
1. 静态资源走nginx代理
16+
2. web接口代理给node应用
17+
3. 首页可以免路径,直接通过域名xxx.com访问
18+
19+
静态资源可以约束下路径到`/static`文件夹下。
20+
21+
## location 优先级
22+
23+
最折腾人的就是第3项。脑子一拍最先想到配置就是用location的`= /`
24+
25+
```
26+
server {
27+
listen 80;
28+
root /Users/echizen/demo/nginx-learn/site1;
29+
30+
location = / {
31+
index /static/index.html;
32+
}
33+
34+
location / {
35+
proxy_pass http://127.0.0.1:8020;
36+
}
37+
}
38+
```
39+
40+
但是测试时一直走到proxy_pass的第二个location block了。翻下[location配置文档](https://nginx.org/en/docs/http/ngx_http_core_module.html#location)
41+
42+
> using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.
43+
44+
还给了个案例,看着优先级规则没理解错啊:
45+
46+
> location = / {
47+
[ configuration A ]
48+
}
49+
50+
location / {
51+
[ configuration B ]
52+
}
53+
54+
location /documents/ {
55+
[ configuration C ]
56+
}
57+
58+
location ^~ /images/ {
59+
[ configuration D ]
60+
}
61+
62+
location ~* \.(gif|jpg|jpeg)$ {
63+
[ configuration E ]
64+
}
65+
The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.
66+
67+
68+
## index
69+
70+
毕竟代码量少,挨个怀疑就怀疑到`index`了,一翻[index配置文档](https://nginx.org/en/docs/http/ngx_http_index_module.html#index),果然踩雷,连例子都甩出来了:
71+
72+
> It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location. For example, with the following configuration:
73+
74+
location = / {
75+
index index.html;
76+
}
77+
78+
location / {
79+
...
80+
}
81+
a “/” request will actually be processed in the second location as “/index.html”.
82+
83+
index内部是通过重定向实现的啊,就给重定向到`location /`
84+
85+
86+
## try_files
87+
88+
那就用try_files吧,不过一开始是拒绝这个玩意的,因为我明明知道准确路径,为啥要try_files来消耗资源,而且这货还必须至少2条file规则。后来仔细反思,其实挺好,这样多配几个规则可见兼容更多的index位置场景,把最常见的放第一个就好。
89+
90+
[try_files文档](https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files)
91+
92+
值的注意的是:
93+
94+
1. try_files最好在最后一条配上404,否则资源找不到nginx会陷入循环寻找导致500
95+
2. 这货默认内部有最后一条规则走$uri,也就是路由的路径去寻找,所以看到没有命中自己index规则而是走了真正的路由路径也不要奇怪。
96+
> If none of the files were found, an internal redirect to the uri specified in the last parameter is made
97+
98+
## 合度代理
99+
100+
之前孤陋寡闻,为了能将所有的路径都转发给node服务,规则配置的是:
101+
102+
```
103+
location / {
104+
proxy_pass http://127.0.0.1:8020;
105+
}
106+
```
107+
108+
但是其实nginx对静态资源的代理性能更好,为啥不优先走nginx呢。所以可以优化成:
109+
110+
```
111+
location / {
112+
try_files $uri @proxy;
113+
}
114+
115+
location @proxy {
116+
proxy_pass http://127.0.0.1:8020;
117+
}
118+
```
119+
120+
## 安全性
121+
122+
目前为止,看上去需求都完成了:
123+
124+
```
125+
server {
126+
listen 80;
127+
root /Users/echizen/demo/nginx-learn/site1;
128+
129+
location = / {
130+
try_files index.html /static/index.html;
131+
}
132+
133+
location / {
134+
try_files $uri @proxy;
135+
}
136+
137+
location @proxy {
138+
proxy_pass http://127.0.0.1:8020;
139+
}
140+
}
141+
```
142+
143+
但是内心惴惴不安,这么合适么,`try_files $uri`让所有请求都先按请求路径查询了一遍,也就是说服务器根目录下的任何文件都被搞成静态资源代理了,服务器所有代码文件现在都可以在浏览器上直接访问查看了。。。虽说我是给内网应用配模板的,安全风险可控,但是总觉得是掉节操。
144+
145+
所以我不再所有代理所有路径了,只让static成为可访问路径:
146+
147+
```
148+
server {
149+
listen 80;
150+
root /Users/echizen/demo/nginx-learn/site1;
151+
152+
location = / {
153+
try_files /static/index.html index.html;
154+
}
155+
156+
location /static/ {
157+
}
158+
159+
location / {
160+
proxy_pass http://127.0.0.1:8020;
161+
}
162+
}
163+
```
164+
165+
## 去除路径中的固定字段
166+
167+
node代理都是用`koa-static`习惯了,`/static/demo.js`只需要`http://xxx.com/demo.js`就能访问,无需非得在路由里加上`static`,虽说也可以用封装一下自定义版的`koa-static`,也借用`send`包但是把`static`路径补上,但是这样就改动了已有应用的路由要求,而且nginx明显有能力将`static`层去掉。
168+
169+
`try_files /static/$uri`搞起来就好。
170+
171+
```
172+
server {
173+
listen 80;
174+
root /Users/echizen/demo/nginx-learn/site1;
175+
176+
location = / {
177+
try_files /static/index.html index.html;
178+
}
179+
180+
location / {
181+
try_files /static/$uri @proxy;
182+
}
183+
184+
location @proxy {
185+
proxy_pass http://127.0.0.1:8020;
186+
}
187+
}
188+
```
189+
190+
不过这也不是最优方案,因为所有请求都会先去匹配`/static/$uri`, 性能再高也是要去判断文件是否存在的,是个消耗。只是对于我的场景,内网应用,保持一致的习惯接入用户最小的改动比一点点的性能损耗来说是合适的。
191+
192+
## 残留的疑惑
193+
194+
在此途中也遇到了2个查了文档搜了google也无法解释的现象:
195+
196+
1. 如果把`try_files`的file顺序调一下,`index.html`放前面(该文件不存在),`http:xxx.com`就不能正常代理到`static/index.html`了,而是走了`proxy_pass`。即:
197+
198+
```
199+
server {
200+
listen 80;
201+
root /Users/echizen/demo/nginx-learn/site1;
202+
203+
location = / {
204+
try_files index.html /static/index.html;
205+
}
206+
207+
location / {
208+
proxy_pass http://127.0.0.1:8020;
209+
}
210+
}
211+
```
212+
难道是`try_files`是个获取文件的尝试,会按优先级命中所有路由?即1.获取/Users/echizen/demo/nginx-learn/site1/index.html静态文件,不存在。 2.尝试http://127.0.0.1:8020 代理,如果404的话继续下去 3.获取/Users/echizen/demo/nginx-learn/site1/static/index.html...
213+
214+
2.上面的写法不ok,但是加上`location /static/`的规则就正常工作了,这个强行我也解释不了了。。。即:
215+
216+
```
217+
server {
218+
listen 80;
219+
root /Users/echizen/demo/nginx-learn/site1;
220+
221+
location = / {
222+
try_files index.html /static/index.html;
223+
}
224+
225+
location /static/ {
226+
}
227+
228+
location / {
229+
proxy_pass http://127.0.0.1:8020;
230+
}
231+
}
232+
```
233+
234+
难道是`try_files`只是管理路由优先级,并不做可访问性代理?所以一定要后面接的path路径本身在其他规则下可访问?。但是try_files的文档介绍它的功能是`Checks the existence of files in the specified order and uses the first found file for request processing`,明显是查找文件是否存在,存在就响应啊。
235+

0 commit comments

Comments
 (0)