7

I am using Whitenoise to serve static files in my Django app. I am NOT using Nginx. I plan to use Whitenoise behind a CDN like Cloudfront in the future. See Whitenoise FAQs.

I have been looking for deployment instructions, which handle these questions:

  1. Since I am not using nginx, I plan to bind gunicorn directly to port 80. This results in an error - Permission Denied. I can run gunicorn as root, but that seems like a bad approach.

  2. How to handle SSL certificate stuff? Typically this is handled by servers like Nginx.

EDIT: I am deploying my application on a Ubuntu 18.04 VM on Google Cloud Compute Engine.

P.S.: Mine is not going to be a very high traffic website. Others have used this configuration to serve websites with a high traffic. See this Squeezing every drop of performance out of a Django app on Heroku.

GunnerFan
  • 3,576
  • 3
  • 25
  • 38
  • 1
    You haven't explained why you aren't using nginx. Just make it easy for yourself and use it. It's not hard to set up. What's your reason for this? (Note, your linked question is about Heroku, which has its own proxy layer in the front end, and is therefore not at all relevant.) – Daniel Roseman May 01 '19 at 10:11
  • 1
    @DanielRoseman Can you provide me with a link to docs about Heroku's proxy layer? My reasons for using Whitenoise - I have posted a link to the Whitenoise FAQs in the OP. It automatically sets caching headers, enables gzip compression. When used with a CDN, the majority of static file requests will never hit the server. – GunnerFan May 01 '19 at 10:41
  • 1
    Using Whitenoise doesn't mean not using nginx. You can happily use them both in conjunction. nginx's value in this is as a reverse proxy, offloading static files to Whitenoise doesn't make that need go away. And Heroku don't really give much detail of their proxy. – Daniel Roseman May 01 '19 at 10:47
  • Makes sense. I think I will go with this approach.There are many tutorials about deploying with Whitenoise on Heroku. But none for deploying on a VM. Using nginx will make my life easier. – GunnerFan May 01 '19 at 10:51

1 Answers1

9

TL;DR

I used nginx as the http server. I removed the configuration associated with static files in nginx, so the static file requests are passed to the wsgi layer (gunicorn) and are handled by Whitenoise. So you can follow any 'nginx + gunicorn + django' deployment instructions/tutorials, which are easily available with a simple google search.

This post cleared it up for me: Deploying your Django static files to AWS (Part 2).

Long Answer

As mentioned before there are many tutorials about deploying Django + Whitenoise applications on Heroku. As pointed out in the comments:

Heroku, which has its own proxy layer in the front end, and is therefore not at all relevant.

Without verifying this statement, I thought this must be true. gunicorn is not a full fledged webserver. In fact gunicorn creators strongly recommend using it behind a proxy server (Eg. Nginx). See docs.

I was confused because I always thought of Nginx as just a reverse proxy. Functioning as a reverse proxy for static assets is just one of the functions of nginx. It provides a lot more features like buffering slow clients, which gunicorn does not, which helps prevent denial-of-service attacks.

I knew this already. It would have been foolish to not use nginx or any other webserver.

Whitenoise is just there to set proper caching headers for static files and enable compression using gzip/brotli. When used with whitenoise.storage.CompressedManifestStaticFilesStorage, it will automatically generate versioned static files. Eg. /static/js/app.49ec9402.js if you have put your file in the template as {%statis%} 'js/app.js'. A versioned file will have max-age set to 10 years i.e. cached forever.

If you are not deploying on Heroku you will still need a web server like Nginx. So you can follow any 'nginx + gunicorn + django' deployment instructions/tutorials, which are easily available with a simple google search. One of which is Deploying your Django static files to AWS (Part 2), which helped me get this issue sorted out.

GunnerFan
  • 3,576
  • 3
  • 25
  • 38
  • 4
    You deserve more upvotes for that. It certainly was really confusing to me too. They should provide a graph of the stack in which whitenoise plays a role. Even Jacob Kaplan-Moss in his talk [here](https://youtu.be/E613X3RBegI?t=337]) says we can now use whitenoise to serve our static files **AND our dynamic files**. Which now that I get it is incorrect, I guess. – lapin Jul 10 '20 at 13:22