大网数据广告活动
服务器运维问答

Nginx displaying failed (13: Permission denied) when trying to access new site

· 52CE 我爱测

I am trying to set up my own web server to learn a bit more about server admin. I have decided that I want to serve each sites files from a public_html folder inside the users /home directory. I have installed Nginx, edited the n…

问题原文

原帖链接 · Chris · CC BY-SA 4.0

I am trying to set up my own web server to learn a bit more about server admin.

I have decided that I want to serve each sites files from a public_html folder inside the users /home directory.

I have installed Nginx, edited the nginx.conf and changed the username / group to nginx.

I have added a new user for the new site and changed the vhosts file to look like so;

server {
    listen         80;
    listen         [::]:80;
    server_name    website.com www.website.com;
    root           /home/website/public_html;
    index          index.html index.htm index.php;

    location / {
      try_files $uri $uri/ =404;
    }

    location ~* \.php$ {
    fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

But when I try and get to the site, it returns a 404 Not Found.

When I check the error log, I am seeing the following errors;

2019/01/02 19:49:45 [crit] 18248#0: *1 stat() "/home/website/public_html/" failed (13: Permission denied)

Any chance someone has come across this before and could tell me how to handle it?

I have had a look around and saw some posts about getenforce, but when i run it, it says Disabled.

I am using CentOS7 if that makes any difference.

Cheers,

社区高分解答

原帖链接 · Herbert · CC BY-SA 4.0

A good way to debug such nginx errors is by checking the logs and using namei to check whether nginx has access to all ancestor folders.

Consider /static/vendor.js gives a HTTP 403 due to this configuration:

server {
    listen 80;

    location /static/ {
    alias /home/ubuntu/bla/front/dist/;
    }
}

First check out nginx access logs, in ubuntu it's here:

tail /var/log/nginx/error.log

Mine says:

2023/07/25 11:52:05 [error] 15138#15138: *3 open() "/home/ubuntu/bla/front/dist/vendor.js" failed (13: Permission denied), client: 145.90.169.214, server: , request: "GET /static/vendor.js HTTP/1.1", host: "129.125.55.156

I notice the file /home/ubuntu/bla/front/dist/vendor.js does exist, so I check whether www-data (nginx user) can access it:

sudo -u www-data namei /home/ubuntu/bla/front/dist/vendor.js

f: /home/ubuntu/bla/front/dist/vendor.js
 d /
 d home
 d ubuntu
   bla - Permission denied

Apparently the bla folder can't be accessed, this is likely due to not having rx rights on the ubuntu folder. That is, if the parent can't be read, the child is invisible. I'll fix this using setfacl, a way to give fine-grained ACL permissions without changing the user and group of a file.

sudo setfacl -m g:www-data:rx /home/ubuntu/

And now:

sudo -u www-data namei /home/ubuntu/bla/front/dist/vendor.js

f: /home/ubuntu/bla/front/dist/vendor.js
 d /
 d home
 d ubuntu
 d bla
 d front
 d dist
 - vendor.js

The file is accessible by www-data, and also nginx's 403 disappeared.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。