Home > Backend Development > PHP Tutorial > Another way to build nginx_lua environment

Another way to build nginx_lua environment

WBOY
Release: 2016-08-08 09:30:09
Original
1069 people have browsed it
Use the integration package provided by Daniel ZhangYichun (http://openresty.org/) for quick installation.

It’s very simple, download ngx_openresty, the integration package includes: Nginx, Lua or Luajit, ngx_lua, and some useful Nginx third-party modules.

For example:

nginx’s third-party module redis. This package is essentially a .lua file, which is a library file that provides some interfaces for accessing redis:

Download it:

git clone https:// github.com/agentzh/lua-resty-redis.git

Copy:

In this package, there is a Lib directory. Copy the files and subdirectories in the Lib directory to the directory configured by lua_package_path above (here is /data /nginx-1.4.2/)

Write a simple lua program to connect to redis and get the content inside:

For example: write a test_redis.lua and put it under /data0/nginx-1.4.2/lua/
local redis = require "resty.redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
    ngx.say("failed to connect:", err)
	return
end


res, err = cache:set("dog", "an aniaml")
if not ok then
    ngx.say("failed to set dog: ", err)
    return
end
ngx.say("set result: ", res)


local res, err = cache:get("dog")
if not res then
	ngx.say("failed to get dog: ", err)
	return
end


if res == ngx.null then
    ngx.say("dog not found.")
    return
end
ngx.say("dog: ", res)


local ok, err = cache:close()
if not ok then
        ngx.say("failed to close:", err)
        return
end
Copy after login

Configure the corresponding access location in nginx.conf:
location /test_redis {
content_by_lua_file lua/test_redis.lua;
}
[root@localhost conf]# curl http://localhost/test_redis
set result: OK
dog: an aniaml

The above introduces another method of setting up the nginx_lua environment, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template