微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

openresty为什么访问根目录时access_by_lua_file调用两次

如何解决openresty为什么访问根目录时access_by_lua_file调用两次

当我使用openresty通过Lua监视器来监视IP时,为什么访问根目录时access_by_lua_file会调用两次 这是我的用法

http {
   access_by_lua_file lua/test.lua;
   server{
    location / {
    default_type text/html;
    }
   }
}

解决方法

https://nginx.org/en/docs/http/ngx_http_index_module.html

应注意,使用索引文件会导致内部重定向

也就是说,对根(/)的请求在内部重定向到/index.html

这是一个演示:

http {
    access_log /dev/stdout;
    access_by_lua_block {
        ngx.log(ngx.INFO,ngx.var.uri,' ',ngx.req.is_internal())
    }
    server {
        listen 8888;
        location / {
            default_type text/html;
        }
    }
}

curl localhost:8888/index.html

2020/08/17 15:14:22 [info] 22411#22411:* 5 [lua] access_by_lua(nginx.conf:15):2: /index.html false ,客户端:127.0.0.1,服务器:,请求:“ GET /index.html HTTP / 1.1”,主机:“ localhost:8888”

127.0.0.1--[17 / Aug / 2020:15:14:22 +0300]“ GET /index.html HTTP / 1.1” 200 14“-”“ curl / 7.68.0”

curl localhost:8888/

2020/08/17 15:15:31 [info] 22411#22411:* 6 [lua] access_by_lua(nginx.conf:15):2: / false ,客户端:127.0。 0.1,服务器:,请求:“ GET / HTTP / 1.1”,主机:“ localhost:8888”

2020/08/17 15:15:31 [info] 22411#22411:* 6 [lua] access_by_lua(nginx.conf:15):2: /index.html true ,客户端:127.0.0.1,服务器:,请求:“ GET / HTTP / 1.1”,主机:“ localhost:8888”

127.0.0.1--[17 / Aug / 2020:15:15:31 +0300]“ GET / HTTP / 1.1” 200 14“-”“ curl / 7.68.0”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。