Can uWSGI be a web-server and application server at the same time?
For example, stand-alone WSGI Containers https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/ But again, it recommends to use an HTTP server. Why? Can't uWSGI handle HTTP requests?
I have read different articles about deploying a Flask application. They say, I'd need uWSGI and nginx - one popular option.
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
https://flask.palletsprojects.com/en/1.1.x/deploying/uwsgi/#uwsgi
My Flask application. 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 config 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)
requirements.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
And it seems to be working. I am running all this in a virtual machine in docker-compose.
My question, why do I need nginx here? Do python developers use uWSGI without a web server?
I am not going to run dev default WSGI server in production, as it is asked here Are a WSGI server and HTTP server required to serve a Flask app?
WSGI servers happen to have HTTP servers but they will not be as good as a dedicated production HTTP server (Nginx, Apache, etc.)
from https://stackoverflow.com/a/38982989/1839360
What I am asking is why uWSGI server cannot be as good for HTTP handling, so I need to put HTTP server between the internet and uWSGI there. Why incoming HTTP requests can go directly to uWSGI (it is not running in the development or debug mode).
For running flask, you do not need nginx, just a webserver of your choice, but life with nginx is just easier. If you are using Apache, you want to consider to use a WSGI.
I remember reading somewhere in the Flask documentation what is stated by an answer to "Are a WSGI server and HTTP server required to serve a Flask app?" as
The answer is similar for "should I use a web server". WSGI servers happen to have HTTP servers but they will not be as good as a dedicated production HTTP server (Nginx, Apache, etc.).
The main idea behind is the architectural principle of splitting layers to ease debugging and increase security, similarly to the concept that you split content and structure (HTML & CSS, UI vs. API):
I have seen clients only running a WSGI server alone, with integrated HTTP support. Using an additional webserver and/ or proxy is just good practice, but IMHO not strictly necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With