documented the way to output error pages with custom dynamic bodies in Lua. thanks rik1083.

This commit is contained in:
agentzh (章亦春) 2011-09-15 09:04:25 +08:00
Родитель 6c7e74fb6e
Коммит ac24af7f0e
6 изменённых файлов: 154 добавлений и 12 удалений

28
README
Просмотреть файл

@ -8,9 +8,9 @@ Status
This module is under active development and is already production ready.
Version
This document describes ngx_lua v0.3.1rc2
This document describes ngx_lua v0.3.1rc3
(<https://github.com/chaoslawful/lua-nginx-module/downloads>) released
on 12 September 2011.
on 13 September 2011.
Synopsis
# set search paths for pure Lua external libraries (';;' is the default path):
@ -1341,15 +1341,35 @@ Nginx API for Lua
context: *rewrite_by_lua*, access_by_lua*, content_by_lua**
When "status >= 200" ("ngx.HTTP_OK"), it will interrupt the execution of
the current Lua thread and returns status code to nginx.
the current request and return status code to nginx.
When "status == 0" ("ngx.OK"), it will quits the current phase handler
(or content handler if content_by_lua directives are used).
(or content handler if the content_by_lua directive are used).
The "status" argument can be "ngx.OK", "ngx.ERROR",
"ngx.HTTP_NOT_FOUND", "ngx.HTTP_MOVED_TEMPORARILY", or other HTTP status
constants.
To return an error page with custom contents, use code snippets like
this:
ngx.status = ngx.HTTP_GONE
ngx.say("This is our own content")
-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
The effect in action:
$ curl -i http://localhost/test
HTTP/1.1 410 Gone
Server: nginx/1.0.6
Date: Thu, 15 Sep 2011 00:51:48 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
This is our own content
ngx.eof
syntax: *ngx.eof()*

Просмотреть файл

@ -13,7 +13,7 @@ This module is under active development and is already production ready.
Version
=======
This document describes ngx_lua [v0.3.1rc2](https://github.com/chaoslawful/lua-nginx-module/downloads) released on 12 September 2011.
This document describes ngx_lua [v0.3.1rc3](https://github.com/chaoslawful/lua-nginx-module/downloads) released on 13 September 2011.
Synopsis
========
@ -1531,14 +1531,36 @@ ngx.exit
**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua**
When `status >= 200` (`ngx.HTTP_OK`), it will interrupt the execution of the current Lua thread and returns
status code to nginx.
When `status >= 200` (`ngx.HTTP_OK`), it will interrupt the execution of the current request and return status code to nginx.
When `status == 0` (`ngx.OK`), it will quits the current phase handler (or content handler if [content_by_lua](http://wiki.nginx.org/HttpLuaModule#content_by_lua) directives are used).
When `status == 0` (`ngx.OK`), it will quits the current phase handler (or content handler if the [content_by_lua](http://wiki.nginx.org/HttpLuaModule#content_by_lua) directive are used).
The `status` argument can be `ngx.OK`, `ngx.ERROR`, `ngx.HTTP_NOT_FOUND`,
`ngx.HTTP_MOVED_TEMPORARILY`, or other [HTTP status constants](http://wiki.nginx.org/HttpLuaModule#HTTP_status_constants).
To return an error page with custom contents, use code snippets like this:
ngx.status = ngx.HTTP_GONE
ngx.say("This is our own content")
-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
The effect in action:
$ curl -i http://localhost/test
HTTP/1.1 410 Gone
Server: nginx/1.0.6
Date: Thu, 15 Sep 2011 00:51:48 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
This is our own content
ngx.eof
-------
**syntax:** *ngx.eof()*

Просмотреть файл

@ -10,7 +10,7 @@ This module is under active development and is already production ready.
= Version =
This document describes ngx_lua [https://github.com/chaoslawful/lua-nginx-module/downloads v0.3.1rc2] released on 12 September 2011.
This document describes ngx_lua [https://github.com/chaoslawful/lua-nginx-module/downloads v0.3.1rc3] released on 13 September 2011.
= Synopsis =
<geshi lang="nginx">
@ -1484,14 +1484,36 @@ Force flushing the response outputs. This operation has no effect in HTTP 1.0 bu
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
When <code>status >= 200</code> (<code>ngx.HTTP_OK</code>), it will interrupt the execution of the current Lua thread and returns
status code to nginx.
When <code>status >= 200</code> (<code>ngx.HTTP_OK</code>), it will interrupt the execution of the current request and return status code to nginx.
When <code>status == 0</code> (<code>ngx.OK</code>), it will quits the current phase handler (or content handler if [[#content_by_lua|content_by_lua]] directives are used).
When <code>status == 0</code> (<code>ngx.OK</code>), it will quits the current phase handler (or content handler if the [[#content_by_lua|content_by_lua]] directive are used).
The <code>status</code> argument can be <code>ngx.OK</code>, <code>ngx.ERROR</code>, <code>ngx.HTTP_NOT_FOUND</code>,
<code>ngx.HTTP_MOVED_TEMPORARILY</code>, or other [[#HTTP status constants|HTTP status constants]].
To return an error page with custom contents, use code snippets like this:
<geshi lang="lua">
ngx.status = ngx.HTTP_GONE
ngx.say("This is our own content")
-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
</geshi>
The effect in action:
<geshi lang="bash">
$ curl -i http://localhost/test
HTTP/1.1 410 Gone
Server: nginx/1.0.6
Date: Thu, 15 Sep 2011 00:51:48 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
This is our own content
</geshi>
== ngx.eof ==
'''syntax:''' ''ngx.eof()''

Просмотреть файл

@ -561,3 +561,35 @@ nil
--- response_body
nil
=== TEST 29: override domains in the cookie
--- config
location /foo {
echo hello;
add_header Set-Cookie 'foo=bar; Domain=backend.int';
add_header Set-Cookie 'baz=bah; Domain=backend.int';
}
location /main {
proxy_pass http://127.0.0.1:$server_port/foo;
header_filter_by_lua '
local cookies = ngx.header.set_cookie
if not cookies then return end
if type(cookies) ~= "table" then cookies = {cookies} end
local newcookies = {}
for i, val in ipairs(cookies) do
local newval = string.gsub(val, "([dD]omain)=[%w_-\\\\.]+",
"%1=external.domain.com")
table.insert(newcookies, newval)
end
ngx.header.set_cookie = newcookies
';
}
--- request
GET /main
--- response_headers
Set-Cookie: foo=bar; Domain=external.domain.com, baz=bah; Domain=external.domain.com
--- response_body
hello

Просмотреть файл

@ -505,3 +505,26 @@ GET /lua
--- response_body
morning
=== TEST 16: error page with custom body
--- config
error_page 410 @err;
location @err {
echo blah blah;
}
location /foo {
rewrite_by_lua '
ngx.status = ngx.HTTP_GONE
ngx.say("This is our own content")
-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
';
echo Hello;
}
--- request
GET /foo
--- response_body
This is our own content
--- error_code: 410

Просмотреть файл

@ -485,3 +485,26 @@ GET /lua
--- response_body
morning
=== TEST 16: error page with custom body
--- config
error_page 410 @err;
location @err {
echo blah blah;
}
location /foo {
access_by_lua '
ngx.status = ngx.HTTP_GONE
ngx.say("This is our own content")
-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
';
echo Hello;
}
--- request
GET /foo
--- response_body
This is our own content
--- error_code: 410