Nginx for FastAPI — Minimalistic

Milind Deore
1 min readSep 4, 2023

This represents the streamlined setup for quick initiation, particularly when you’re involved in crafting a Docker image and require external traffic to access your host machine, which can then be routed to one of the applications running on it or an application within a Docker container.

Sometimes, you require a fundamental setup to get started swiftly, allowing you to focus on more critical tasks. This minimalistic configuration doesn’t encompass all security or modularity aspects.

$ sudo apt-get install nginx

$ sudo vim /etc/nginx/sites-available/fastapi-app

server {
listen 80;
server_name 13.233.118.98;
location / {
proxy_pass http://127.0.0.1:8000;
}
}

$ sudo ln -s /etc/nginx/sites-available/fastapi-app /etc/nginx/sites-enabled/

$ sudo nginx -t (check configuration is correct)

$ sudo systemctl restart nginx

Now, you can divert the external traffic to your application running either on the host or inside docker container. Please make the relative changes based like port number, application name, path, etc.

Install Certbot

$ sudo apt install certbot
$ sudo apt install python3-certbot-nginx

Add the ‘A’ record with the domain registrar, once the DNS is propogated properly, run the following command:

$ sudo certbot --nginx -d abcwebapp.com 

--

--