nginx 由于权限被拒绝而无法为 React 提供服务。 www-data 无法访问前端/构建/

问题描述 投票:0回答:1

我正在尝试配置 nginx,以便它可以同时服务于 React 和 Django。这是我的配置:

server {
    listen 80;
    server_name 182.20.4.110 mydomain.io;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/myproj_app/static/;
    }


    location / {
        alias /home/ubuntu/myproj_app/frontend/build/;
    }

    location /apidoc {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

}

https://gist.github.com/axilaris/2329d3ff73034b51483366772c7cef9c

但是,我不断

403 Forbidden

并且根据 nginx 错误日志,它在 React 构建目录中不断获得权限被拒绝

sudo tail -f /var/log/nginx/error.log

2024/04/01 12:23:57 [error] 3616#3616: *68 "/home/ubuntu/myproj_app/frontend/build/index.html" is forbidden (13: Permission denied), client: 172.20.4.193, server: 182.20.4.110, request: "GET / HTTP/1.1", host: "182.20.4.110"
2024/04/01 12:23:58 [error] 3616#3616: *69 "/home/ubuntu/myproj_app/frontend/build/index.html" is forbidden (13: Permission denied), client: 172.20.1.172, server: 182.20.4.110, request: "GET / HTTP/1.1", host: "182.20.4.110"
2024/04/01 12:24:21 [error] 3616#3616: *67 "/home/ubuntu/myproj_app/frontend/build/index.html" is forbidden (13: Permission denied), client: 172.20.1.172, server: 182.20.4.110, request: "GET / HTTP/1.1", host: "mydomain.io"
2024/04/01 12:24:27 [error] 3616#3616: *70 "/home/ubuntu/myproj_app/frontend/build/index.html" is forbidden (13: Permission denied), client: 172.20.4.193, server: 182.20.4.110, request: "GET / HTTP/1.1", host: "182.20.4.110"
2024/04/01 12:24:28 [error] 3616#3616: *71 "/home/ubuntu/myproj_app/frontend/build/index.html" is forbidden (13: Permission denied), client: 172.20.1.172, server: 182.20.4.110, request: "GET / HTTP/1.1", host: "182.20.4.110"
2024/04/01 12:24:30 [error] 3616#3616: *67 "/home/ubuntu/myproj_app/frontend/build/index.html" is forbidden (13: Permission denied), client: 172.20.1.172, server: 182.20.4.110, request: "GET / HTTP/1.1", host: "mydomain.io"

我尝试将所有者更改为 nginx 用户 www-data 但仍然被拒绝

ubuntu@ip-182-20-4-110:~$ ls -ld /home/ubuntu/myproj_app/frontend/build/
drwxr-xr-x 3 www-data www-data 4096 Apr  1 09:47 /home/ubuntu/myproj_app/frontend/build/
ubuntu@ip-182-20-4-110:~$ sudo -u www-data ls /home/ubuntu/myproj_app/frontend/build/
ls: cannot access '/home/ubuntu/myproj_app/frontend/build/': Permission denied

我该如何解决这个问题。我无法在 ubuntu 22.04 上使用 nginx 为我的 React 页面提供服务。这是由我无法控制的人在 AWS 上的 EC2 上设置的。 AWS 是否可以在策略中进行任何设置来阻止访问?

reactjs django ubuntu nginx chown
1个回答
0
投票

这成功了。必须在获得许可的情况下遍历父级。

ubuntu@ip-172-20-3-120:~$ sudo usermod -a -G ubuntu www-data 
ubuntu@ip-172-20-3-120:~$ sudo chmod 750 /home/ubuntu/ 
ubuntu@ip-172-20-3-120:~$ sudo chmod 750 /home/ubuntu/myproj_app/ 
ubuntu@ip-172-20-3-120:~$ sudo chmod 750 /home/ubuntu/myproj_app/frontend/
© www.soinside.com 2019 - 2024. All rights reserved.