折腾:
[已解决]Flask中用gunicorn和Nginx出错:502 Bad Gateway
期间,把配置文件改为:
(SIPEvents) ➜ conf.d cat default.conf server { listen 80; server_name 115.29.173.126; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { proxy_pass http://127.0.0.1:8080/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } (SIPEvents) ➜ conf.d pwd /etc/nginx/conf.d |
重启nginx。然后再去:
(SIPEvents) ➜ SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 app:hello [2016-08-18 17:24:08 +0000] [20442] [INFO] Starting gunicorn 19.6.0 [2016-08-18 17:24:08 +0000] [20442] [INFO] Listening at: http://127.0.0.1:8080 (20442) [2016-08-18 17:24:08 +0000] [20442] [INFO] Using worker: sync [2016-08-18 17:24:08 +0000] [20447] [INFO] Booting worker with pid: 20447 [2016-08-18 17:24:08 +0000] [20448] [INFO] Booting worker with pid: 20448 [2016-08-18 17:24:09 +0000] [20453] [INFO] Booting worker with pid: 20453 [2016-08-18 17:24:09 +0000] [20454] [INFO] Booting worker with pid: 20454 |
终于,至少,有变化了:
Internal Server Error
然后log输出:
[2016-08-18 17:24:16 +0000] [20447] [ERROR] Error handling request / Traceback (most recent call last): File "/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 135, in handle self.handle_request(listener, req, client, addr) File "/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request respiter = self.wsgi(environ, resp.start_response) TypeError: hello() takes no arguments (2 given) |
flask gunicorn nginx Internal Server Error
flask gunicorn nginx TypeError: hello() takes no arguments (2 given)
python – Flask + uwsgi + nginx: TypeError: … takes no arguments (2 given) – Stack Overflow
(SIPEvents) ➜ SIPEvents cat app.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello Flask and uWSGI!" if __name__ == "__main__": app.run(host=’0.0.0.0′, port=8080) |
去把app.run中的参数:
app.run(host=’0.0.0.0′, port=8080)
去掉,换成:
app.run()
(SIPEvents) ➜ SIPEvents cat app.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello Flask and uWSGI!" if __name__ == "__main__": app.run() |
再去运行试试
结果错误依旧。
TypeError: session() takes no arguments (2 given) · Issue #172 · Diaoul/subliminal
gunicorn TypeError: hello() takes no arguments (2 given)
python – Gunicorn Internal Server Error – Stack Overflow
去换成:
gunicorn -w 4 -b 127.0.0.1:8080 app:app
(SIPEvents) ➜ SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 app:app [2016-08-18 17:37:36 +0000] [20635] [INFO] Starting gunicorn 19.6.0 [2016-08-18 17:37:36 +0000] [20635] [INFO] Listening at: http://127.0.0.1:8080 (20635) [2016-08-18 17:37:36 +0000] [20635] [INFO] Using worker: sync [2016-08-18 17:37:36 +0000] [20640] [INFO] Booting worker with pid: 20640 [2016-08-18 17:37:36 +0000] [20641] [INFO] Booting worker with pid: 20641 [2016-08-18 17:37:36 +0000] [20642] [INFO] Booting worker with pid: 20642 [2016-08-18 17:37:37 +0000] [20643] [INFO] Booting worker with pid: 20643 |
然后终于工作了:
[总结]
此处的:
不论是uwsgi还是gunicorn,中的module和callable,对于这种代码:
(SIPEvents) ➜ SIPEvents cat app.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello Python Flask gunicorn !" if __name__ == "__main__": app.run() |
即Flask的程序的名字是:(文件名app.py中的文件名)app
而程序内部的,可以被调用的==callable,是:app
对应着:
app = Flask(__name__) |
-》所生成的Flask的实例的名字
可以理解为:
gunicorn Flask中py的文件名:py文件内部的Flask的实例的名字
即可正常调用。
此时,先去找:
app.py
然后再找到
app.py中的app,这个Flask的实例
针对于
/
这个路由,调用对应的hello去输出对应的内容:
Hello Python Flask gunicorn !
转载请注明:在路上 » [已解决]Flask中用gunicorn和Nginx出错:Internal Server Error,TypeError: hello() takes no arguments (2 given)