Windows,Flask 和 Cherrypy 作为 WSGI 服务器 - gzip 压缩不起作用
Windows, Flask and Cherrypy as WSGI server - gzip compression does not work
我运行正在公司使用一个较旧的应用程序,由于内部政策和应用程序需要,它必须 运行 Windows7。应用程序来自 Flask 0.12.4,WSGI 服务器是 Cherrypy 17.4.1。我成功 运行 这个应用程序,但我想打开 GZIP 压缩,但我无法做到这一点。我浏览了几个文档和代码示例,但没有任何效果。应用程序仅适用于内网,因此我无法使用某些在线工具对其进行测试。
我的cherrypy配置:
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()
我已经使用 firefox 和 chrome 开发人员工具测试了提供的文件,但它声称没有 GZIP 压缩。
使用 curl 似乎是一样的:
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
的确,没有它我也能活下去,但我真的很好奇这里出了什么问题。如果有人能给我建议,我将不胜感激。
您不能覆盖 /static
路径,因为所有 /
都委托给了 Flask,您也不能应用工具。来自 CherryPy 文档 Advanced 章节中的 Host a foreign WSGI application in CherryPy section:
Important
You cannot use tools with a foreign WSGI application.
您必须使用单独的路径(如 /site
)来为 Flask WSGI 应用程序提供服务,并硬编码 /static
作为模板中静态资源的基本路径。
将 Flask 服务器推向生产环境时,您确实希望从 CDN 或专用 HTTP 服务器(如 nginx)提供静态资源,而不是通过 Flask 或 CherryPy。您可以简单地将 nginx 配置为始终直接从磁盘提供压缩服务 /static
,例如,同时从 WSGI 提供任何剩余路径。
我运行正在公司使用一个较旧的应用程序,由于内部政策和应用程序需要,它必须 运行 Windows7。应用程序来自 Flask 0.12.4,WSGI 服务器是 Cherrypy 17.4.1。我成功 运行 这个应用程序,但我想打开 GZIP 压缩,但我无法做到这一点。我浏览了几个文档和代码示例,但没有任何效果。应用程序仅适用于内网,因此我无法使用某些在线工具对其进行测试。
我的cherrypy配置:
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()
我已经使用 firefox 和 chrome 开发人员工具测试了提供的文件,但它声称没有 GZIP 压缩。
使用 curl 似乎是一样的:
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
的确,没有它我也能活下去,但我真的很好奇这里出了什么问题。如果有人能给我建议,我将不胜感激。
您不能覆盖 /static
路径,因为所有 /
都委托给了 Flask,您也不能应用工具。来自 CherryPy 文档 Advanced 章节中的 Host a foreign WSGI application in CherryPy section:
Important
You cannot use tools with a foreign WSGI application.
您必须使用单独的路径(如 /site
)来为 Flask WSGI 应用程序提供服务,并硬编码 /static
作为模板中静态资源的基本路径。
将 Flask 服务器推向生产环境时,您确实希望从 CDN 或专用 HTTP 服务器(如 nginx)提供静态资源,而不是通过 Flask 或 CherryPy。您可以简单地将 nginx 配置为始终直接从磁盘提供压缩服务 /static
,例如,同时从 WSGI 提供任何剩余路径。