Deploying FastAPI Application Part 3

Remember, in the previous tutorial, we configured the supervisor and installed nginx with:

sudo apt install nginx

In this one, we are going to use nginx to serve our API. It is a reverse proxy, load balancer, and much more. However, all we need to understand for now is that it acts as a physical router at our home. The way the router is the internet request entrypoint at our home. In a similar way, nginx will sniff for all HTTP requests and let them enter the correct process.

For nginx to work, We need to create a file in /etc/nginx/sites-available

johnwick@fastapixcelery:/etc/nginx/sites-available$ sudo touch fastapi_app
[sudo] password for johnwick: 

johnwick@fastapixcelery:/etc/nginx/sites-available$ sudo nano fastapi_app

and put the below configuration to this file.

upstream gunicorn_server {
    server unix:/home/johnwick/project/infra/run/gunicorn.sock fail_timeout=0;
}


server {
    listen 80;
    server_name ip_or_domain;  #put your server ip/domain

    access_log /home/johnwick/project/logs/nginx-access.log;
    error_log /home/johnwick/project/logs/nginx-error.log;

    location / {
        include proxy_params;
        proxy_pass http://gunicorn_server;
    }

}

Once, this configration file is saved, check once for any syntax errors by executing

johnwick@fastapixcelery:/etc/nginx/sites-available$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


johnwick@fastapixcelery:/etc/nginx/sites-available$ sudo ln -s /etc/nginx/sites-available/fastapi_app /etc/nginx/sites-enabled/

Finally, we create a soft link and put the same configuration in sites-enabled.

One final thing, nginx process by default uses www-data. Since johnwick is the owner of the gunicorn sock file, www-data will not be able to connect nginx with gunicorn, to fix it, open the nginx.conf file and change the first line containing user www-data; to user johnwick;

johnwick@fastapixcelery:/etc/nginx/sites-available$ cd ..
johnwick@fastapixcelery:/etc/nginx$ sudo nano nginx.conf 
#change first line to user johnwick;


Now, all you need to do is to restart nginx and allow it to sniff for http and https requests via the firewall.

johnwick@fastapixcelery:~/project/logs$ sudo ufw delete allow 8000
Rule deleted
Rule deleted (v6)
johnwick@fastapixcelery:~/project/logs$ sudo ufw allow 'Nginx Full'
Rule added
Rule added (v6)

Hurray, it was a long-running process!! If you faced issues with it, try to find the logs and understand what the core issue is. Now, we can visit our ip/domain name and see the ping pong, FINALLY!!

If you want https enables for your site read this wonderful small doc: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04
Oops, we haven't still configured celery, beat. See you in the next tutorial

FastAPITutorial

Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.

We know, we might make it hard for you but definitely worth the efforts.

Contacts

Refunds:

Refund Policy
Social

Follow us on our social media channels to stay updated.

© Copyright 2022-23 Team FastAPITutorial