作者:zsx2502853407 | 来源:互联网 | 2024-12-13 10:56
最近遇到了一个棘手的问题,即在尝试通过uWSGI和nginx运行Python应用程序时,频繁出现权限错误:
-bash-4.1# > /var/log/nginx/error.log
-bash-4.1# curl 127.0.0.1:8080
502 Bad Gateway
nginx/1.6.2
-bash-4.1# cat /var/log/nginx/error.log
2015/01/16 18:51:32 [crit] 4937#0: *1 connect() to 127.0.0.1:3301 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: uwsgi_demo, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:3301", host: "127.0.0.1:8080"
测试的应用程序代码如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ['Hello World']
测试程序所在的目录结构如下所示:
-bash-4.1# ll
total 12
-rw-r--r--. 1 nginx nginx 139 Jan 16 15:34 app.py
-rw-r--r--. 1 root root 136 Jan 16 18:37 demo.ini
-rw-r-----. 1 root root 95 Jan 16 18:45 demo.log
uWSGI的配置文件内容为:
[uwsgi]
socket = 127.0.0.1:3031
wsgi-file = app.py
uid = nginx
gid = nginx
chmod-socket = 666
logto = demo.log
stats = 127.0.0.1:9191
nginx的配置如下:
server {
listen 8080;
server_name uwsgi_demo;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3301;
}
}
当前运行的进程情况如下:
-bash-4.1# ps aux | grep nginx
nginx 4819 0.0 0.3 53996 6852 pts/4 S+ 18:48 0:00 uwsgi26 demo.ini
nginx 4821 0.0 0.3 73460 6048 pts/4 S+ 18:48 0:00 uwsgi26 demo.ini
root 4935 0.0 0.1 141788 2568 ? Ss 18:50 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 4937 0.0 0.1 142184 3292 ? S 18:50 0:00 nginx: worker process
root 4939 0.0 0.0 6380 692 pts/2 S+ 18:50 0:00 grep nginx
根据上述信息,此问题可能是由于文件权限设置不当导致的。建议检查所有相关文件和目录的权限设置,确保它们能够被正确的用户(如nginx)访问。此外,也可以尝试调整uWSGI配置中的chmod-socket
参数,以确保套接字具有适当的权限。