python - NGINX、uWSGI 和 Flask 未运行子进程

标签 python nginx flask uwsgi

我的 NGINX 配置:

server {
  server_name 127.0.0.1;
  listen 4450;
  location ~* ^/.*$ {
  include uwsgi_params;
    uwsgi_pass unix:/tmp/esrvadmin.sock;
  }
}

uWSGI启动:

uwsgi --uid root -s /tmp/esrvadmin.sock --chown-socket nobody:root \
  --file /var/www/sitios/manten/srv.py \
  --processes 2 --callable app --pidfile /var/run/edesarrollos/esrvadmin.pid

带有 Flask 代码的 Python:

import os, subprocess
from flask import Flask, abort, request
app = Flask(__name__)

DETO_DIR = '/var/www/sitios/manten/detos'

@app.route('/detonate')
def index():
  #return str(subprocess.check_output(['ls','-l']))
  token = request.args.get('token','')
  if token != '':
     # Si no existe el directorio de detonatores, se crea
     if not os.path.exists(DETO_DIR):
       os.mkdir(DETO_DIR)
     if os.path.isfile(DETO_DIR+"/"+token):
       try:
         os.system(open(DETO_DIR+"/"+token).read())
       except Exception as ex:
         return str(ex)
       return "Reiniciado Correctamente"
     else:
       abort(404) 
  else:
    abort(404)

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0')

ma​​in 模式下,我可以在 os.system("something args") 中运行任何内容并接收其 HTTP 响应,即使使用“service Something start”也能正确完成。我也尝试过使用子进程函数,但没有成功。

但是当我从 NGINX-uWSGI 运行时,我收到一个永远等待的 http 请求,当然永远不会响应,但它完成了工作(启动服务)。

我发现的日志中最好的错误描述:

2013/01/28 03:38:24 [error] 3978#0: *3 upstream timed out (110: Connection timed out) 
while reading response header from upstream, client: 
127.0.0.1, server: 127.0.0.1, request: "GET
/detonate?token=start-    
a81260812b643d8672ccf9570033109f200595779e0a352e630a75760328e2d375025ef349e
3d599f368092abb63511f-23611e81194b04d5d0a6d0f02baf7fb9
HTTP/1.1", upstream: "uwsgi://unix:/tmp/esrvadmin.sock:", host: "127.0.0.1:4450"

知道会发生什么吗?

最佳答案

请阅读此处:http://uwsgi-docs.readthedocs.org/en/latest/ThingsToKnow.html (--close-on-exec 部分)请不要以 root 身份运行 uWSGI!

关于python - NGINX、uWSGI 和 Flask 未运行子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14560010/

相关文章:

python - 如何导出点云以在Matlab中查看

python - 将 NaN 和字符串列表转换为 int?

linux - apache 错误 - 403 禁止在虚拟主机上通过 nginx 反向代理

python - 生成图像 -> 嵌入带有数据 uri 的 flask 中

python - SQLAlchemy 与 Flask 不提交

python - 在python中打开txt文件中的链接

python - 如何在 python 脚本中显示 MySql 数据?

nginx - 有什么方法可以让您的 nginx 代理不转发 HTTP -> HTTPS 仅针对特定 URL?

docker - 如何将位置映射到 Nginx 中的上游服务器?

python - flask.cli.NoAppException : Failed to find Flask application or factory in module "flaskr". 使用“FLASK_APP=flaskr:name 指定一个