问题
在 CentOS 上安装完 NGINX 之后,尝试修改 NGINX 的 root 地址
1 2 3 4 5 6 7
| ... http{ server{ root /root/blog; } } ...
|
在 root 用户下,新建文件夹 blog 如下
重启 NGINX 服务
访问 HTTP 80 端口
1 2 3 4 5 6 7 8
| $ curl 0.0.0.0:80 <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.12.2</center> </body> </html>
|
排查
排查错误日志,发现为Permission denied
权限错误
1 2 3
| $ tail /var/log/nginx/error.log
2019/04/22 20:36:47 [error] 26764#0: *68 "/root/blog/index.html" is forbidden (13: Permission denied), client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "0.0.0.0"
|
查看 NGINX 的进程用户,发现 NGINX 的 worker 进程为 NGINX 用户启动的
1 2 3 4 5
| $ ps -aux | grep nginx root 26085 0.0 0.0 125720 5268 ? Ss 17:05 0:00 nginx: master process /usr/sbin/nginx nginx 26764 0.0 0.0 126140 4224 ? S 19:34 0:00 nginx: worker process nginx 26765 0.0 0.0 126140 4224 ? S 19:34 0:00 nginx: worker process root 26955 0.0 0.0 112660 976 pts/5 S+ 20:41 0:00 grep --color=auto nginx
|
查看 root 文件夹的用户权限,发现 root 文件夹的其他用户只有读权限,这样 NGINX 用户是无法访问的,于是可以理解为何报错 403 了。
1 2 3
| $ ls -l
dr-xr-xr--. 10 root root 4096 Apr 22 20:35 root
|
解决
简单粗暴法
将 nginx.conf 的 user 修改为 root,这样 worker 进程是在 root 用户下启动的,可以顺利访问 /root/blog 文件夹。
更安全的方法
上述方法让 root 用户跑 worker 进程,太危险了。其实可以新建一个账号,在该账号下放置静态文件,然后将其权限暴露,这种做法更加安全。
1 2 3 4
| $ adduser www $ passwd www $ cp -R /root/blog /home/www/ $ chmod 755 /home/www
|
再将 nginx.conf 的 root 地址修改为/home/www/blog
1 2 3 4 5
| http{ server{ root:/home/www/blog; } }
|
这样,就能正常访问 root 主页了