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 · Jul 8, 2019 · 9520 views
    This topic created in 2500 days ago, the information mentioned may be changed or developed.

    以下是 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 replies    2019-10-10 12:02:46 +08:00
    maxAir
        1
    maxAir  
    OP
       Jul 8, 2019   ❤️ 1
    window 下没有问题,可以顺利访问,第一次用 mac,死活通不了
    MilkShake
        2
    MilkShake  
       Jul 8, 2019
    这个跟目录权限有关系的,你可以把项目路径放在其他目录上,应该就可以了。
    wuchujie
        3
    wuchujie  
       Jul 8, 2019
    为什么不加个日志呢。看下什么报错呗
    access_log /Users/xl/xxxxx.access.log
    error_log /Users/xl/xxxx.error.log
    taolu
        4
    taolu  
       Jul 8, 2019 via Android
    root 目录下如果不存在 index.html index.htm 文件,也会报 403,最好还是看日志
    maxAir
        5
    maxAir  
    OP
       Jul 8, 2019
    @aaa5838769 意思是不能放在 Desktop 里面吗, 我放在 Users/xl/work 这样也是一样的效果, 访问 localhost 就直接 403 了
    maxAir
        6
    maxAir  
    OP
       Jul 8, 2019   ❤️ 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  
       Jul 8, 2019
    /Users/xl/ 目录的权限呢
    maxAir
        8
    maxAir  
    OP
       Jul 8, 2019
    @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  
       Jul 8, 2019
    我记得 Mac 上面监听 80 端口是需要 root 权限的
    kidlj
        10
    kidlj  
       Jul 8, 2019
    ```
    user root owner;

    user root xl;
    user nobody;
    ```

    到底用的哪个 user ?制定这么多干什么
    yangmiemie
        11
    yangmiemie  
       Jul 8, 2019
    用 docker
    maxAir
        12
    maxAir  
    OP
       Jul 8, 2019
    @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  
       Jul 8, 2019
    ```
    user root owner;
    ```

    user <user_name> <group_name>;

    owner 是什么?

    -------

    建议把 user 这一行注释掉,然后用普通用户(非 sudo )启动试一下。
    wednesdayco
        14
    wednesdayco  
       Jul 8, 2019
    跑 docker 完事儿了- -
    lowman
        15
    lowman  
       Jul 8, 2019
    403 访问的资源存在权限问题, 检 查一下启动 nginx 的 用户 是否具有对访问资源(包括各级路径)的读写及操作权限
    MilkShake
        16
    MilkShake  
       Jul 8, 2019
    @maxAir 这个是权限问题导致的,搜索 “ mac nginx 权限 ”关键字 希望能解决你的问题
    maxAir
        17
    maxAir  
    OP
       Jul 8, 2019
    @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
       Jul 8, 2019
    @aaa5838769 google 百度, 都百度好久了, 被这个问题困扰几天了, 该试都都试了,🤦‍♂️
    maxAir
        19
    maxAir  
    OP
       Jul 8, 2019
    @wednesdayco 嗯,🤦‍ 还没填过 docker 的坑, 去了解一下
    xutonglei86
        20
    xutonglei86  
       Jul 8, 2019
    目录及目录下的文件,直接给个 777 权限试试
    kidlj
        21
    kidlj  
       Jul 8, 2019 via iPhone
    应该是访问的目录设置了额外的权限控制(ls 结果有一个 @)。

    https://apple.stackexchange.com/questions/42177/what-does-signify-in-unix-file-permissions
    maxAir
        22
    maxAir  
    OP
       Jul 8, 2019
    @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  
       Jul 8, 2019 via iPhone
    @maxAir 看上边的链接,有 + 号也是设置了额外的权限策略,比如 ACL。

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

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

    然后贴出来。
    maxAir
        40
    maxAir  
    OP
       Jul 9, 2019
    @jsjscool 我在根目录下新建 code 文件夹: 权限:drwxrwxrwx 3 root wheel 96 7 9 09:49 code 一样的效果,code 与 Users 平级的
    maxAir
        41
    maxAir  
    OP
       Jul 9, 2019
    @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  
       Jul 9, 2019 via Android
    都喊 docker,知道 docker 在 Mac 上多耗内存吗?

    新手建议试试 bitnami nginx 套件
    kidlj
        43
    kidlj  
       Jul 9, 2019
    不是权限的问题。

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

    上边只贴了一个访问 / 报 403 的 error logs.
    lancelock
        48
    lancelock  
       Jul 9, 2019
    这没必要用 nginx 啊,随便装个 http-server 就行了
    stevenkang
        49
    stevenkang  
       Jul 9, 2019
    # 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
       Jul 10, 2019
    @kidlj 昨天忙,没来得及回。我现在感觉是文件加载的问题:
    http://teststatic.***.com/**/??**/dist/**/**/**.js , 比如我项目中这样的一个链接, 这两个问号如果在链接里面, 如果是 window 环境是可以直接拿到资源的(不要这两个问号也是可以拿到的, 此处我没太明白为什么这样写,不知道是什么规范), 但是如果是在 mac 上, 部分这样的 js 文件就拿不到,报 403,
    kidlj
        51
    kidlj  
       Jul 10, 2019
    @maxAir 具体的静态资源(非 index )报错,看 error log。如果找不到资源,就是 404 ;如果打不开文件或者无权进入所在目录,就是 403.
    maxAir
        52
    maxAir  
    OP
       Jul 10, 2019
    @lancelock 嗯, 我搞一个试试
    maxAir
        53
    maxAir  
    OP
       Jul 10, 2019
    @mryys 好的,thx
    WillisJ
        54
    WillisJ  
       Oct 10, 2019
    楼主解决了吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1534 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 81ms · UTC 16:47 · PVG 00:47 · LAX 09:47 · JFK 12:47
    ♥ Do have faith in what you're doing.