Nginx user 配置引发的血案
权限问题
将 user 配置改成
user zhongwei;
报了一个错误
[emerg]: getgrnam("zhongwei") failed in /usr/local/nginx/conf/nginx.conf:1
参考 Nginx 的配置文档
Syntax: user user [group];
Default: user nobody nobody;
Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.
在 group 省略的情况下,group 默认等于 user 名。然后,ls -la 了一下 web 目录。
pelican-wiki $ ls -la
total 104
drwxr-xr-x 19 zhongwei staff 646 3 23 22:22 .
drwxr-xr-x 68 zhongwei staff 2312 3 21 21:28 ..
drwxr-xr-x 16 zhongwei staff 544 3 31 19:28 .git
group的值是 staff,于是改成
user zhongwei staff;
之后一切工作正常。
try_files 造成死循环
try_files 按顺序检查文件是否存在,返回第一个找到的文件,至少需要两个参数,但最后一个参数是内部重定向,和rewrite效果一致。
前面的值是相对$document_root的文件路径,也就是说参数的意义不同,甚至可以用一个状态码 (404)作为最后一个参数,如果不注意会有死循环造成500错误。
示例1:
location ~.*\.(gif|jpg|jpeg|png)$ {
root /web/wwwroot;
try_files /static/$uri $uri;
}
原意图是访问http://example.com/test.jpg 时先去检查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg 。
但由于最后一个参数是一个内部重定向,所以并不会检查/web/wwwroot/test.jpg是否存在,只要第一个路径不存在就会重新向然后再进入这个location造成死循环,结果会出现500 Internal Server Error。
实例2:
location ~.*\.(gif|jpg|jpeg|png)$ {
root /web/wwwroot;
try_files /static/$uri $uri 404;
}
这样才会先检查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg,最后不存在则返回404 not found。
可能你会觉得500和404好像都是错误码,差别不大,其实500不仅多次重定向浪费性能,还会影响到nginx的upstream造成错误的判断当前机器有故障而将新请求路由到其它机器甚至备用机,404则不会。
location /images/ { root /opt/html/; try_files $uri $uri/ /images/default.gif; }
比如请求 127.0.0.1/images/test.gif 会依次查找:
- 文件/opt/html/images/test.gif
- 文件夹 /opt/html/images/test.gif/下的index文件
- 请求127.0.0.1/images/default.gif
try_files如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页即访问127.0.0.1/images/,不会去访问 127.0.0.1/images/index.html。

浙公网安备 33010602011771号