V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
maxAir
V2EX  ›  NGINX

想在 mac 上配置 nginx 启一个本地服务,访问 localhost ,一直 403 forbidden,心态已崩,求教大神

  •  
  •   maxAir · 2019-07-08 16:16:03 +08:00 · 8243 次点击
    这是一个创建于 1725 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是 nginx.conf 文件配置: user root owner;

    user root xl;

    user nobody;

    worker_processes 1;

    #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;

    #pid logs/nginx.pid;

    events { worker_connections 1024; }

    http { include mime.types; default_type application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    #access_log  logs/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #gzip  on;
    
    upstream node_server{
      server 127.0.0.1:3000;
      keepalive 2000;
    }
    server {
        listen       80;
        server_name  localhost;
    
        location / {
                       root   /Users/xl/Desktop/work/;
                       index  index.html index.htm;
                    #    add_header  Access-Control-Allow-Origin *;
                    #    add_header  Cache-Control  max-age=3600;
                    #    concat on;
                    #    concat_max_files 100;
                    #    concat_types "application/javascript" "text/css" "text/html";
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
    
    
    include servers/*;
    

    }

    已经检查本地项目目录下所以文件和文件夹都有 rwx 权限; 项目路径在: /Users/xl/Desktop/work;

    54 条回复    2019-10-10 12:02:46 +08:00
    maxAir
        1
    maxAir  
    OP
       2019-07-08 16:17:23 +08:00   ❤️ 1
    window 下没有问题,可以顺利访问,第一次用 mac,死活通不了
    aaa5838769
        2
    aaa5838769  
       2019-07-08 16:23:08 +08:00
    这个跟目录权限有关系的,你可以把项目路径放在其他目录上,应该就可以了。
    wuchujie
        3
    wuchujie  
       2019-07-08 16:29:28 +08:00
    为什么不加个日志呢。看下什么报错呗
    access_log /Users/xl/xxxxx.access.log
    error_log /Users/xl/xxxx.error.log
    taolu
        4
    taolu  
       2019-07-08 16:39:41 +08:00 via Android
    root 目录下如果不存在 index.html index.htm 文件,也会报 403,最好还是看日志
    maxAir
        5
    maxAir  
    OP
       2019-07-08 16:41:05 +08:00
    @aaa5838769 意思是不能放在 Desktop 里面吗, 我放在 Users/xl/work 这样也是一样的效果, 访问 localhost 就直接 403 了
    maxAir
        6
    maxAir  
    OP
       2019-07-08 17:04:30 +08:00   ❤️ 1
    @wuchujie , var 目录下有日志:2019/07/08 16:40:43 [error] 5896#0: *1 directory index of "/Users/xl/work/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost", 但是我查看 xl/work 目录是有 rwx 权限的, 不知道为什么了
    ipengxh
        7
    ipengxh  
       2019-07-08 17:08:44 +08:00
    /Users/xl/ 目录的权限呢
    maxAir
        8
    maxAir  
    OP
       2019-07-08 17:13:58 +08:00
    @ipengxh
    /Users/xl:drwxr-xr-x@ 31 xl staff 992 7 8 16:31 xl
    /Users:drwxr-xr-x 5 root admin 160 4 13 21:21 Users

    也是有 rwx 权限的
    misaka19000
        9
    misaka19000  
       2019-07-08 17:16:00 +08:00
    我记得 Mac 上面监听 80 端口是需要 root 权限的
    kidlj
        10
    kidlj  
       2019-07-08 17:27:42 +08:00
    ```
    user root owner;

    user root xl;
    user nobody;
    ```

    到底用的哪个 user ?制定这么多干什么
    yangmiemie
        11
    yangmiemie  
       2019-07-08 17:28:22 +08:00
    用 docker
    maxAir
        12
    maxAir  
    OP
       2019-07-08 17:33:34 +08:00
    @kidlj 其实只有第一个打开的哈, 我把代码贴上来, 不知道咋把前面的 注释 “#” 给弄没了, 而且字号还变大了, 我再贴一遍试试。

    user root owner;
    # user root xl;
    # user nobody;
    worker_processes 1;

    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;

    #pid logs/nginx.pid;


    events {
    worker_connections 1024;
    }


    http {
    include mime.types;
    default_type application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout 65;

    #gzip on;

    upstream node_server{
    server 127.0.0.1:3000;
    keepalive 2000;
    }
    server {
    listen 80;
    server_name localhost;

    location / {
    root /Users/xl/work/;
    index index.html index.htm;
    # add_header Access-Control-Allow-Origin *;
    # add_header Cache-Control max-age=3600;
    # concat on;
    # concat_max_files 100;
    # concat_types "application/javascript" "text/css" "text/html";
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }



    include servers/*;
    }
    kidlj
        13
    kidlj  
       2019-07-08 17:46:44 +08:00
    ```
    user root owner;
    ```

    user <user_name> <group_name>;

    owner 是什么?

    -------

    建议把 user 这一行注释掉,然后用普通用户(非 sudo )启动试一下。
    wednesdayco
        14
    wednesdayco  
       2019-07-08 17:53:57 +08:00
    跑 docker 完事儿了- -
    lowman
        15
    lowman  
       2019-07-08 17:57:07 +08:00
    403 访问的资源存在权限问题, 检 查一下启动 nginx 的 用户 是否具有对访问资源(包括各级路径)的读写及操作权限
    aaa5838769
        16
    aaa5838769  
       2019-07-08 17:58:43 +08:00
    @maxAir 这个是权限问题导致的,搜索 “ mac nginx 权限 ”关键字 希望能解决你的问题
    maxAir
        17
    maxAir  
    OP
       2019-07-08 18:05:24 +08:00
    @kidlj
    1. 注释掉 user root owner,
    2. 执行 brew services start nginx,
    提示:XLdeMacBook-Pro:~ xl$ brew services start nginx
    ==> Successfully started `nginx` (label: homebrew.mxcl.nginx),
    启动后, 还是不行,同样报 403
    maxAir
        18
    maxAir  
    OP
       2019-07-08 18:07:42 +08:00
    @aaa5838769 google 百度, 都百度好久了, 被这个问题困扰几天了, 该试都都试了,🤦‍♂️
    maxAir
        19
    maxAir  
    OP
       2019-07-08 18:12:59 +08:00
    @wednesdayco 嗯,🤦‍ 还没填过 docker 的坑, 去了解一下
    xutonglei86
        20
    xutonglei86  
       2019-07-08 18:14:24 +08:00
    目录及目录下的文件,直接给个 777 权限试试
    kidlj
        21
    kidlj  
       2019-07-08 18:45:51 +08:00 via iPhone
    应该是访问的目录设置了额外的权限控制(ls 结果有一个 @)。

    https://apple.stackexchange.com/questions/42177/what-does-signify-in-unix-file-permissions
    maxAir
        22
    maxAir  
    OP
       2019-07-08 22:16:54 +08:00
    @kidlj
    drwxrwxrwx 5 root admin 160 7 8 21:32 Users
    drwxrwxrwx+ 31 xl staff 992 7 8 16:31 xl
    去除了 @ 同样报 403 forbidden 呃。。
    kidlj
        23
    kidlj  
       2019-07-08 22:25:05 +08:00 via iPhone
    @maxAir 看上边的链接,有 + 号也是设置了额外的权限策略,比如 ACL。

    我的 Mac 上也是用 brew 安装和启动的 nginx,配置的目录权限是没有 @ 或者 + 的,访问正常。
    maxAir
        24
    maxAir  
    OP
       2019-07-08 22:38:32 +08:00
    @kidlj
    知道怎么去除这个+ 号吗? , 各种搜没搜到, 只有去除 @ 的, 同样的命令对+ 不管用
    kidlj
        25
    kidlj  
       2019-07-08 22:44:15 +08:00 via iPhone
    @maxAir 太晚了,身边也没有电脑。明天我帮你看下。
    imdong
        26
    imdong  
       2019-07-08 23:03:03 +08:00
    localtion 去掉
    root index 提取出来放上级
    shuangyeying
        27
    shuangyeying  
       2019-07-09 06:46:19 +08:00
    换个端口试试呗,貌似 macOS 的 80-1024 的端口默认都被占用了。
    jorneyr
        28
    jorneyr  
       2019-07-09 07:25:25 +08:00
    在根目录下创建一个文件夹 www,给 /www 权限 777,看看能不能跑起来,能跑起来再去研究权限的问题。
    leopku
        29
    leopku  
       2019-07-09 07:40:18 +08:00 via iPhone
    问就换 caddy
    michealzh
        30
    michealzh  
       2019-07-09 09:04:40 +08:00
    sudo 启动 nginx
    primordial
        31
    primordial  
       2019-07-09 09:10:24 +08:00
    sudu
    primordial
        32
    primordial  
       2019-07-09 09:10:42 +08:00
    maxAir
        33
    maxAir  
    OP
       2019-07-09 09:23:02 +08:00
    @imdong 试过, 和放 location 里面没有区别的
    maxAir
        34
    maxAir  
    OP
       2019-07-09 09:24:27 +08:00
    @jorneyr 不改访问路径,直接访问 nginx 自己的 index.html 是可以访问的,要指向其它目录就会 403, 应该还是权限问题
    zeevin
        35
    zeevin  
       2019-07-09 09:32:52 +08:00
    你要打开 autoindex on; 允许列目录
    doublechenpaul
        36
    doublechenpaul  
       2019-07-09 09:33:23 +08:00
    我也是 macos,10.13.2,sudo nginx 没问题
    qsbaq
        37
    qsbaq  
       2019-07-09 09:39:04 +08:00
    docker +1
    jsjscool
        38
    jsjscool  
       2019-07-09 09:40:01 +08:00
    文件夹权限原因,root 不要指向 Users 目录。新建一个独立的代码目录,比如 /code/www
    kidlj
        39
    kidlj  
       2019-07-09 09:45:59 +08:00
    分别检查一下如下目录的权限:

    ls -led /Users/xl/work/
    ls -led /Users/xl

    然后贴出来。
    maxAir
        40
    maxAir  
    OP
       2019-07-09 09:57:52 +08:00
    @jsjscool 我在根目录下新建 code 文件夹: 权限:drwxrwxrwx 3 root wheel 96 7 9 09:49 code 一样的效果,code 与 Users 平级的
    maxAir
        41
    maxAir  
    OP
       2019-07-09 10:03:51 +08:00
    @kidlj

    XLdeMacBook-Pro:/ xl$ ls -led /Users/xl/work
    drwxrwxrwx 4 xl staff 128 7 9 10:02 /Users/xl/work

    XLdeMacBook-Pro:/ xl$ ls -led /Users/xl
    drwxrwxrwx+ 31 xl staff 992 7 8 16:31 /Users/xl
    0: group:everyone deny delete
    66beta
        42
    66beta  
       2019-07-09 10:07:34 +08:00 via Android
    都喊 docker,知道 docker 在 Mac 上多耗内存吗?

    新手建议试试 bitnami nginx 套件
    kidlj
        43
    kidlj  
       2019-07-09 10:21:12 +08:00
    不是权限的问题。

    你的 /Users/xl/work 目录下有 index.html 文件吗?没有创建一个,否则是不能 list index 的。
    mryys
        44
    mryys  
       2019-07-09 10:41:49 +08:00
    LZ MAMP 了解下
    maxAir
        45
    maxAir  
    OP
       2019-07-09 10:54:28 +08:00
    @kidlj
    work 下面是我的项目文件。 入口页是可以访问的, 但是当指向项目里面的其它文件的时候, 部分文件就会报 403, 嗯~~, 直接访问 localhost 报 403 应该是 work 下面没有 index.html , 但是,项目文件不知道为什么报这个
    zlccn
        46
    zlccn  
       2019-07-09 10:56:01 +08:00
    nginx 第一行 修改成 user root; 然后重新 reload 一下
    kidlj
        47
    kidlj  
       2019-07-09 10:57:35 +08:00
    访问项目内其它文件时候,报的错也贴出来 nginx 的 error logs.

    上边只贴了一个访问 / 报 403 的 error logs.
    lancelock
        48
    lancelock  
       2019-07-09 11:23:12 +08:00
    这没必要用 nginx 啊,随便装个 http-server 就行了
    stevenkang
        49
    stevenkang  
       2019-07-09 11:29:46 +08:00
    # Nginx 容器化运行
    > docker run --name nginx -v /etc/nginx.conf:/etc/nginx/nginx.conf:ro -v /etc/nginx:/v-nginx -p 192.168.1.100:80:80 -p 192.168.1.100:443:443 --rm -d nginx
    maxAir
        50
    maxAir  
    OP
       2019-07-10 13:44:39 +08:00
    @kidlj 昨天忙,没来得及回。我现在感觉是文件加载的问题:
    http://teststatic.***.com/**/??**/dist/**/**/**.js , 比如我项目中这样的一个链接, 这两个问号如果在链接里面, 如果是 window 环境是可以直接拿到资源的(不要这两个问号也是可以拿到的, 此处我没太明白为什么这样写,不知道是什么规范), 但是如果是在 mac 上, 部分这样的 js 文件就拿不到,报 403,
    kidlj
        51
    kidlj  
       2019-07-10 13:54:53 +08:00
    @maxAir 具体的静态资源(非 index )报错,看 error log。如果找不到资源,就是 404 ;如果打不开文件或者无权进入所在目录,就是 403.
    maxAir
        52
    maxAir  
    OP
       2019-07-10 15:14:00 +08:00
    @lancelock 嗯, 我搞一个试试
    maxAir
        53
    maxAir  
    OP
       2019-07-10 15:49:10 +08:00
    @mryys 好的,thx
    WillisJ
        54
    WillisJ  
       2019-10-10 12:02:46 +08:00
    楼主解决了吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5475 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:06 · PVG 17:06 · LAX 02:06 · JFK 05:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.