Directly using Nginx as a server without Uvicorn
See original GitHub issueHi, as I understand it, I can implement fastapi application with (or at least I know such options):
- only Uvicorn
- Uvicorn behind Nginx
- Uvicorn behind Gunicorn
- (Uvicorn behind something else?)
I found out that one can use Nginx unit directly with FastAPI as described here in nginx unit docs. It works as expected. It seems that using only Nginx is slightly simpler for me (maybe somehow faster or slower too? why?) than Uvicorn + something, but I haven’t found anything in docs/issues about using it directly and using Uvicorn alone isn’t recommended in some cases (as far as i know), hence the questions.
The questions are
- what are the possible drawbacks to using this approach / why should I avoid using Nginx directly
- if there are no contraindications, what additional steps should I eventually take to make it work well
- what are the main differences between these solutions (Uvicorn vs Uvicorn + something vs Nginx only)
To provide any context on how I exactly implemented it with Nginx only, let’s say in our application’s root folder we have config.json
in config
folder and hello world fastapi app in app/main.py
, then it might look like this:
Dockerfile
FROM nginx/unit:1.23.0-python3.9
COPY ./config/config.json /docker-entrypoint.d/config.json
RUN mkdir build
COPY . ./build
RUN apt update && apt install -y python3-pip \
&& pip3 install -r /build/requirements.txt \
&& apt remove -y python3-pip \
&& apt autoremove --purge -y \
&& rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/*.list
EXPOSE 80
config.json
{
"listeners": {
"*:80": {
"pass": "applications/fastapi"
}
},
"applications": {
"fastapi": {
"type": "python 3.9",
"path": "/build/app/",
"module": "main",
"callable": "app"
}
}
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:18 (7 by maintainers)
Top GitHub Comments
I wasn’t aware of Nginx Unit, that’s interesting!
First of all, let’s clarify things a bit:
Now, for “Nginx Unit”. From what I understand, it’s an Nginx module implementing its own version of ASGI; so it comes “instead of” Uvicorn, it’s not just Nginx on its own. Actually, it seems that you can even configure Nginx (the full HTTP server) in front of Nginx Unit (the app server): https://unit.nginx.org/howto/integration/
I see that they have an implementation of ASGI in pure C. I would say that it should work quite well, after all, Nginx has proven to be very efficient for years, so I guess the people behind know their stuff.
The difference could be in terms of performance; and the only way to find out which one is better is to perform benchmarks. I didn’t find existing for ASGI, that could be interesting to do.
I have tested nginx unit using wrk. It’s around 20% faster than uvicorn with a real application that queries sqlite, and 40% faster than hypercorn under the same condition. Due to it’s high throughput, it may be the future of asgi server. As for http2, uvicorn does not support it either. Hypercorn do. So if you need extreme fast servers you can try nginx unit, but if you want more asgi’s feature you may try hypercorn