Setting up Django for Deployment: Nginx
Tags: django, gunicorn, python, nginx
Published: 2021-06-22 05:19:11

---
title: Setting up Django for Deployment: Nginx
published: true
date: 2021-06-21 08:46:00 UTC
tags: Django, Gunicorn, Python, Nginx
canonical_url: https://vuyisile.com/setting-up-django-for-deployment-nginx/
---

In this post, you will see how to setup nginx as a reverse proxy to gunicorn and how to use it to serve up static files in Django. In the [previous post](https://dev.to/vndlovu/setting-up-django-for-deployment-gunicorn-3adh), I showed how to install and setup the gunicorn WSGI server. This time we’ll focus on nginx.

Nginx is a web server and reverse proxy that accepts requests from clients and decides how to serve them. In the case of Django, we’ll configure it to handle the Django static files and then proxy or pass requests for dynamic content to the gunicorn server.

## Install Nginx

There are a [couple of ways to install Nginx](https://dev.to/vndlovu/installing-nginx-on-linux-3cie-temp-slug-5941966). The easiest method is to install it using a package manager. In Ubuntu, this looks like this:  
  
`sudo apt-get install nginx`

Once installed, nginx _should_ automatically start and create a system service. If it doesn’t auto start, you can start it by running:  
  
`sudo service nginx start`

## Setup Static Content

When you’re developing Django locally using the development server, static files are automatically served for you. Once you start using gunicorn, things change. Gunicorn knows nothing about handling static files, so we need to tell Django where to put them and Nginx where to find them. You can see this behavior if you run your project using gunicorn and attempt to view a page with static content such as the Django admin.

Start the gunicorn server with:

`gunicorn myproject.wsgi`

Navigate to [http://localhost:8000/admin](http://localhost:8000/admin) and you’ll notice that the page looks broken:

![](https://i1.wp.com/vuyisile.com/wp-content/uploads/2021/06/django_admin_no_css-1.png?resize=462%2C281&ssl=1)

This is because gunicorn failed to serve the css and js files needed to make the page look pretty. To fix this problem we have to do two things:

1. Configure a static files directory in Django
2. Configure Nginx to serve files from this directory

Nginx can serve up pretty much any form of static content you point it to. The first step in serving up the static files is to add `STATIC_ROOT` path to your project’s `settings.py` file:

`STATIC_URL = '/static/'STATIC_ROOT =  "/home/vndlovu/django/static/"`

The `STATIC_URL` is the URL Django will point to for all static files e.g `http://example.com/static/`. Static files include html, css, images, and Javascript files. The `STATIC_ROOT` line is a path to any folder where your project’s static files will be stored.

Once you have configured static files, run the `collectstatic` command to generate the default static files required by Django. If your project has any additional static files, place them in the folder you specified above.

`python manage.py collectstatic`

Type “yes” at the prompt that appears and Django will copy or generate all the static files you need to your `STATIC_ROOT` folder.

## Configure Nginx

Create a configuration file for nginx. The location of this file depends on how you installed Nginx and what OS you’re on. A common location for nginx configs in Ubuntu Linux is `/etc/nginx/sites-available/` If this directory doesn’t exist in your operating system, please check for the appropriate location for your OS in the [nginx documentation](https://nginx.org/en/docs/). You can name the config file anything you like. For this example I will call it `django`. Use your favourite text editor to create the configuration file at `/etc/nginx/sites-available/django` and add the following configuration:

```

server { 

        listen 80;
        server_name localhost;

         location /static/ {
             root /home/vndlovu/learning/django/;
         }

        location / {
            proxy_pass http://localhost:8000;
        }
}

```

This configuration is a **very basic** configuration that tells Nginx to listen on port 80, the default HTTP port for all requests coming to the server specified in `server_name`. You can use a domain name or server IP address here. The next line is a location block that tells Nginx where to find static files whenever the `/static/` URL is hit. Notice that the location to the static file directory here is the same as the one specified in `settings.py`. This is very important.

The next location block will handle all requests to the root URL or in this case [http://localhost/](http://localhost/) . Nginx cannot handle dynamic WSGI content that will be generated by Python so in this block we tell it to proxy or pass the request to [http://localhost:8000](http://localhost:8000) where gunicorn is listening. In essence we’re telling Nginx not to handle dynamic requests but only handle static files and let gunicorn handle everything else. Save this file.

Next, activate the configuration by creating a symbolic link to this config in `/etc/nginx/sites-enabled`/. To do this, navigate to `/etc/nginx/sites-enabled` and create the symlink there:

`cd /etc/nginx/sites-enabled/sudo ln -s /etc/nginx/sites-available/django`

The command above will create a shortcut or link to your configuration into the `/etc/nginx/sites-enabled/` folder where nginx checks for what sites to enable. Reload Nginx to effect these changes:  
  
`sudo service nginx reload`

If the configuration is correct and there are no errors, Nginx should restart silently in the background.

## Test

Test that Nginx can serve your Django website by navigating to [http://localhost/](http://localhost/). Notice that I didn’t add the 8000 port. That is because we configured Nginx to listen on port 80 and to pass any Django requests to port 8000. If everything went well, you should see the familiar Django Admin login screen:

![The Django Admin screen](https://i2.wp.com/vuyisile.com/wp-content/uploads/2021/06/nginx_working.png?resize=632.4%2C391&ssl=1)

## Conclusion

In this post, you saw how to install and setup the Nginx web server to serve Django static files and act as a reverse proxy to gunicorn. These two web servers work very well together and can be used for production deployments. In a future article I will show you how to run Django in a Docker container.