Skip to content

Commit 2e18b38

Browse files
committed
Merge branch 'master' into ssl-cert-by-lua
2 parents 3dccace + 4388b1e commit 2e18b38

16 files changed

+200
-65
lines changed

README.markdown

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Production ready.
5757
Version
5858
=======
5959

60-
This document describes ngx_lua [v0.9.13](https://github.com/openresty/lua-nginx-module/tags) released on 21 November 2014.
60+
This document describes ngx_lua [v0.9.15](https://github.com/openresty/lua-nginx-module/tags) released on 18 February 2015.
6161

6262
Synopsis
6363
========
@@ -197,10 +197,11 @@ Synopsis
197197
}
198198

199199
# use nginx var in code path
200-
# WARN: contents in nginx var must be carefully filtered,
200+
# WARNING: contents in nginx var must be carefully filtered,
201201
# otherwise there'll be great security risk!
202-
location ~ ^/app/(.+) {
203-
content_by_lua_file /path/to/lua/app/root/$1.lua;
202+
location ~ ^/app/([-_a-zA-Z0-9/]+) {
203+
set $path $1;
204+
content_by_lua_file /path/to/lua/app/root/$path.lua;
204205
}
205206

206207
location / {
@@ -290,7 +291,7 @@ Nginx Compatibility
290291
===================
291292
The latest module is compatible with the following versions of Nginx:
292293

293-
* 1.7.x (last tested: 1.7.7)
294+
* 1.7.x (last tested: 1.7.10)
294295
* 1.6.x
295296
* 1.5.x (last tested: 1.5.12)
296297
* 1.4.x (last tested: 1.4.4)
@@ -306,7 +307,7 @@ The latest module is compatible with the following versions of Nginx:
306307
Installation
307308
============
308309

309-
The [ngx_openresty bundle](http://openresty.org) can be used to install Nginx, ngx_lua, either one of the standard Lua 5.1 interpreter or LuaJIT 2.0/2.1, as well as a package of powerful companion Nginx modules. The basic installation step is a simple `./configure --with-luajit && make && make install`.
310+
It is highly recommended to use the [ngx_openresty bundle](http://openresty.org) that bundles Nginx, ngx_lua, LuaJIT 2.0/2.1 (or the optional standard Lua 5.1 interpreter), as well as a package of powerful companion Nginx modules. The basic installation step is a simple command: `./configure --with-luajit && make && make install`.
310311

311312
Alternatively, ngx_lua can be manually compiled into Nginx:
312313

@@ -319,9 +320,9 @@ Build the source with this module:
319320

320321
```bash
321322

322-
wget 'http://nginx.org/download/nginx-1.7.7.tar.gz'
323-
tar -xzvf nginx-1.7.7.tar.gz
324-
cd nginx-1.7.7/
323+
wget 'http://nginx.org/download/nginx-1.7.10.tar.gz'
324+
tar -xzvf nginx-1.7.10.tar.gz
325+
cd nginx-1.7.10/
325326

326327
# tell nginx's build system where to find LuaJIT 2.0:
327328
export LUAJIT_LIB=/path/to/luajit/lib
@@ -337,6 +338,7 @@ Build the source with this module:
337338

338339
# Here we assume Nginx is to be installed under /opt/nginx/.
339340
./configure --prefix=/opt/nginx \
341+
--with-ld-opt='-Wl,-rpath,/path/to/luajit-or-lua/lib" \
340342
--add-module=/path/to/ngx_devel_kit \
341343
--add-module=/path/to/lua-nginx-module
342344

@@ -985,9 +987,9 @@ Copyright and License
985987

986988
This module is licensed under the BSD license.
987989

988-
Copyright (C) 2009-2014, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
990+
Copyright (C) 2009-2015, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
989991

990-
Copyright (C) 2009-2014, by Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, CloudFlare Inc.
992+
Copyright (C) 2009-2015, by Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, CloudFlare Inc.
991993

992994
All rights reserved.
993995

@@ -1470,6 +1472,20 @@ and the Nginx config must be reloaded each time the Lua source file is modified.
14701472
The Lua code cache can be temporarily disabled during development by
14711473
switching [lua_code_cache](#lua_code_cache) `off` in `nginx.conf` to avoid reloading Nginx.
14721474

1475+
Nginx variables are supported in the file path for dynamic dispatch, for example:
1476+
1477+
```nginx
1478+
1479+
# WARNING: contents in nginx var must be carefully filtered,
1480+
# otherwise there'll be great security risk!
1481+
location ~ ^/app/([-_a-zA-Z0-9/]+) {
1482+
set $path $1;
1483+
content_by_lua_file /path/to/lua/app/root/$path.lua;
1484+
}
1485+
```
1486+
1487+
But be very careful about malicious user inputs and always carefully validate or filter out the user-supplied path components.
1488+
14731489
[Back to TOC](#directives)
14741490

14751491
rewrite_by_lua
@@ -1615,6 +1631,8 @@ When the Lua code cache is turned on (by default), the user code is loaded once
16151631

16161632
The `rewrite_by_lua_file` code will always run at the end of the `rewrite` request-processing phase unless [rewrite_by_lua_no_postpone](#rewrite_by_lua_no_postpone) is turned on.
16171633

1634+
Nginx variables are supported in the file path for dynamic dispatch just as in [content_by_lua_file](#content_by_lua_file).
1635+
16181636
[Back to TOC](#directives)
16191637

16201638
access_by_lua
@@ -1709,6 +1727,8 @@ When the Lua code cache is turned on (by default), the user code is loaded once
17091727
and the Nginx config must be reloaded each time the Lua source file is modified.
17101728
The Lua code cache can be temporarily disabled during development by switching [lua_code_cache](#lua_code_cache) `off` in `nginx.conf` to avoid repeatedly reloading Nginx.
17111729

1730+
Nginx variables are supported in the file path for dynamic dispatch just as in [content_by_lua_file](#content_by_lua_file).
1731+
17121732
[Back to TOC](#directives)
17131733

17141734
header_filter_by_lua

doc/HttpLuaModule.wiki

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Production ready.
1010

1111
= Version =
1212

13-
This document describes ngx_lua [https://github.com/openresty/lua-nginx-module/tags v0.9.13] released on 21 November 2014.
13+
This document describes ngx_lua [https://github.com/openresty/lua-nginx-module/tags v0.9.15] released on 18 February 2015.
1414

1515
= Synopsis =
1616
<geshi lang="nginx">
@@ -148,10 +148,11 @@ This document describes ngx_lua [https://github.com/openresty/lua-nginx-module/t
148148
}
149149
150150
# use nginx var in code path
151-
# WARN: contents in nginx var must be carefully filtered,
151+
# WARNING: contents in nginx var must be carefully filtered,
152152
# otherwise there'll be great security risk!
153-
location ~ ^/app/(.+) {
154-
content_by_lua_file /path/to/lua/app/root/$1.lua;
153+
location ~ ^/app/([-_a-zA-Z0-9/]+) {
154+
set $path $1;
155+
content_by_lua_file /path/to/lua/app/root/$path.lua;
155156
}
156157
157158
location / {
@@ -232,7 +233,7 @@ The Lua state (Lua VM instance) is shared across all the requests handled by a s
232233
= Nginx Compatibility =
233234
The latest module is compatible with the following versions of Nginx:
234235

235-
* 1.7.x (last tested: 1.7.7)
236+
* 1.7.x (last tested: 1.7.10)
236237
* 1.6.x
237238
* 1.5.x (last tested: 1.5.12)
238239
* 1.4.x (last tested: 1.4.4)
@@ -245,7 +246,7 @@ The latest module is compatible with the following versions of Nginx:
245246
246247
= Installation =
247248

248-
The [http://openresty.org ngx_openresty bundle] can be used to install Nginx, ngx_lua, either one of the standard Lua 5.1 interpreter or LuaJIT 2.0/2.1, as well as a package of powerful companion Nginx modules. The basic installation step is a simple <code>./configure --with-luajit && make && make install</code>.
249+
It is highly recommended to use the [http://openresty.org ngx_openresty bundle] that bundles Nginx, ngx_lua, LuaJIT 2.0/2.1 (or the optional standard Lua 5.1 interpreter), as well as a package of powerful companion Nginx modules. The basic installation step is a simple command: <code>./configure --with-luajit && make && make install</code>.
249250

250251
Alternatively, ngx_lua can be manually compiled into Nginx:
251252

@@ -257,9 +258,9 @@ Alternatively, ngx_lua can be manually compiled into Nginx:
257258
Build the source with this module:
258259

259260
<geshi lang="bash">
260-
wget 'http://nginx.org/download/nginx-1.7.7.tar.gz'
261-
tar -xzvf nginx-1.7.7.tar.gz
262-
cd nginx-1.7.7/
261+
wget 'http://nginx.org/download/nginx-1.7.10.tar.gz'
262+
tar -xzvf nginx-1.7.10.tar.gz
263+
cd nginx-1.7.10/
263264

264265
# tell nginx's build system where to find LuaJIT 2.0:
265266
export LUAJIT_LIB=/path/to/luajit/lib
@@ -275,6 +276,7 @@ Build the source with this module:
275276
276277
# Here we assume Nginx is to be installed under /opt/nginx/.
277278
./configure --prefix=/opt/nginx \
279+
--with-ld-opt='-Wl,-rpath,/path/to/luajit-or-lua/lib" \
278280
--add-module=/path/to/ngx_devel_kit \
279281
--add-module=/path/to/lua-nginx-module
280282
@@ -818,9 +820,9 @@ There are also various testing modes based on mockeagain, valgrind, and etc. Ref
818820
819821
This module is licensed under the BSD license.
820822
821-
Copyright (C) 2009-2014, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
823+
Copyright (C) 2009-2015, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
822824
823-
Copyright (C) 2009-2014, by Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, CloudFlare Inc.
825+
Copyright (C) 2009-2015, by Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, CloudFlare Inc.
824826
825827
All rights reserved.
826828
@@ -1205,6 +1207,19 @@ and the Nginx config must be reloaded each time the Lua source file is modified.
12051207
The Lua code cache can be temporarily disabled during development by
12061208
switching [[#lua_code_cache|lua_code_cache]] <code>off</code> in <code>nginx.conf</code> to avoid reloading Nginx.
12071209
1210+
Nginx variables are supported in the file path for dynamic dispatch, for example:
1211+
1212+
<geshi lang="nginx">
1213+
# WARNING: contents in nginx var must be carefully filtered,
1214+
# otherwise there'll be great security risk!
1215+
location ~ ^/app/([-_a-zA-Z0-9/]+) {
1216+
set $path $1;
1217+
content_by_lua_file /path/to/lua/app/root/$path.lua;
1218+
}
1219+
</geshi>
1220+
1221+
But be very careful about malicious user inputs and always carefully validate or filter out the user-supplied path components.
1222+
12081223
== rewrite_by_lua ==
12091224
12101225
'''syntax:''' ''rewrite_by_lua <lua-script-str>''
@@ -1338,6 +1353,8 @@ When the Lua code cache is turned on (by default), the user code is loaded once
13381353
13391354
The <code>rewrite_by_lua_file</code> code will always run at the end of the <code>rewrite</code> request-processing phase unless [[#rewrite_by_lua_no_postpone|rewrite_by_lua_no_postpone]] is turned on.
13401355
1356+
Nginx variables are supported in the file path for dynamic dispatch just as in [[#content_by_lua_file|content_by_lua_file]].
1357+
13411358
== access_by_lua ==
13421359
13431360
'''syntax:''' ''access_by_lua <lua-script-str>''
@@ -1423,6 +1440,8 @@ When the Lua code cache is turned on (by default), the user code is loaded once
14231440
and the Nginx config must be reloaded each time the Lua source file is modified.
14241441
The Lua code cache can be temporarily disabled during development by switching [[#lua_code_cache|lua_code_cache]] <code>off</code> in <code>nginx.conf</code> to avoid repeatedly reloading Nginx.
14251442
1443+
Nginx variables are supported in the file path for dynamic dispatch just as in [[#content_by_lua_file|content_by_lua_file]].
1444+
14261445
== header_filter_by_lua ==
14271446
14281447
'''syntax:''' ''header_filter_by_lua <lua-script-str>''

src/api/ngx_http_lua_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/* Public API for other Nginx modules */
2020

2121

22-
#define ngx_http_lua_version 9014
22+
#define ngx_http_lua_version 9015
2323

2424

2525
typedef struct {

src/ngx_http_lua_common.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,6 @@ struct ngx_http_lua_co_ctx_s {
300300

301301
ngx_http_cleanup_pt cleanup;
302302

303-
unsigned nsubreqs; /* number of subrequests of the
304-
* current request */
305-
306303
ngx_int_t *sr_statuses; /* all capture subrequest statuses */
307304

308305
ngx_http_headers_out_t **sr_headers;
@@ -311,6 +308,9 @@ struct ngx_http_lua_co_ctx_s {
311308

312309
uint8_t *sr_flags;
313310

311+
unsigned nsubreqs; /* number of subrequests of the
312+
* current request */
313+
314314
unsigned pending_subreqs; /* number of subrequests being
315315
waited */
316316

@@ -378,8 +378,6 @@ typedef struct ngx_http_lua_ctx_s {
378378
unsigned flushing_coros; /* number of coroutines waiting on
379379
ngx.flush(true) */
380380

381-
int uthreads; /* number of active user threads */
382-
383381
ngx_chain_t *out; /* buffered output chain for HTTP 1.0 */
384382
ngx_chain_t *free_bufs;
385383
ngx_chain_t *busy_bufs;
@@ -406,6 +404,8 @@ typedef struct ngx_http_lua_ctx_s {
406404

407405
ngx_http_lua_posted_thread_t *posted_threads;
408406

407+
int uthreads; /* number of active user threads */
408+
409409
uint16_t context; /* the current running directive context
410410
(or running phase) for the current
411411
Lua chunk */

src/ngx_http_lua_control.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ ngx_http_lua_ngx_redirect(lua_State *L)
196196
u_char *p;
197197
u_char *uri;
198198
size_t len;
199+
ngx_table_elt_t *h;
199200
ngx_http_request_t *r;
200201

201202
n = lua_gettop(L);
@@ -247,23 +248,23 @@ ngx_http_lua_ngx_redirect(lua_State *L)
247248

248249
ngx_memcpy(uri, p, len);
249250

250-
r->headers_out.location = ngx_list_push(&r->headers_out.headers);
251-
if (r->headers_out.location == NULL) {
251+
h = ngx_list_push(&r->headers_out.headers);
252+
if (h == NULL) {
252253
return luaL_error(L, "no memory");
253254
}
254255

255-
r->headers_out.location->hash = ngx_http_lua_location_hash;
256+
h->hash = ngx_http_lua_location_hash;
256257

257258
#if 0
258259
dd("location hash: %lu == %lu",
259-
(unsigned long) r->headers_out.location->hash,
260+
(unsigned long) h->hash,
260261
(unsigned long) ngx_hash_key_lc((u_char *) "Location",
261262
sizeof("Location") - 1));
262263
#endif
263264

264-
r->headers_out.location->value.len = len;
265-
r->headers_out.location->value.data = uri;
266-
ngx_str_set(&r->headers_out.location->key, "Location");
265+
h->value.len = len;
266+
h->value.data = uri;
267+
ngx_str_set(&h->key, "Location");
267268

268269
r->headers_out.status = rc;
269270

@@ -272,7 +273,16 @@ ngx_http_lua_ngx_redirect(lua_State *L)
272273

273274
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
274275
"lua redirect to \"%V\" with code %i",
275-
&r->headers_out.location->value, ctx->exit_code);
276+
&h->value, ctx->exit_code);
277+
278+
if (len && uri[0] != '/') {
279+
r->headers_out.location = h;
280+
}
281+
282+
/*
283+
* we do not set r->headers_out.location here to avoid the handling
284+
* the local redirects without a host name by ngx_http_header_filter()
285+
*/
276286

277287
return lua_yield(L, 0);
278288
}

src/ngx_http_lua_headerfilterby.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
2929

3030

31-
/* light user data key for the "ngx" table in the Lua VM regsitry */
32-
static char ngx_http_lua_headerfilterby_ngx_key;
33-
34-
3531
/**
3632
* Set environment table for the given code closure.
3733
*
@@ -64,12 +60,6 @@ ngx_http_lua_header_filter_by_lua_env(lua_State *L, ngx_http_request_t *r)
6460
* */
6561
ngx_http_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */);
6662

67-
/* {{{ initialize ngx.* namespace */
68-
lua_pushlightuserdata(L, &ngx_http_lua_headerfilterby_ngx_key);
69-
lua_rawget(L, LUA_REGISTRYINDEX);
70-
lua_setfield(L, -2, "ngx");
71-
/* }}} */
72-
7363
/* {{{ make new env inheriting main thread's globals table */
7464
lua_createtable(L, 0, 1 /* nrec */); /* the metatable for the new env */
7565
ngx_http_lua_get_globals_table(L);
@@ -141,6 +131,11 @@ ngx_http_lua_header_filter_by_chunk(lua_State *L, ngx_http_request_t *r)
141131
dd("exited: %d, exit code: %d, old exit code: %d",
142132
(int) ctx->exited, (int) ctx->exit_code, (int) old_exit_code);
143133

134+
#if 1
135+
/* clear Lua stack */
136+
lua_settop(L, 0);
137+
#endif
138+
144139
if (ctx->exited && ctx->exit_code != old_exit_code) {
145140
if (ctx->exit_code == NGX_ERROR) {
146141
return NGX_ERROR;
@@ -157,9 +152,6 @@ ngx_http_lua_header_filter_by_chunk(lua_State *L, ngx_http_request_t *r)
157152
return NGX_DECLINED;
158153
}
159154

160-
/* clear Lua stack */
161-
lua_settop(L, 0);
162-
163155
return NGX_OK;
164156
}
165157

0 commit comments

Comments
 (0)