简体   繁体   English

如何使用Gunicorn和Varnish运行Flask?

[英]How to run Flask with Gunicorn and Varnish?

I'm writing a simple web application with Flask and will run it using Gunicorn. 我正在使用Flask编写一个简单的Web应用程序,并将使用Gunicorn运行它。 I'd like to know how to cache the pages returned by this application using Varnish. 我想知道如何使用Varnish缓存此应用程序返回的页面。

I've been able to use Varnish with a Django application, also running on Gunicorn, by following this article . 我已经能够使用清漆Django应用程序,还对Gunicorn运行,按照这篇文章 The instructions included using one extra application and some middleware, but I'm not sure how to do it with Flask. 说明包括使用一个额外的应用程序和一些中间件,但我不知道如何使用Flask。

Thanks for your suggestions! 谢谢你的建议!

Basically, all you have to do is return the appropriate cache headers when you render your Flask views. 基本上,您只需在渲染Flask视图时返回相应的缓存标头。

For instance, here is a simple view which renders a robots.txt file, and specifies that it should be cached for 30 days: 例如,这是一个简单的视图,它呈现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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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