我在WhiteNoise
应用程序中使用了Flask/Python3
,其中gunicorn
是Web服务器,如下所示:
from whitenoise import WhiteNoise
app = Flask(__name__, static_folder='static')
app.wsgi_app = WhiteNoise(app.wsgi_app, root='static/')
app.wsgi_app.add_files(app.static_folder)
我甚至尝试在static_folder
中创建Flask object
并稍后将其添加到其他WhiteNoise
文件中,但这些文件也无效。编译很好,但是当我这样做时:
curl -i -H "Accept-Encoding: gzip" https://my-homepage/static/css/my.css
我收到以下回复:
HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.8.1
Date: Wed, 23 May 2018 09:53:38 GMT
Content-Length: 50162
Content-Type: text/css; charset=utf-8
Last-Modified: Wed, 23 May 2018 09:51:21 GMT
Cache-Control: public, max-age=43200
Expires: Wed, 23 May 2018 21:53:38 GMT
Etag: "1527069081.0-50162-130551313"
Accept-Ranges: bytes
Strict-Transport-Security: max-age=31536000
Via: 1.1 vegur
您可以看到Content-Encoding: gzip
不存在。我错过了什么?
答案 0 :(得分:2)
经过几次尝试后,我找到了解决方案。似乎WhiteNoise
文档有点过时,并没有提及所有内容。
我更改了以下行:
app.wsgi_app = WhiteNoise(app.wsgi_app, root='static/')
为:
app.wsgi_app = WhiteNoise(app.wsgi_app, root=os.path.join(os.path.dirname(__file__), 'static'), prefix='static/')
首先,prefix
参数是必需的(文档中未提及),此外Flask
应用程序不知道如何处理'static/'
路径,因此绝对路径具有提供。
答案 1 :(得分:0)
您应该使用WhiteNoise附带的command line utility自行进行压缩。
引用
WhiteNoise附带一个命令行实用程序,它将生成 您的文件的压缩版本。
$ python -m whitenoise.compress --help
usage: compress.py [-h] [-q] [--no-gzip] [--no-brotli]
root [extensions [extensions ...]]
Search for all files inside <root> *not* matching <extensions> and produce
compressed versions with '.gz' and '.br' suffixes (as long as this results in
a smaller file)
positional arguments:
root Path root from which to search for files
extensions File extensions to exclude from compression (default: jpg,
jpeg, png, gif, webp, zip, gz, tgz, bz2, tbz, swf, flv, woff,
woff2)
optional arguments:
-h, --help show this help message and exit
-q, --quiet Don't produce log output
--no-gzip Don't produce gzip '.gz' files
--no-brotli Don't produce brotli '.br' files
你可以运行它 在开发过程中,将压缩文件提交给您 存储库,或者您可以将其作为构建和部署的一部分运行 过程