Skip to main content

Deploy Django App with NGINX

After the multiple issues that arose from having Django as an App service talking to the Azure VM (containing PostgreSQL) database, which would then talk to the Static Web App. The entire configuration was dropped.

  • The following tutorial was followed to setup Django with NGINX on the VM.
  • Following the steps in the tutorial the deployment was successful.

Serving static files via NGINX

To serve static files via NGINX, you need to specify the path to the static files in the NGINX conf.

  1. To do so, you first need to run python manage.py collectstatic in the correct venv.
  2. Open NGINX conf and add,
     location /static/ {
    <!-- root <path-to-the-static folder> -->
    root /home/gqc/riverflows_django/riverflows/;
    }
  3. In this case, the riverflows_django/riverflows/ contains the static folder.

Handling CORS via NGINX

  1. Once the app is hosted through NGINX, CORS headers are no longer handled by Django, but are handled by NGINX.
  2. Adding the following line in the NGINX conf allows CORS requests.
    add_header Access-Control-Allow-Origin * always;

Additional steps

  1. Adding certificate to the VM is documented here
  2. Mounting additional storage on the VM here
  3. Moving PSQL databases to different drives here