【问题标题】:How do I prevent this bad gateway error using nginx, uwsgi, and flask?如何使用 nginx、uwsgi 和烧瓶防止出现此错误网关错误?
【发布时间】:2014-09-06 10:30:30
【问题描述】:

当我运行uwsgi --ini /root/code/poetree/uwsgi.ini 时,我在下面的设置中不断收到错误的网关 502 错误。但是,正如您在最后的日志中看到的那样,应用程序在工作人员启动时正常启动(如 ps aux | grep uwsgi 所示)。另一项观察是,下面的服务器块(在 /etc/nginx/sites-available/conf 中)在 /audio 中获取静态数据时工作正常,但在尝试加载任何动态页面时出现网关错误。

如果我改用app.flask_app.run(host='0.0.0.0', port=8080),那么我可以看到页面没有问题,所以这告诉我它与uwsgi / nginx有关。

uwsgi.ini

[uwsgi]
base = /root/code/poetree
app = app
module = %(app)
callable = flask_app
pythonpath = /usr/local/lib/python2.7/dist-packages
socket = %(base)/%n.sock                                                                                                                                                         
master = true
processes = 5
chmod-socket    = 666
logto = /var/log/uwsgi/%n.log

/etc/nginx/sites-available/conf

server {
        listen 80;

        root /;

        server_name <IP_ADDRESS>

        location /audio/ {
                root /data;
                try_files $uri $uri/ =404;
        }
        location / {
                try_files $uri @poetree;
        }
        location @poetree {
                 include uwsgi_params;
                 uwsgi_pass unix:/root/code/poetree/uwsgi.sock;
        }
}

/var/log/uwsgi/uwsgi.log:

*** Starting uWSGI 2.0.6 (64bit) on [Wed Jul 16 11:48:13 2014] ***
compiled with version: 4.8.2 on 16 July 2014 11:38:51
os: Linux-3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014
nodename: cinjon
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /root/code/poetree
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
your processes number limit is 3751
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 /root/code/poetree/uwsgi.sock fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x11af010
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 436608 bytes (426 KB) for 5 cores
*** Operational MODE: preforking ***
added /usr/local/lib/python2.7/dist-packages/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x11af010 pid: 580 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 580)
spawned uWSGI worker 1 (pid: 583, cores: 1)
spawned uWSGI worker 2 (pid: 584, cores: 1)
spawned uWSGI worker 3 (pid: 585, cores: 1)
spawned uWSGI worker 4 (pid: 586, cores: 1)
spawned uWSGI worker 5 (pid: 587, cores: 1)

【问题讨论】:

  • nginx 日志中的任何内容?

标签: python nginx flask uwsgi


【解决方案1】:

我假设这些行解释了问题,如果我在某个地方错了,请纠正我

!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

要解决 uwsgi 中的 pcre 问题,请参阅此 thread 可能会对您有所帮助

另外,最好运行uwsgi,以防止在root而不是当前用户下运行。

编辑2:

我建议您尝试使用反向代理传递而不是 uwsgi params

location / {
        proxy_pass  http://127.0.0.1:5000/;
        proxy_set_header    Host    $host;
        proxy_set_header    X-Real-IP   $remote_addr;
    }

更改端口号

【讨论】:

  • 感谢 Nava,我在线程中进行了指定的更改,并且该警告不再出现。但是,同样的错误正在发生。我已经更新了现在显示的调试日志。
  • 已编辑问题..您可以尝试反向代理传递,只需像往常一样在screen 中运行您的应用程序python app.py 并尝试转发请求,这将有助于发现错误
  • 通常你不需要内部路由,所以 pcre 不是问题。
猜你喜欢
  • 2013-05-14
  • 2019-09-29
  • 1970-01-01
  • 2017-11-12
  • 2017-11-24
  • 2012-11-28
  • 2015-04-30
  • 1970-01-01
  • 2015-05-08
相关资源
最近更新 更多