简体   繁体   中英

Windows, Flask and Cherrypy as WSGI server - gzip compression does not work

I am running one older app in company, due to internal policy and app needs it must run on Windows7 . App is consist from Flask 0.12.4 , WSGI server is Cherrypy 17.4.1 . I successfully run this app but I would like to switch on GZIP compression, but I am not able to do that. I went through several documentation and code examples but nothing work. App is only for intranet so I can't test it using some online tools.

my cherrypy configuration:

import os
import cherrypy
from machines import app

path   = os.path.abspath(os.path.dirname(__file__))
config = {
  'global' : {
    'server.socket_host' : '0.0.0.0',
    'server.socket_port' : 5000,
    'server.thread_pool' : 10,
    'log.screen':True,
    'log.error_file': './log/cherry_error.log',
    'environment': 'production',
  },
  '/static' : {
    'tools.gzip.on'       : True,
    'tools.staticdir.on'  : True,
    'tools.staticdir.dir' : os.path.join(path, 'static'),
    'tools.gzip.mime_types': ['text/*', 'application/*'],
  }
}


if __name__ == '__main__':    
    cherrypy.tree.graft(app.wsgi_app, '/')
    cherrypy.config.update(config)       
    cherrypy.engine.start()

I have tested served files using firefox and chrome developers tools, but it claim no GZIP compression.

Using curl is seems to be same:

curl --head --compressed http://192.168.1.4:5000/static/style.css
HTTP/1.1 200 OK
Content-Length: 68506
Content-Type: text/css; charset=utf-8
Last-Modified: Sat, 07 Sep 2019 14:55:52 GMT
Cache-Control: public, max-age=43200
Expires: Sun, 08 Sep 2019 04:54:20 GMT
ETag: "1567868152.63-68506-1294405540"
Date: Sat, 07 Sep 2019 16:54:20 GMT
Accept-Ranges: bytes
Server: 0.0.0.0

True is I can live without that, but I am really curious what is wrong here. If somebody can advice me I would be really grateful.

You can't override the /static path, since all of / is delegated to Flask, nor can you apply tools. From the Host a foreign WSGI application in CherryPy section in the Advanced chapter of the CherryPy documentation:

Important

You cannot use tools with a foreign WSGI application.

You'd have to use a separate path, like /site , to serve the Flask WSGI app, and hardcode /static as the base path for static resources in your templates.

When pushing a Flask server to production, you really want to serve static resources from a CDN or dedicated HTTP server such as nginx, and not through Flask or CherryPy. You can trivially configure nginx to always serve /static straight from disk with compression, for example, while serving any remaining paths from WSGI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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