【问题标题】:python flask - how setup cache in nginxpython flask - 如何在 nginx 中设置缓存
【发布时间】:2017-03-28 12:06:49
【问题描述】:

我有一个烧瓶/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 .. 我是初学者,谢谢

【问题讨论】:

    标签: python caching nginx flask


    【解决方案1】:

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

    另外,如果你真的需要加快速度,你可以使用 NGINX 内置的 memcache 支持(虽然这可能太复杂了)。有关详细信息,请参阅此doc

    如果您真的希望 NGINX 进行缓存,请查看 proxy_cache 模块上的 official docs。还有一个可用的指南here

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 2018-03-21
      • 2018-08-07
      • 1970-01-01
      • 2021-10-04
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多