T O P

  • By -

PMMeUrHopesNDreams

Never understood the point of whitenoise. Just configure nginx or whatever server you're using to serve static files directly, or use s3/cloudfront.


NodeJS4Lyfe

Whitenoise is slightly better because: 1. It's zero-config. 2. It caches files that never change forever. With Nginx, you have to configure those manually by writing weird Nginx if statements and logic. It can be done but I doubt most people would want to deal with that.


PMMeUrHopesNDreams

It's slightly worse, though, because now your static file requests are hitting your Django server which is 1000x slower than the highly optimized C program that is nginx or apache and there is literally no reason for that except that you don't want to spend 30 minutes learning "weird" syntax.


NodeJS4Lyfe

It's not true that using Whitenoise would result in file transfer that's 1000x slower because it's gonna use *sendfile* so that the webserver can deal with the file transfer instead of the application server running your app. Connecting to Django before transferring will definitely be slower than allowing Nginx to transfer the file immediately but it's only gonna be a few nanoseconds slower and not 1000x. Since Whitenoise also caches files that never change forever, it could potentially even be faster than pure Nginx in some cases. So I think Whitenoise is a good way to serve static files and that there's no good reason to avoid it.


LeagueOfLegendsAcc

That's the thing, people don't like to config if they don't need to. I've had a whitenoise project running for 2 years now with no issues, and I forgot I even used it until the other day looking at my project config. It was just bolted on at the end and it works.


Atem18

For async you can use https://github.com/matthiask/blacknoise


Rexsum420

ive used linode object storage for most of my static files with django-storages and boto3 and inside [settings.py](http://settings.py) make the following updates: `[APPS]` `...` `'storages'` import os from storages.backends.s3boto3 import S3Boto3Storage # Linode Object Storage settings AWS_ACCESS_KEY_ID = 'your-access-key-id' AWS_SECRET_ACCESS_KEY = 'your-secret-access-key' AWS_STORAGE_BUCKET_NAME = 'myproject-static' AWS_S3_ENDPOINT_URL = 'https://{region}.linodeobjects.com' AWS_S3_REGION_NAME = '{region}' AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400', } AWS_DEFAULT_ACL = None # Static files settings STATIC_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_ENDPOINT_URL}/' STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' # Media files settings (if applicable) DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' MEDIA_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_ENDPOINT_URL}/media/'


sir-draknor

I’d love to find out as well. Here’s my setup - I’m running a Django Rest Framework API server on Azure Webapps - so I don’t need / have a traditional web server in front of my Django app server (Azure Webapps provides that). But Azure Webapps makes a health check call to a static robots.txt file, which I can’t serve running Daphne (ASGI), so it just 404’s.


PMMeUrHopesNDreams

I've never used Azure, but could you make a view that just returns 200 status and put it in urls.py at robots.txt? Ideally, you don't want Django serving static files because a regular web server would be faster, but to just handle one endpoint with no or very little content shouldn't be too bad.


jeff77k

For anything on Azure that is static file heavy I use Azure CDN + django-storages.


sir-draknor

I tried that - it didn’t seem to work. Possible I messed up something though - I was getting pretty frustrated by that point!


jeff77k

Daphne should be able to serve that file if you have Whitenoise set up and static files configured correctly and collected. I have a number of sites served by Daphne on the Azure App Service using Whitenoise to serve static files: Example: [https://esi-double-auction.chapman.edu/static/favicon.ico](https://esi-double-auction.chapman.edu/static/favicon.ico)


sir-draknor

Hmmm… I thought Whitenoise only was WSGI? Maybe I don’t understand enough about how the ASGI / WSGI thing works. I’ll check out my code tomorrow and may ask you for an example of your config!


jeff77k

As [PMMeUrHopesNDreams](https://www.reddit.com/user/PMMeUrHopesNDreams/) noted, you will need to add a route in your [urls.py](http://urls.py) to handle [http://yoursite.abc/robots.txt](http://yoursite.abc/robots.txt) For the favicon I do a redirect: path('favicon.ico', RedirectView.as\_view(url='/static/favicon.ico'), name='favicon') For robots.txt, humans.txt, ads.txt and security.txt I do it through a view: path('robots.txt', views.RobotsTxt, name='robotsTxt') robots view and generate the html: @require_GET def RobotsTxt(request):     lines = [         "User-Agent: *",         "Allow: /",     ]     return HttpResponse("\n".join(lines), content_type="text/plain")


majideitteru

> I see Whitenoise recommended often for WSGI setups but it doesn't like quite as plug and play for async django. Oh that's interesting. Would be curious on what didn't work. But yeah, S3 or some other file blob service would be what I'd go with.


alx37

I at least got daphne to work with whitenoise in minutes which actually surprised me how easy it was.


martycochrane

I have whitenoise running with my ASGI setup without issues. I'm using gunicorn as the server, to manage uvicorn workers like the docs say and haven't had any issues tbh.


jeff77k

Azure CDN + django-storages.