使用nginx在多个实例中共享缓存

时间:2016-10-16 20:18:19

标签: caching nginx amazon-elasticache

我将提出我的问题......

我有一个Node.js网络应用程序正常工作。我在AWS(Elastic Load Balancing,经典模式)中有一个负载均衡器,带有一个带有多个EC2实例的Auto Scaling组。

每个EC2实例都安装了一个Nginx反向代理,可以与PM2和Node.js(Express)一起使用。我使用Nginx缓存静态文件,缓存请求,https和其他东西。

我需要知道如何使用Nginx在所有实例之间共享缓存,而不是在每台机器中使用每个实例的内存缓存。我想使用Memcache o Redis,但我需要选择一个,我更喜欢Redis。

这是我在这一刻工作的例子(只是重要的代码):

proxy_cache_path /cache/nginx levels=1:2 keys_zone=cache_zone_name:10m;
location / {
            #root   html;
            #index  index.html index.htm;

            #Config proxy inverse cache
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;

            # Add cache debugging header
            add_header X-Cache-Status $upstream_cache_status;

            # Configure cache
            proxy_cache        cache_zone_name;
            proxy_cache_valid  any 1m;
            proxy_cache_key    $scheme$host$request_uri;

        }

我在ServerFault中找到了这个问题而没有回答。

谢谢!

编辑:重要说明,我需要一个不影响性能的解决方案:)

1 个答案:

答案 0 :(得分:0)

nginx将缓存的数据存储在文件中,但 cache_keys的映射(网址的唯一标识符)到内容位置存储在内存中

例如:当您访问url http://localhost/index.html时,这意味着index.html的内容将存储在/ cache / nginx / c / 29 / b7f54b2df7773722d382f4809d65029c中,而cache_key的md5将存储在内存中(md5(httplocalhostindex) .html) - > / cache / nginx / c / 29 / b7f54b2df7773722d382f4809d65029c)

Nginx无法将此映射保留在本地存储或共享内存(redis / memcached)上,这将影响缓存性能

参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html(搜索关键字keys_zone)