Nginx 502 权限错误现象分析
今日在使用 uwsgi 配合 nginx 部署 flask 应用的过程中,遭遇了 502 错误。通过 vim /var/log/nginx/error.log 检查 nginx 的错误日志,发现了如下报错信息:
2026/07/22 00:46:36 [crit] 15890#15890: *74 connect() to unix:/root/jianshuvue/jianshu.sock failed (13: Permission denied) while connecting to upstream, client: 120.42.13.98, server: jianshu.weiweiblog.cn, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/root/jianshuvue/jianshu.sock:", host: "jianshu.weiweiblog.cn", referrer: "http://jianshu.weiweiblog.cn/jianshu/67eb7ed414d3"
出现 Permission denied 提示,表明存在权限访问障碍。执行 ps -ef | grep nginx 命令以查看 nginx 当前的进程运行状态:
root 15889 1 0 00:01 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 15890 15889 0 00:01 ? 00:00:00 nginx: worker process root 16795 15654 0 00:48 pts/3 00:00:00 grep --color=auto nginx
观察发现,nginx 的 worker 进程由 www-data 用户运行,而创建的 /root/jianshuvue/jianshu.sock 文件所有者为 root。因此,解决方案是将 nginx 进程的运行用户更改为 root,具体操作见 vim /etc/nginx/nginx.conf:
# user www-data;
user root;
worker_processes auto;
pid /run/nginx.pid;
随后通过 ,/etc/init.d/nginx restart 重启 nginx 服务,网站即可恢复正常访问。
以上内容即为本次问题的完整解决过程,希望能对各位的学习有所帮助,也欢迎大家继续支持本站。