August 2017
Intermediate to advanced
278 pages
8h 53m
English
We again used content_by_lua_block to return content, and this contains our basic counter code. It starts by making a connection to Redis on the localhost (127.0.0.1) and returns an error if the connection fails.
If the connection is successful, we attempt to fetch a value from Redis with a key named counter. In the event that there's no data or an invalid number, we set the counter to 0 (zero). Then, we increment the Lua variable to indicate there's been a hit to this URL. This is then stored back in Redis (via the set command), and the value of the counter is returned as plain text with a status of 200 (the default for ngx.say).
Read now
Unlock full access