不使用nginx运行Flask应用程序

问题描述 投票:0回答:1

在没有nginx或其他web服务器的情况下运行Flask应用?

uWSGI可以同时作为web服务器和应用服务器吗?

比如说 独立的WSGI容器https:/flask.palletsprojects.comen1.1.xdeployingwsgi-standalone。但它又建议使用HTTP服务器。为什么呢?uWSGI不能处理HTTP请求吗?

我读过不同的关于部署Flask应用的文章,他们说,我需要uWSGI和nginx--一个流行的选择。他们说,我需要uWSGI和nginx--一个流行的选择。

https:/www.digitalocean.comcommunitytutorialshow-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

https:/uwsgi-docs.readthedocs.ioenlatesttutorialsDjango_and_nginx.html。

https:/flask.palletsprojects.comen1.1.xdeployinguwsgi#uwsgi。

我的Flask应用程序。app_service.py

import json
import os

from flask import Flask, Response, redirect


portToUse = 9401


@app.route("/app/people")
def get_service_people():
    print("Get people")
    people_str = "{ \"John\", \"Alex\" }"
    return Response(people_str, mimetype="application/json;charset=UTF-8")


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=portToUse)

uwsgi配置 uwsgi.ini

[uwsgi]
chdir = $(APPDIR)
wsgi-file = app_service.py
callable = app
uid = psc-user
gid = psc-user
master = true

processes = 1
threads = 1
http-timeout = 300
socket-timeout = 300
harakiri = 300

http = 0.0.0.0:9401
socket = /tmp/uwsgi.socket
chmod-sock = 664
vacuum = true
die-on-term = true

; Images serving: https://github.com/unbit/uwsgi/issues/1126#issuecomment-166687767
wsgi-disable-file-wrapper = true

log-date = %%Y-%%m-%%d %%H:%%M:%%S
logformat-strftime = true
logformat = %(ftime) | uWSGI    | %(addr) (%(proto) %(status)) | %(method) %(uri) | %(pid):%(wid) | Returned %(size) bytes in %(msecs) ms to %(uagent)

要求.txt

# Web framework for python app.
Flask==1.1.1

# JWT tocket utils to retrieve the tocken from HTTP request header.
# It is used for retrieving optional permissions from gateway.
# https://pypi.org/project/PyJWT/
PyJWT==1.7.1

# Eureka API client library to implement service discovery pattern
py_eureka_client==0.7.4

# Python application server
uWSGI==2.0.18

而且它似乎在工作。我在docker-compose的虚拟机中运行这一切。

我的问题是 为什么我需要nginx?? python开发者在没有web服务器的情况下使用uWSGI吗?

更新

我不打算在生产中运行开发的默认WSGI服务器,因为它在这里问道服务Flask应用是否需要WSGI服务器和HTTP服务器?

WSGI服务器恰好有HTTP服务器,但它们不会像专门的生产型HTTP服务器(Nginx、Apache等)那样好用。

https:/stackoverflow.coma389829891839360。

为什么会这样呢?

我想问的是,为什么uWSGI服务器不能很好的处理HTTP,所以我需要把HTTP服务器放在互联网和uWSGI之间。为什么输入的HTTP请求可以直接进入uWSGI(不是在开发或调试模式下运行)。

python-3.x nginx flask uwsgi
1个回答
1
投票

对于运行flask,你不需要nginx,只需要一个你选择的webserver,但使用nginx的生活只是更容易。如果你使用的是Apache,你可以考虑使用一个名为 WSGI.

我记得在Flask文档中的某个地方读到过一个叫 回答 "服务Flask应用是否需要WSGI服务器和HTTP服务器?" 作为

对于 "是否应该使用Web服务器",答案也是类似的。WSGI服务器恰好有HTTP服务器,但它们不会像专用的生产型HTTP服务器(Nginx、Apache等)那样好。

背后的主要思想是 分层建筑原理 以方便调试和增加安全性,类似于你把内容和结构(HTML & CSS,UI与API)分开的概念。

更新

我见过的客户只单独运行一个WSGI服务器,并集成HTTP支持。使用额外的Web服务器和/或代理只是一个很好的做法,但IMHO并不是严格意义上的必要。

参考文献

© www.soinside.com 2019 - 2024. All rights reserved.