加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

在Nginx上使用Lua重定向到相同的URL(openresty设置)

发布时间:2020-12-13 20:54:29 所属栏目:Nginx 来源:网络整理
导读:我正在尝试修改请求标头并在Lua中将其重定向, ngx.redirect("/") 和 ngx.exec("/") 但我收到以下错误: attempt to call ngx.redirect after sending out the headers 有没有一种简单的方法来添加标头值并将其重定向到Lua中的其他位置?在文档中我没有找到任

我正在尝试修改请求标头并在Lua中将其重定向,

 ngx.redirect("/")

 ngx.exec("/")

但我收到以下错误:

attempt to call ngx.redirect after sending out the headers

有没有一种简单的方法来添加标头值并将其重定向到Lua中的其他位置?在文档中我没有找到任何合适的指令,是否仍可以在使用content_by_lua_file的同时完成类似的操作?

我正在使用openresty.

最佳答案
从redirect method documentation:

Note that this method call terminates the processing of the current request and that it must be called before ngx.send_headers or explicit response body outputs by either ngx.print or ngx.say.

因此,请检查或使用其他请求阶段处理程序,例如rewrite_by_lua.

至于设置标题,请使用ngx.header

例如:

location /testRedirect {
   content_by_lua '
     ngx.header["My-header"]= "foo"
     return ngx.redirect("http://www.google.com")
   ';
}

curl 07002

输出:

HTTP/1.1 302 Moved Temporarily
Server: openresty
Date: Tue,30 Jun 2015 17:34:38 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
My-header: foo
Location: http://www.google.com

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

注意:大多数站点将不接受来自重定向的自定义标头,因此请考虑在这种情况下使用cookie.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读