原创

nginx如何解决403 Forbidden问题

问题说明

nginx配置location后,无法访问,返回403

        location /demo.html {
            alias /home/work/demo.html;
        }

file

nginx 访问日志/var/log/nginx/access.log提示:

20:34:02 [error] 420066#0: *186516 open() "/home/work/demo.html" failed (13: Permission denied), client: 219.219.219.219, server: demo.com, request: "GET /demo.html HTTP/2.0", host: "demo.com"

问题原因

nginx启动用户默认是nginx,用户nginx无法访问/home/work/demo.html文件导致的
//测试nginx是否可以访问文件权限

[root@demo root]# sudo -u nginx stat /home/work/demo.html
stat: cannot statx '/home/work/demo.html': Permission denied

解决办法

1.将nginx用户加入work用户组

# sudo gpasswd -a nginx work
Adding user nginx to group blog

2.修改用户组访问权限

# chmod g+x /home/work

3.测试结果,访问成功

# sudo -u nginx stat /home/work/demo.html
  File: /home/work/demo.html
  Size: 32            Blocks: 8          IO Block: 4096   regular file

4.重新加载nginx配置即可

# nginx -s reload
正文到此结束