找回密码
 新建账号

nginx + php 上传文件时报 500 错误

[复制链接]
php 发表于 2024/5/10 17:18 | 显示全部楼层 |阅读模式
nginx + php 上传文件时出现了 500 错误。
  1. PS C:\Users\wuxiancheng\Downloads> curl --url "https://www.wuxiancheng.cn/upload.php" --form "file=@1.jpg" --include
  2. HTTP/2 500
  3. server: nginx
  4. date: Fri, 10 May 2024 07:17:06 GMT
  5. content-type: text/html
  6. content-length: 497
复制代码
经过排查,PHP 方面没有问题,没有记录任何错误,也没有任何权限问题。
nginx 日志记录了以下错误信息
  1. 2024/05/10 15:17:06 [crit] 157157#157157: *1 open() "/server/nginx/client_body_temp/0000000001" failed (13: Permission denied), client: 2408:8266:2b01:b90:6804:bed0:6bb9:68f5, server: www.wuxiancheng.cn, request: "POST /i.php HTTP/2.0", host: "www.wuxiancheng.cn"
复制代码
错误日志已经明确指出错误出在运行 nginx 的用户对 client_body_temp 目录没有写权限。
  1. root@www.wuxiancheng.cn:~# ls -l /server/nginx/
  2. total 36
  3. drwx------ 2 nobody nginx 4096 May  7 18:06 client_body_temp
  4. drwx------ 4 nobody nginx 4096 May  7 23:58 fastcgi_temp
  5. drwx------ 6 nobody nginx 4096 May  7 22:34 proxy_temp
  6. drwx------ 2 nobody nginx 4096 May  7 18:06 scgi_temp
  7. drwx------ 2 nobody nginx 4096 May  7 18:06 uwsgi_temp
复制代码
以上内容是节选,并不是全部内容。
运行 nginx 的用户是 nginx 用户组也是 nginx,而 client_body_temp fastcgi_temp proxy_temp scgi_temp uwsgi_temp 的属主(owner)是 nobody,如果以 root 身份运行 nginx,nginx 配置文件中可以指定运行 nginx 的用户和用户组,缺省用户和用户组是 nobody,如果以普通用户身份运行 nginx ,nginx user 指令配置的用户和用户组会被忽略,并且触发一个 warning 级别的错误。
  1. user nobody nobody;
复制代码
  1. 2024/05/10 16:46:08 [warn] 157623#157623: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /server/nginx/conf/nginx.conf:1
复制代码
如果以 root 身份运行 nginx 而在运行 nginx 之前不存在某些目录或者文件,运行之后会生成这些目录或者文件,以 root 身份运行 nginx 时 nginx user 指令配置的用户和用户组不会被忽略,这些生成的目录和文件的属主和用户组就变成了 nginx user 指令配置的用户和用户组。
要解决这个问题,可以关闭 nginx 后删除这些异常创建的目录和文件,这些目录和文件后续会自动创建,或者使用 chown 命令修改它们的属主和用户组。
  1. rm -rf /server/nginx/{client_body_temp,fastcgi_temp,proxy_temp,scgi_temp,uwsgi_temp}
复制代码
完成操作后重新启动 nginx 就没有权限问题了。

手机版|轻松E站

GMT+8, 2024/7/27 17:21

快速回复 返回顶部 返回列表