Flask + uWSGI + Nginx + Fedora 20 安装问题

3

我是一名帮助翻译的助手。

我正在尝试将一个使用Flask框架开发的应用程序通过uWSGI部署到Nginx上,操作系统为Fedora 20,但遇到了一些问题。

我有以下配置文件:

Nginx - 默认配置文件,进行了以下编辑:

location / { try_files $uri @yourapplication; }
location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/sjudson_app_uswgi.sock;
}

uWSGI:

[uwsgi]
socket = /tmp/%n.sock
wsgi-file = sjudson_app/sjudson_app.py
callable = sjudson
master = true
chmod-socket = 666
logto = /home/server/logs/uwsgi.log

首先,当我尝试运行时:

uwsgi --ini sjudson_app_uwsgi.ini

我只是得到了:

[uWSGI] getting INI configuration from sjudson_app_uwsgi.ini

无限期。其次,当我直接从命令行运行它时:
uwsgi --socket /tmp/sjudson_app_uwsgi.sock --wsgi-file sjudson_app/sjudson_app.py --callable sjudson --master --chmod-socket=666

I get:

*** Starting uWSGI 2.0.3 (32bit) on [Tue Mar 25 17:58:44 2014] ***
compiled with version: 4.8.2 20131212 (Red Hat 4.8.2-7) on 25 March 2014 16:48:01
os: Linux-3.13.6-200.fc20.i686+PAE #1 SMP Fri Mar 7 17:17:53 UTC 2014
nodename: new-host-6
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/server
detected binary path: /usr/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/sjudson_app_uwsgi.sock fd 3
Python version: 2.7.5 (default, Feb 19 2014, 13:47:40)  [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x881cc40
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 127952 bytes (124 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x881cc40 pid: 1131 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1131)
spawned uWSGI worker 1 (pid: 1136, cores: 1)

但是当我尝试连接服务器时,出现了502错误。我注意到这个stackoverflow帖子:Flask, nginx和uwsgi,其中涉及了类似的问题,最受欢迎的答案涉及到套接字权限不正确。然而,我确实使用了chmod-socket=666参数,并查看了/tmp:

srw-rw-rw-.  1 server server    0 Mar 25 17:53 sjudson_app_uwsgi.sock

所以在我看来,这不是问题所在。

编辑:

意识到我忘记包括Nginx的错误信息:

2014/03/26 07:58:38 [crit] 792#0: *15 connect() to unix:/tmp/sjudson_app_uswgi.sock 
failed (2: No such file or directory) while connecting to upstream, client: 
173.79.242.54, server: localhost, request: "GET / HTTP/1.1", upstream: 
"uwsgi://unix:/tmp/sjudson_app_uswgi.sock:", host: "173.79.242.54"

我不确定这种情况下所谓的“没有该文件或目录”是什么意思。我知道.sock文件是存在的,那么它到底在找什么呢?


检查nginx错误日志,uWSGI已经正确启动,因此您需要弄清楚为什么nginx无法连接到它。当您使用"logto"启动时,日志会输出到指定的文件中,因此在命令行中看不到更多内容是正常的。 - roberto
删除logto指令并从终端重新运行uwsgi。之后尝试从nginx重新连接(一定要始终检查nginx错误日志)。在Unix套接字上出现“没有此文件或目录”的意思是没有进程正在侦听该套接字。如果仍然无法工作,请尝试使用tcp套接字。 - roberto
非常感谢您的帮助。不幸的是,它没有解决问题。您所说的tcp sockets是什么意思?您是否指明要运行的套接字,例如127.0.0.1:3031? - JackGibbs
类似错误:2014/03/26 09:16:51 [crit] 4131#0: *1 connect() to 127.0.0.1:3031 failed (13: Permission denied) while connecting to upstream, client: 173.79.242.54, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://127.0.0.1:3031", host: "173.79.242.54" - 该服务器正在以默认的nginx用户运行,该用户只有在nginx安装时授予的权限。我需要将其添加到哪个组中吗? - JackGibbs
2
开始禁用SELinux,在标准系统中,TCP套接字没有权限(除了端口<1024)。在确保问题是由SELinux策略引起的之后,您可以开始为您的nginx+uWSGI设置编写一个策略(是的,您需要投入时间来理解SELinux的工作原理)。 - roberto
显示剩余5条评论
2个回答

2

我几乎有着完全相同的设置和错误信息。要解决这个问题,我必须执行以下操作:

  • change the socket statement in my uwsgi.ini file to:

    socket = 127.0.0.1:8081
    
  • add the following to my nginx.conf file (outside of the server statement):

    upstream uwsgi { server 127.0.0.1:8081; }
    
  • change the location statement in my nginx.conf file to:

    location / { try_files $uri @uwsgi; }
    location @uwsgi {
        include uwsgi_params;
        uwsgi_pass uwsgi;
    }
    

这样就使得nginx和uwsgi能够通信。这些指令来自http://blog.djcentric.com/setting-up-uswgi-nginx-what-you-need-to-know/


0
请在nginx配置中更改字符串,将
uwsgi_pass unix:/tmp/sjudson_app_uswgi.sock;
更改为
uwsgi_pass unix:///tmp/sjudson_app_uswgi.sock;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接