nginx启动进程对文件的权限掌控

最近在启动二进制的nginx时,总是遇到一个报错信息:/usr/local/nginx/client_body_temp/0000000018" failed (13: Permission denied);然后一直不怎么,明白,为啥要报这个错误,就开始去检查文件的权限

第一步:检查nginx启动目录下,文件的权限

找到nginx的启动目录,然后进入目录:

bash 复制代码
[AD\y ~]$ whereis nginx
nginx: /usr/local/nginx
[AD\y ~]$cd /usr/local/nginx
[AD\yc ~]$ ll
total 36
drwx------  2 nobody root 4096 Apr  2 18:30 client_body_temp
drwxrwxr-x  2 cwos   cwos 4096 Mar 27 14:47 conf
drwx------  2 nobody root 4096 Mar 25 11:16 fastcgi_temp
drwxr-xr-x  2 cwos   cwos 4096 Mar 25 10:49 html
drwxrwxr-x  2 cwos   cwos 4096 Mar 27 14:48 logs
drwx------ 12 nobody root 4096 Mar 27 14:53 proxy_temp
drwxrwxr-x  2 cwos   cwos 4096 Mar 25 10:49 sbin
drwx------  2 nobody root 4096 Mar 25 11:16 scgi_temp
drwx------  2 nobody root 4096 Mar 25 11:16 uwsgi_temp

查看文件的大部分是属于root和cwos用户的,然后我们查看nginx进程的启动用户是那个

bash 复制代码
[AD\y nginx]$ ps -aux|grep nginx
AD\yckj+  5801  0.0  0.0 112808   968 pts/0    S+   14:49   0:00 grep --color=auto nginx
root     26741  0.0  0.0  41864  1552 ?        Ss   Mar27   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   26742  0.0  0.0  54728  5884 ?        S    Mar27   0:03 nginx: worker process
[AD\ynginx]$

从结果中我们知道nginx的worker process进程是nobody用户,master进程是root启动的,也就是说实际执行nginx.conf中的静态文件执行工作的是nobody,接着我们检查nginx的配置文件中user信息

第二步:查看nginx配置文件nginx.conf

bash 复制代码
[AD\yc nginx]$ cd conf/
[AD\yc conf]$ cat nginx.conf

#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;
}

发现配置文件中user信息为注释状态,则nginx的工作进程就默认使用nobody用户进行启动了,然后我们继续探究报错的文件:usr/local/nginx/client_body_temp,对于nobody而已有可执行的权限,于是我看了这个目录的权限,果然这个目录client_body_temp,对于nobody没有可读写执行的权限,于是我改了这个目录的权限:

bash 复制代码
#切换成root用户
[root@client_body_temp]# cd ..
[root@ nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@ nginx]# ll
total 36
drwx------  2 nobody root 4096 Apr  2 18:30 client_body_temp
drwxrwxr-x  2 cwos   cwos 4096 Mar 27 14:47 conf
drwx------  2 nobody root 4096 Mar 25 11:16 fastcgi_temp
drwxr-xr-x  2 cwos   cwos 4096 Mar 25 10:49 html
drwxrwxr-x  2 cwos   cwos 4096 Mar 27 14:48 logs
drwx------ 12 nobody root 4096 Mar 27 14:53 proxy_temp
drwxrwxr-x  2 cwos   cwos 4096 Mar 25 10:49 sbin
drwx------  2 nobody root 4096 Mar 25 11:16 scgi_temp
drwx------  2 nobody root 4096 Mar 25 11:16 uwsgi_temp
[root@jsglz-ibiscp2021ndddkf-dev-ocr-f2u9ua nginx]# chown nobody:root -R client_body_temp/
[root@jsglz-ibiscp2021ndddkf-dev-ocr-f2u9ua nginx]#

第三步:修改目录权限后,再次查看nginx的error日志

修改目录权限后,再次在页面上访问nginx代理的页面,观察后端ngin日志

bash 复制代码
[root@ nginx]# cd logs/
[root@ logs]# ls
access.log  error.log  nginx.pid
[root@ logs]# tail -100f error.log
2026/03/25 11:52:45 [error] 18609#0: *68 directory index of "/data/cwos/cbs-real-front//" is forbidden, client: 10.135.8.103, server: _, request: "GET /cbs-real/ HTTP/1.1", host: "10.128.165.129", referrer: "http://10.128.165.129/cbs-real/index.html"
2026/03/25 11:57:17 [notice] 17067#0: signal process started

果然报错日志不再是之前的信息,说明nobody用户已经有了执行目录client_body_temp的权限了

题外话

此外,在这次nginx权限处理的过程中,我顺便解决了另外一个权限文件,问题也和权限有关,保存信息如下:2026/03/25 14:15:10 error 17074#0: *160 directory index of "/data/cwos/cbs-shield-front//" is forbidden , client: 10.135.8.103, server: _, request: "GET /cbs-shield/ HTTP/1.1", host: "10.128.165.129", referrer: "http://10.128.165.129/cbs-shield/index.html"

原因是我在浏览器页面上访问nginx配置的静态文件路径,结果浏览器一直返回404,然后nginx后端日志就一直提示forbidden,我就查看了目录/data/cwos/cbs-shield-front的权限,如下:

bash 复制代码
[root@ cbs-shield-front]# ll
total 9156
drwxrwxrwx 5 cwos cwos      99 Dec  2 17:04 cbs-shield
-rwxrwxrwx 1 cwos cwos 9371843 Mar 25 14:45 cbs-shield-front-5.2.1.202603061804.tar.gz
drwxrwxrwx 6 cwos cwos     112 Apr 22  2025 home
drwxrwxrwx 3 cwos cwos      24 Mar  6 17:51 portal

发现整个目录的所有文件都是rwx状态,而且全部都是777,说明整个目录已经有可执行权限了,然后我就很不理解,为啥nginx不能正常访问这个目录下的静态文件,各种纠结和询问同事之下,才知道,这个/data/cwos/cbs-shield-front/目录,每一个层,nobody用户都得拥有可执行权限,才能正常访问静态文件成功,于是我逐层网上查看权限,如下图:

bash 复制代码
[root@jsglz-ibiscp2021ndddkf-dev-ocr-f2u9ua cbs-shield-front]# cd ..
[root@jsglz-ibiscp2021ndddkf-dev-ocr-f2u9ua cwos]# ll
total 392220
drw-rw-rw- 5 cwos cwos       100 Mar 26 16:39 cbs-shield-front
...
[root@j cwos]# cd ..
[root@j data]# ll
drwx------ 29 cwos        cwos                 4096 Mar 27 15:34 cwos

终于我找到原因了,这个目录/data,没有可执行权限,这个目录/data/cwos,也没有可执行权限,于是我把这两个目录的可执行权限,加上,赋予所有用户后,浏览器页面上的访问终于不再报404...,我在服务器上也能正常访问通了,如下:

bash 复制代码
[root@jrf cbs-ibis-front]# curl -X GET http://10.128.175.98/cbs-shield/home/index.html
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="favicon.ico"><title>****</title><script defer="defer" type="module" src="js/chunk-vendors.2a17b061.js"></script><script defer="defer" type="module" src="js/app.a1df35db.js"></script><link href="css/chunk-vendors.5a7fd0e2.css" rel="stylesheet"><link href="css/app.cbdd3ed9.css" rel="stylesheet"><script defer="defer" src="js/chunk-vendors-legacy.2ce98c45.js" nomodule></script><script defer="defer" src="js/app-legacy.141e4c1f.js" nomodule></script></head><body><noscript><strong>We're sorry but **** doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="sub-app"></div></body></html>[root@jrf cbs-ibis-front]#

总结:nginx代理的静态文件路径地址,如:http://10.128.175.98/cbs-shield/home/index.html

bash 复制代码
 location /cbs-shield {
        alias /data/cwos/cbs-shield-front/;
        try_files $uri $uri/ /cbs-shield/portal$uri /cbs-shield/portal/cbs-shield/index.html;
    }

这种静态文件的访问,内部一般包含js等脚本,所以文件是需要执行的,不是直接返回的,需要整个目录的文件夹都有可执行权限,否则就会报404未授权

相关推荐
古法老登瞎折腾2 小时前
大数据智能运维实战(十一):运维 MCP 的工程化——权限、限流、审计与错误处理
java·大数据·运维·spring ai·mcp
琥珀.2 小时前
mysql中json数组转列表查询
linux·运维·服务器
dawnsky.liu3 小时前
在 RHEL 中使用 goose 实现远程主机管理
linux·运维·ai
Linux运维技术栈5 小时前
CVE-2026-91403 高危漏洞修复:Rocky Linux 9 多版本 JDK 环境清理实操
java·linux·运维
云上先途15 小时前
企业 AI 服务部署周期多久,会不会影响原有业务运转?
大数据·运维·人工智能
执伞人ONLY6 小时前
AI知识库自动化知识加工没做对,人工整理永远追不上知识产生
运维·人工智能·自动化
叼烟扛炮6 小时前
Linux 学习笔记(三十三):线程互斥——互斥锁(Mutex)与死锁
linux·运维·服务器·笔记·学习·1024程序员节
无足鸟ICT7 小时前
【RHCA+】who命令(查看已登陆用户)
linux·运维·服务器
行思理7 小时前
Nginx 跨站请求(CORS)配置完全指南:从原理到生产实践
java·前端·nginx
RacentCLM7 小时前
证书即服务CaaS:从申请、续期到部署的自动化链路拆解
运维·数据库·自动化·ssl