首页 > 解决方案 > 如何使用 curl 获取数据而不是网站?

问题描述

我有这段代码,据说它从 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"

标签: curlnginxredisgetopenresty

解决方案


推荐阅读