Proxying with django and gunicorn - python

I have a django application that i serve using gunicorn. I do that by using the method prescribed on the gunicorn site - embedding gunicorn into my django application.
I'm trying to set up a proxy into my application so that when you go to "http://mysite.com/proxy/" it does proxy you to "http://mysite.com:8100".
I know i can do that with apache and other webservers. For some reasons i would prefer to do it directly with gunicorn/django. One of theses reasons is keeping everything in the same place.
My question is, what is the best way to do that ? Also is it a terrible idea altogether ?
Thanks.

You should deploy some proxy application into your gunicorn installation, such as WSGIProxy.

I've written dj-revproxy for easy integration of a proxy in django. Bonus point it's using restkit which use the gunicorn HTTP engine. (I'm one of the gunicorn authors). More info here:
https://github.com/benoitc/dj-revproxy

Related

How can I deploy FastAPI manually on a Ubuntu Server?

I have a very simple API (2 routes) which just has GET requests, and doesnt need any authentication or anything for now.
I want to know what is the best and appropariate way to deploy my API for production. I am unable to use docker, and would like to do it the server way.
So i have a few questions:
On the fastapi documentation it says you can do uvicorn main:app --host 0.0.0.0 --port 80 but i was thinking if that is the correct way for production? Do i just enter that command, and will the API automatically start listening on the servers IP address? Also is this method efficient and will it be able to handle all the requests? Or what would i change for it to be faster?
When should i use a process manager?
When should i use multiple workers? And what benefits do they provide?
When should i use Gunicorn as mentioned here? https://www.uvicorn.org/deployment/#gunicorn
I am just a little confused on how to deploy this because one article says do this, another says do this.
If for whatever reasons you don't like to use Docker-Ce, the best way is to create a systemd-service unit for your application so every time it goes down, systemd will try to restart it, then run it with servers like wgsi or gunicorn.
This link can help about systemd-services too:
https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd
P.S note that the way you serve gunicorn isn't really related to docker or systemd-service, for both approaches you need to config gunicorn.
To answer your Question:
How can I deploy FastAPI manually on a Ubuntu Server?
You can check out this video tutorial on how to
Deploy FastAPI on Ubuntu
The deployment has the following architecture within a single Ubuntu VM.
As you take a look at the Architectural diagram above for FastAPI Deployment, it shows a single VM deployment.
Within the Ubuntu VM, there are two systemd services namely caddy.service and gunicorn.service up and running. The gunicorn.service runs the FastAPI application and the caddy.service exposes the FastAPI application running on Gunicorn as a reverse proxy with the help of uvicorn.workers.UvicornWorker worker class. In addition to this, our FastAPI communicates to PostgreSQL database server in an asynchronous fashion with the help of databases package that provides simple asyncio support for PostgreSQL database.

Flask using Nginx?

I am a .net developer coming over to python. I have recently started using Flask and have some quick questions about serving files.
I noticed a lot of tutorials focused on nginix and flask. However, I am able to run flask without nginx. I'm just curious as to why this is used together (nginx and flask). Is nginx only for static files?
Nginx is a proxy server, imagine your apps have multiples microservices on differents languagues.
For more info NGINX REVERSE PROXY
On a development machine flask can be run without a webserver (nginx, apache etc) or an application container (eg uwsgi, gunicorn etc).
Things are different when you want to handle the load on a production server. For starters python is relatively very slow when it comes to serving static content where as apache / nginx do that very well.
When the application becomes big enough to be broken into multiple separate services or has to be horizontally scaled, the proxy server capabilities of nginx come in very handy.
In the architectures I build, nginx serves as the entry point where ssl is terminates and the rest of the application is behind a VPN and firewall.
Does this help?
From http://flask.pocoo.org/docs/1.0/deploying/ :
"While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well. Some of the options available for properly running Flask in production are documented here."

The WSGI module of Nginx is out of date?

I love python and I want to use python to construct my blog. I have deployed a nginx server and I find that in python wiki:
In addition to the above, some non-Python-based Web servers support
Python-based applications by embedding the Python virtual machine for
improved performance:
Nginx WSGI support module for Nginx HTTP server
I goto Nginx WSGI wiki page, but find:
mod_wsgi tested with Nginx 0.8.31 and python-2.6.2
My nginx version is 1.4.4(deployed one year before)...
I find a update information page in that wiki page, I click it hopefully:
Browser gives DNS look up error:ERR_NAME_NOT_RESOLVED
So, my question is:
mod_wsgi is out of date and is not good choice for python server?
If I want to use nginx as my http server, what is a better choice?
If I don't insist to use nginx, what is my best choice?
As I mentioned in my comment, the preferred way to deploy Python applications with nginx is to use it as a reverse proxy to a standalone Python WSGI server such as gunicorn or uwsgi.
The documentation for both projects includes sample configuration for running with nginx.

difference between uwsgi module in nginx and uwsgi server

I'm new to linux development. I'm a bit confused on the documentation i read.
My ultimate goal is to host a simple python backed web service that would examine an incoming payload, and forward it to other server. This should be less than 30 lines of code in python.
I'm planning to use nginx to serve up python file. From my research, i also need a python web framework. I chose to go with uwsgi. I'm so confused. which one do I need? an nginx uwsgi module, or uwsgi server? i don't want to put django just for this simple purpose.
the nginx documentation mention that
Do not confuse the uwsgi protocol with the uWSGI server (that speaks the uwsgi protocol)
So, does that mean, i don't need to install uwsgi server separately? do i just install nginx, and start configuring? I'm using nginx 1.4.4
Could someone share a step by step configuration procedure on how to configure uwsgi with nginx, along with a sample python code(hello world maybe)? i can configure nginx just fine, but i don't know how to make it serve python pages. all the docs i could find involves having django on top.
You're mixing up things, so let me clarify.
Python's standard way of publishing applications via web servers is WSGI--you can think of it as a Python's native CGI. uWSGI is a WSGI-compliant server that uses uwsgi protocol to talk to other uWSGI instances or upstream servers. Usually the upstream server is nginx with HttpUwsgiModule that allows it to communicate using uwsgi protocol--with nginx you have additional layer of protection for your app server, load balancing and serving the static files. In most scenarios, You Should Be Using Nginx + UWSGI. To answer your question, uWSGI is installed and run separately from nginx, and they both need to be configured to communicate to each other.
Pure WSGI is pretty low-level, so you may want to use a WSGI-compliant framework. I guess the top two are Django and Flask.
For a hello world Flask setup, Serving Flask With Nginx seems to be a good article.

Deploying Django using fcgi causes slow page loading

I need to deploy my django app on a shared server, where I don't have root access (e.g. httpd.conf) andn all I have is the folder public_html.
Now, I followed the sites describing using fcgi to deploy django (e.g. this). However, the pages load very slowly, I guess the reason is that django needs to be reloaded upon every request? Essentially, I would like a server that runs permanently and simply gets requests from apache.
Before trying out the solution with sockets and so on, I would like to ask for some professional opinions.
thanks!
FCGI sucks. Even its author admits it.
Using Apache ? Try mod_wsgi. It's the most professional deployment solution for apache.
Using Nginx or something else ? Then consider uWSGI. or gunicorn.
Link to a benchmark.

Categories