Chris Love CNM | Tech Blog

Jul/13

1

A match made in heaven: Nginx, Redis, and Lua – Part 1

5 Flares Twitter 3 Facebook 1 Google+ 0 LinkedIn 1 Reddit 0 Filament.io Made with Flare More Info'> 5 Flares ×

Have a need for speed? Want to truly do some crazy stuff with web programming? Give the combination of nginx, lua, and redis a test drive.

So what the heck am I talking about. A couple of years ago I joined a band of motley men build high volume software platform to help out nonprofits on the web. More to come later about that. Doing prelim architecture, we figured out some system needs.

  • Crazy fast web server: nginx
  • Just as crazy fast nosql server: redis
  • Some glue to connect it together: lua

Quick background about these components

Nginx: Started off by Igor Sysoev in 2002, it is one of a few servers addressing C10k problem. I admire about Nginx because how it is based on an asynch event-driven architecture.

Nginx powers several high-visibility sites, such as Netflix, Hulu, Pinterest, CloudFlare, Airbnb, WordPress.com, GitHub, SoundCloud, Zynga, Eventbrite, Zappos, Media Temple, Heroku, RightScale, Engine Yard and NetDNA

Redis: Salvatore Sanfilippo, also known as antirez, created Redis as an in memory advanced key-value store. It is plain simple key based database that can contain strings, hashes, lists, sets and sorted sets. Redis is used by this sites such as: craigslist.org, github.com, guardian.co.uk, Disqus, Pinterest, stackoverflow.com. flickr.com, instagram.com, twitter.com. Lua is supported as a scripting language that can run operations in Redis, and this is how I discovered the wonderful scripting language call Lua.

Lua: The Pontifical Catholic University of Rio de Janeiro in Brazil designs, implements, and maintains this lightweight, super fast scripting language. Lua is actually one of the leading languages used in video games like World of Warcraft and Angry Birds. Xiaozhe Wang and Yichun “agentzh” Zhang maintain a nginx module that allows us to embed Lua scripts inside of Nginx. Since I always want Ferrari performance let’s throw in LuaJit into this stack of toys.

Well enough about what this stuff is, let’s install it.

The install

Most distros such as ubuntu have installs for the majority of these components. You may have to compile lua-nginx-module into nginx, and lua cjson may need to be installed via luarocks.

As Gentoo is my distro of choice here are the additions to the portage files.

make.conf:

NGINX_MODULES_HTTP="autoindex access browser charset empty_gif fastcgi gzip limit_req limit_conn map proxy referer rewrite  ssi userid lua ndk stub_status upstream_ip_hash geoip"

package.use

dev-db/redis jemalloc test
dev-libs/libpcre jit -bzip2 -readline -zlib unicode
www-servers/nginx fastcgi ssl xslt pcre-jit luajit aio

package.accept_keywords

=dev-lang/luajit-2.0.0_beta7 ~amd64
=dev-lua/lua-cjson-2.0.0 **

emerge

emerge redis dev-libs/libpcre lua dev-lua/lua-cjson dev-lang/lua dev-lang/luajit
emerge nginx

Redis client for Nginx

mkdir -p /u01/lua
cd /u01/lua
git clone https://github.com/agentzh/lua-resty-redis

Redis and Nginx should start without any initial configuration. We will need to tweak the nginx conf in order to run lua, but fire up Nginx and point you browser to localhost and voila. Start redis and you can test with:

./redis-cli
 redis> ping

should respond with ‘PONG’.

Other Install Notes

  1. Nginx install notes here outline source install
  2. Redis can be cloned from https://github.com/antirez/redis and installed from source
  3. Luarocks is a simple way to install cjson

Part 2 of this series will cover basic Nginx Lua programming.
Part 3 will cover using redis and Nginx.

No tags

2 comments

Leave a Reply

>>

5 Flares Twitter 3 Facebook 1 Google+ 0 LinkedIn 1 Reddit 0 Filament.io 5 Flares ×