Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nginx to serve content directly out of a redis cache

I am using nginx to pass requests to a Node app. The app basically acts as a remote cache for html (checks to see if what the user is requesting is in the redis db, if it is just show that, if not grab it and store it in the redis cache and serve it up.)

I was curious if there was anyway to bypass hitting the Node app by having nginx serve up the content directly from redis? I have been fooling around with the http_redis module but I can't really get it to work.

A simple example would be: http://mywebsite.com/a where nginx would serve the content up in the 'a' key or pass it on to the node app if the key did not exist. Is this even possible?

like image 488
Ryan S Avatar asked Dec 29 '10 17:12

Ryan S


People also ask

Can nginx cache dynamic content?

Cache both static and dynamic content from your proxied web and application servers, to speed delivery to clients and reduce the load on the servers.

Does nginx support caching?

By default, NGINX Plus and NGINX serve cached content for as long as it is valid. Validity is configurable or can be controlled by the Cache-Control header set by the origin server.

What is Redis in nginx?

With Redis we can use replication to create additional caches. While we could store cache files in a shared folder Nginx stores the cache keys in memory so this makes it difficult to scale out our caching solution.

Is Redis a reverse proxy?

nginx-redis-proxy is a reverse proxy based on nginx and redis to cache objects (web pages and more).


1 Answers

Maybe something more difficult to setup than Webdis but you can do that directly in the nginx daemon with some extra modules like redis2-nginx-module. You will have to recompile nginx.

There is some good examples of configuration on the home page.

For instance :

# GET /get?key=some_key
location /get {
    set_unescape_uri $key $arg_key;  # this requires ngx_set_misc
    redis2_query get $key;
    redis2_pass foo.com:6379;
}

Of course, with a little more nginx configuration, you can get another URL pattern.

Note that for this example, you will have to compile ngx_set_misc module too.

like image 128
Fabien Avatar answered Sep 21 '22 18:09

Fabien