>  기사  >  백엔드 개발  >  nginx_lua 환경을 구축하는 또 다른 방법

nginx_lua 환경을 구축하는 또 다른 방법

WBOY
WBOY원래의
2016-08-08 09:30:091019검색
빠른 설치를 위해서는 Daniel ZhangYichun(http://openresty.org/)에서 제공하는 통합 패키지를 사용하세요.

매우 간단합니다. ngx_openresty를 다운로드하세요. 통합 패키지에는 Nginx, Lua 또는 Luajit, ngx_lua 및 일부 유용한 Nginx 타사 모듈이 포함되어 있습니다.

예:

nginx의 타사 모듈 redis에서 이 패키지는 기본적으로 Redis 액세스를 위한 일부 인터페이스를 제공하는 라이브러리 파일인 .lua 파일입니다.

다운로드 아래:

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

복사:

이 패키지에는 Lib 디렉터리가 있습니다. Lib 디렉터리에 있는 파일과 하위 디렉터리를 위의 lua_package_path로 구성된 디렉터리(여기서는 /data/nginx-1.4.2/)에 복사한 후

작성하세요. 간단한 lua 프로그램은 redis에 연결하고 내용을 얻습니다.

예: test_redis.lua를 작성하고 /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

nginx.conf 해당 액세스 위치 구성:
location /test_redis {
content_by_lua_file lua/test_redis.lua;
}
[root@localhost conf]# 컬 http://localhost/ test_redis
결과 설정: OK
개: aniaml

위 내용은 관련 측면을 포함하여 nginx_lua 환경을 설정하는 또 다른 방법을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.