nginx+lua+memcached,报错:curl: (52) Empty reply from server

发布于 2021-11-23 20:32:01 字数 1467 浏览 878 评论 8

server {
  listen 80;
  server_name localhost;
  index index.html index.htm index.jsp;
  root /www;
  
  autoindex on;
  autoindex_exact_size on;
  autoindex_localtime on;

  location / {
    default_type text/html;

    access_by_lua '
      clinetIP = ngx.req.get_headers()["X-Real-IP"]
      if clinetIP == nil then
        clientIP = ngx.req.get_headers()["x_forwarded_for"]
      end
      if clinetIP == nil then
        clinetIP = ngx.var.remote_addr
      end
      ngx.say(clientIP)
      
      local memcached = require "resty.memcached"
      local memc, err = memcached:new()
      if not memc then
        ngx.say("failed to instantiate memc: ", err)
        return
      end
      local ok, err = memc:connect("127.0.0.1", 11211)
      if not ok then
        ngx.say("failed to connect: ", err)
        return
      end
      
      local res, flags, err = memc:get("id")
      ngx.say(res)
    
      if err then
        ngx.say("failed to get clientIP ", err)
        return
      end

      if res == "1" then
        ngx.exec("@client_test")
        return
      end

      ngx.exec("@client")
    ';
  }

  location @client{
    echo "client";
  }

  location @client_test{
    echo "client_test";
  }
}
nginx -t 通过

但是执行curl http://localhost/,报错:

curl: (52) Empty reply from server

这是什么意思?




如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

坏尐絯 2021-11-30 01:00:31

引用来自“char1st”的评论

ngx.say()  即response

ngx.exec() 是内部重定向

你不能响应后再内部重定向。

千笙结 2021-11-30 00:23:53

我直接用的是openResty http://openresty.org/cn/index.html 这个打包好了很多lua组件。

回忆凄美了谁 2021-11-29 22:23:01

引用来自“char1st”的评论

ngx.say()  即response

ngx.exec() 是内部重定向

你不能响应后再内部重定向。

风透绣罗衣 2021-11-29 21:36:02

去看tengine 的error.log 。 PS最好把lua代码单独写一个文件 。

不乱于心 2021-11-29 18:58:29

引用来自“char1st”的评论

ngx.say()  即response

ngx.exec() 是内部重定向

你不能响应后再内部重定向。

沙与沫 2021-11-29 11:02:53

我现在都是在root下操作的

空城仅有旧梦在 2021-11-28 06:18:42

1.用浏览器访问,看是否可以

2.尝试用curl http://本机ip地址,是否可以

3.尝试用curl http://127.0.0.1,是否可以

如果可以,应该是权限问题。sudo,或者用root登录运行就可以了。

能否归途做我良人 2021-11-26 22:55:42

ngx.say()  即response

ngx.exec() 是内部重定向

你不能响应后再内部重定向。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文