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

2019-07-08 16:16:03 +08:00
 maxAir

以下是 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;

8290 次点击
所在节点    NGINX
54 条回复
maxAir
2019-07-08 16:17:23 +08:00
window 下没有问题,可以顺利访问,第一次用 mac,死活通不了
aaa5838769
2019-07-08 16:23:08 +08:00
这个跟目录权限有关系的,你可以把项目路径放在其他目录上,应该就可以了。
wuchujie
2019-07-08 16:29:28 +08:00
为什么不加个日志呢。看下什么报错呗
access_log /Users/xl/xxxxx.access.log
error_log /Users/xl/xxxx.error.log
taolu
2019-07-08 16:39:41 +08:00
root 目录下如果不存在 index.html index.htm 文件,也会报 403,最好还是看日志
maxAir
2019-07-08 16:41:05 +08:00
@aaa5838769 意思是不能放在 Desktop 里面吗, 我放在 Users/xl/work 这样也是一样的效果, 访问 localhost 就直接 403 了
maxAir
2019-07-08 17:04:30 +08:00
@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
2019-07-08 17:08:44 +08:00
/Users/xl/ 目录的权限呢
maxAir
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
2019-07-08 17:16:00 +08:00
我记得 Mac 上面监听 80 端口是需要 root 权限的
kidlj
2019-07-08 17:27:42 +08:00
```
user root owner;

user root xl;
user nobody;
```

到底用的哪个 user ?制定这么多干什么
yangmiemie
2019-07-08 17:28:22 +08:00
用 docker
maxAir
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
2019-07-08 17:46:44 +08:00
```
user root owner;
```

user <user_name> <group_name>;

owner 是什么?

-------

建议把 user 这一行注释掉,然后用普通用户(非 sudo )启动试一下。
wednesdayco
2019-07-08 17:53:57 +08:00
跑 docker 完事儿了- -
lowman
2019-07-08 17:57:07 +08:00
403 访问的资源存在权限问题, 检 查一下启动 nginx 的 用户 是否具有对访问资源(包括各级路径)的读写及操作权限
aaa5838769
2019-07-08 17:58:43 +08:00
@maxAir 这个是权限问题导致的,搜索 “ mac nginx 权限 ”关键字 希望能解决你的问题
maxAir
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
2019-07-08 18:07:42 +08:00
@aaa5838769 google 百度, 都百度好久了, 被这个问题困扰几天了, 该试都都试了,🤦‍♂️
maxAir
2019-07-08 18:12:59 +08:00
@wednesdayco 嗯,🤦‍ 还没填过 docker 的坑, 去了解一下
xutonglei86
2019-07-08 18:14:24 +08:00
目录及目录下的文件,直接给个 777 权限试试

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/581046

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX