简体   繁体   English

django css和js文件在heroku上压缩gzip

[英]django css and js files gzip compression on heroku

I am trying to compress my css and js files and serve as gzip file, but with no success. 我正在尝试压缩我的css和js文件并作为gzip文件,但没有成功。

I have tried all methods, django-pipeline, django-compressor etc. 我尝试了所有方法,django-pipeline,django-compressor等。

I got django-compressor to create the gzip files but I am not sure how to serve them. 我有django-compressor来创建gzip文件,但我不知道如何为它们服务。 I use GzipMiddleaware to compress and serve my html files. 我使用GzipMiddleaware来压缩和提供我的html文件。

Can any one please write step by step guide on how gzip and serve static files on heroku django. 任何人都可以写一步一步指导gzip如何在heroku django上提供静态文件。

Sorry to ask for detailed steps, I am not asking to be spoon fed, but am stuck with this issue for almost 24 hours now, tried n number of solutions, but none seems to work. 很抱歉要求详细的步骤,我不是要求用勺子喂,但是我现在已经坚持了这个问题将近24小时,尝试了许多解决方案,但似乎都没有。

FYI, I am using django-1.5 仅供参考,我使用的是django-1.5

Thanks in advance for your help 在此先感谢您的帮助

You should seriously consider uploading your static files to s3 as well. 您应该认真考虑将静态文件上传到s3。 Django is really not cut out for serving files, and it will consume precious resources on your app server to do so. Django实际上并没有因为提供文件而被淘汰,而且它会占用您应用服务器上的宝贵资源。

Here's some great reasons for using s3 for staticfiles. 以下是将s3用于静态文件的一些重要原因。 http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/ http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/

Here's a article explaining how to get S3 to serve gzipped resources: http://www.jamiebegin.com/serving-compressed-gzipped-static-files-from-amazon-s3-or-cloudfront/ 这篇文章解释了如何让S3提供gzip资源: http//www.jamiebegin.com/serving-compressed-gzipped-static-files-from-amazon-s3-or-cloudfront/

Here's a storage backend that will collectstatic to s3. 这是一个存储后端,将收集静态到s3。

You could probably easily write a storage backend that gzips and upload to s3 by subclassing this backend and django-compressor's backend. 您可以轻松地编写一个存储后端gzip 通过继承此后端和django-compressor的后端来上传到s3。

I had the exact same issue. 我有完全相同的问题。 I was using grunt but found there was a lot of extra config etc etc etc then I found django-compressor which is AWESOME! 我正在使用grunt但发现有很多额外配置等等然后我发现django-compressor是真棒!

But I deploy to Heroku which doesn't gzip anything and relys on the app to do it. 但我部署到Heroku,它没有gzip任何东西,并依赖于应用程序来做到这一点。

I couldnt find any wsgi wrapper that served pre-compressed gzip files, so I extended dj-static to do it for me. 我找不到任何提供预压缩gzip文件的wsgi包装器,所以我扩展了dj-static来为我做这件事。 Check out my fork of dj-static which includes the additions. 查看我的dj-static分支,其中包括添加内容。 Basically it will check for a corresponding static filename that ends in .gz and if it finds it, it will serve it instead. 基本上它会检查以.gz结尾的相应静态文件名,如果找到它,它将代替它。 I use this for my Heroku projects. 我用它来做我的Heroku项目。

https://github.com/hdickson/dj-static https://github.com/hdickson/dj-static

To test on your dev server if you run the django built in web server be sure to run it with the --nostatic command line switch. 要在您的开发服务器上进行测试,如果您运行django内置的Web服务器,请确保使用--nostatic命令行开关运行它。 ie python manage.py runserver 0.0.0.0:80 --nostatic 即python manage.py runserver 0.0.0.0:80 --nostatic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM