我正在使用 Flask 编写一个简单的 Web 应用程序,并将使用 Gunicorn 运行它。我想知道如何使用 Varnish 缓存此应用程序返回的页面。
通过关注 this article,我已经能够将 Varnish 与 Django 应用程序一起使用,该应用程序也在 Gunicorn 上运行.说明包括使用一个额外的应用程序和一些中间件,但我不确定如何使用 Flask 来完成。
感谢您的建议!
最佳答案
基本上,您所要做的就是在呈现 Flask View 时返回适当的缓存 header 。
例如,这是一个简单的 View ,它呈现一个 robots.txt
文件,并指定它应该被缓存 30 天:
from flask import Flask, make_response, render_template
app = Flask(__name__)
@app.route('/robots.txt')
def robots():
response = make_response(render_template('robots.txt'))
response.headers['Cache-Control'] = 'max-age=%d' % 60 * 60 * 24 * 30
return response
关于python - 如何使用 Gunicorn 和 Varnish 运行 Flask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916341/