Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

你好,请问下下面的redis的set的配置写法是否准确? #10

Closed
LazyZhu opened this issue Oct 23, 2012 · 4 comments
Closed

Comments

@LazyZhu
Copy link

LazyZhu commented Oct 23, 2012

主要是变量的写法无法确定是否准确。

location /toredis {
    default_type text/html;
    set $key $url;
    set $body $echo_request_body;
    set $exptime 300;
    content_by_lua '
        local redis = require "resty.redis"
        local red = redis:new()

        red:set_timeout(1000)

        local ok, err = red:connect("127.0.0.1", 6379)
        if not ok then
            return nil
        end

        local ok, err = red:set(ngx.var.key,ngx.var.body,ngx.var.exptime)
        if not ok then
            return nil
        end

        red:close()
    ';
}
@agentzh
Copy link
Member

agentzh commented Oct 23, 2012

你应当使用 ngx.req.read_body(), ngx.req.get_body_data() 和 ngx.req.get_body_file() 来读取请求体。另一种选择是使用 ngx.req.socket().

你上面的代码并没有读取请求体数据。引用 $echo_request_body 变量并不会自动触发请求体读取操作。

@LazyZhu
Copy link
Author

LazyZhu commented Oct 23, 2012

感谢回复!

如果我在content_by_lua前面加proxy_pass是否就可以使 $echo_request_body 变量赋值?

@LazyZhu
Copy link
Author

LazyZhu commented Oct 23, 2012

加入了请求读取操作,请帮忙再看一下:

location /from_proxy {
    proxy_pass http://backend;
}

location /to_redis {
    default_type text/html;
    set $key $url;
    set $exptime 300;
    content_by_lua '

        local res = ngx.location.capture("/from_proxy", {
            method = ngx.HTTP_GET,
            body = ngx.req.get_body_data()})

        local redis = require "resty.redis"
        local red = redis:new()

        red:set_timeout(1000)

        local ok, err = red:connect("127.0.0.1", 6379)
        if not ok then
            return nil
        end

        local ok, err = red:set(ngx.var.key,ngx.var.res.body,ngx.var.exptime)
        if not ok then
            return nil
        end

        red:close()
    ';
}

@agentzh
Copy link
Member

agentzh commented Oct 23, 2012

Hello!

On Mon, Oct 22, 2012 at 10:42 PM, LazyZhu notifications@github.com wrote:

加入了请求体读取操作,请帮忙再看一下:

location /proxy_request {
proxy_pass http://backend;
}

location /toredis {
default_type text/html;
set $key $url;
set $exptime 300;
content_by_lua '

    local res = ngx.location.capture("/proxy_request", {
        body = ngx.req.get_body_data()})

你这里并没有如你说的那样加入请求体读取操作。ngx.req.read_body() 才是请求体读取操作:

http://wiki.nginx.org/HttpLuaModule#ngx.req.read_body

你调用 ngx.req.get_body_data() 和读取 $echo_request_body 变量的效果几乎是完全相同的。

-agentzh

@LazyZhu LazyZhu closed this as completed Oct 26, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants