【问题标题】:Python WhiteNoise not gzip compressing in Flask applicationPython WhiteNoise 不在 Flask 应用程序中进行 gzip 压缩
【发布时间】:2018-05-23 10:08:37
【问题描述】:

我在Flask/Python3 应用程序中使用了WhiteNoisegunicorn 是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)

我什至尝试在 Flask object 中创建 static_folder 并稍后将其添加到其他 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 不存在。我错过了什么?

【问题讨论】:

    标签: python python-3.x flask python-requests gzip


    【解决方案1】:

    经过几次尝试,我找到了解决方案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/' 路径,因此必须提供绝对路径。

    【讨论】:

    • 所以归结为路径不正确,您还需要使用whitenoise.compress对其进行压缩吗?
    • 不仅路径不正确,prefix 参数也是必需的,但WhiteNoise 文档中没有记录。也许也有用,stable WhiteNoise 文档更加过时,您必须使用WhiteNoise latest 文档:whitenoise.evans.io/en/latest/flask.html。不过,它没有提到prefix 的要求。不,不需要whitenoise.compress。我尝试了使用和不使用当前.gz 文件,似乎whitenoise自行生成它。至少我在 Heroku 上的应用是这样的。
    • 干杯。感谢您参与尝试解决这个问题。
    • 遇到了同样的问题。对我来说,prefixwhitenoise.compress 的组合有效。
    【解决方案2】:

    您应该使用 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
    

    你可以运行这个 在开发过程中并将您的压缩文件提交到您的 存储库,或者您可以将其作为构建和部署的一部分运行 进程。

    【讨论】:

    • 感谢您已经为我指明了正确的方向。我假设gzip 文件是从whitenoise 创建的。但是,即使在我的根路径中有 .gz 文件后,我也会收到相同的 curl 响应。
    • 我不确定。文件夹中是否存在原始未压缩文件?也许尝试删除它们。
    • 我已经试过了。尝试卷曲其中一个css 时,删除原始未压缩文件会给我一个404 错误。将curl 用于css.gz 会给我一个200 响应,但仍然缺少Content-Encoding: gzip 标头。此外,我看到WhiteNoise 文档没有说明为Flask 应用手动生成gzip 文件,仅针对WSGI 应用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2016-09-08
    • 2020-11-03
    • 2012-11-27
    • 2012-09-01
    相关资源
    最近更新 更多