python flask - 如何在nginx中设置缓存

时间:2016-11-14 18:51:39

标签: python caching nginx flask

我有一个flask / nginx应用程序,并希望为2个示例url设置缓存:

1. /category/apps-5.html
2. /product/test-product-sell-34690064571.html

我的实际nginx配置是这样的:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

server {
    listen 443;
    ssl on;

    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    server_name  selfmarket.net www.selfmarket.net;

    ssl_certificate /etc/letsencrypt/live/selfmarket.net/cert.pem;        # path to your cacert.pem
    ssl_certificate_key /etc/letsencrypt/live/selfmarket.net/privkey.pem;    # path to your privkey.pem

    fastcgi_param   HTTPS               on;
    fastcgi_param   HTTP_SCHEME         https;

    # Serve static files and uploads
    location ^~ /static/ {
        root /opt/master/app/;
    }


    location / {
        proxy_pass 'http://127.0.0.1:8080/';
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
}

我如何为我的示例网址设置简单缓存(3分钟到期)?是我第一次使用nginx ..我很开始,谢谢

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是在后端缓存。 Flask有一个简单的缓存,或者你可以使用memcache。请参阅示例hereherehere

此外,如果您真的需要加快速度,可以使用NGINX提供的内置内存缓存支持(尽管这可能过于复杂)。有关详细信息,请参阅此doc

如果您真的想要NGINX进行缓存,请查看proxy_cache模块上的official docs。还有一本指南here