Nginx user 配置引发的血案

文章目录

    在折腾完了 gulp livereload web server 之后,我才发现其实 Nginx 才是最靠谱的 web 调试服务。
    毕竟写伪接口用 Nginx 要方便的多,也不用考虑 node 模块的一堆奇葩依赖。

    但是 Nginx 也让我吐了一口血。白天在公司 Ubuntu 开发机上一切正常,但是回家后发现 Mac 下的 Nginx 一直报错。

    stat() “/Users/zhongwei/test/index.html” failed (13: Permission denied),
    rewrite or internal redirection cycle while internal redirect to “/index.html”

    这里有两个问题

    • 权限问题
    • 为何造成了死循环

    问题一个一个解决

    权限问题

    将 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;
    

    之后一切工作正常。

    不要忘了死循环问题

    The problem here is that the second parameter here, $uri/, causes each of the files in your index directive to be tried in turn. If none are found, it then moves on to /index.html, which causes the same location block to be re-entered, and since it still doesn’t exist, you get an endless loop.

    try_files $uri $uri/ /index.html;
    替换为
    try_files $uri $uri/ =404;
    即可
    

    参考

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式