如何使用curl代替网站获取数据?

时间:2018-07-17 05:48:03

标签: curl nginx redis get openresty

我有这段代码,据说可以从url中获取redis键值:

location /getkey/(^[a-zA-Z0-9_.-]*)$ {
            add_header 'Access-Control-Allow-Origin' '*';
            content_by_lua_block {
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeout(1000) -- 1 sec

                -- or connect to a unix domain socket file listened
                -- by a redis server:
                --     local ok, err = red:connect("unix:/path/to/redis.sock")

                local ok, err = red:connect("127.0.0.1", 6379)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end


                ngx.say("set result: ", ok)
                ngx.say("Getting $1")
                local res, err = red:get("$1")
                if not res then
                    ngx.say("failed to get $1: ", err)
                    return
                end

                if res == ngx.null then
                    ngx.say("$1 not found.")
                    return
                end

                ngx.say(res)
    }
}

当前,我正在使用以下curl命令来获取数据响应:

curl -X GET http://localhost/getkey/insertkeyhere

但是我得到的却是html:

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>openresty/1.13.6.2</center>
</body>
</html>

在我的日志中,它显示找不到目录/位置:

2018/07/17 13:38:17 [error] 18425#18425: *179 open() "/usr/local/openresty/nginx/html/getkey/hello2" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /getkey/hello2 HTTP/1.1", host: "localhost"

0 个答案:

没有答案