Skip to content

Commit 9e5cc6e

Browse files
committed
bugfix: nginx rewrite directive may initiate internal redirects without clearing any module ctx and rewrite_by_lua* handlers might think it was re-entered and thus it might lead to request hang. thanks twistedfall for the report in #1235.
1 parent 90d7522 commit 9e5cc6e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/ngx_http_lua_rewriteby.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ ngx_http_lua_rewrite_handler(ngx_http_request_t *r)
126126

127127
return NGX_HTTP_OK;
128128
}
129+
130+
r->write_event_handler = ngx_http_core_run_phases;
131+
ctx->entered_rewrite_phase = 0;
132+
133+
return NGX_DECLINED;
129134
}
130135

131136
return rc;
@@ -363,6 +368,9 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
363368
return NGX_HTTP_OK;
364369
}
365370

371+
r->write_event_handler = ngx_http_core_run_phases;
372+
ctx->entered_rewrite_phase = 0;
373+
366374
return NGX_DECLINED;
367375
}
368376

t/023-rewrite/redirect.t

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,43 @@ GET /read
122122
--- raw_response_headers_like: Location: /foo\r\n
123123
--- response_body_like: 302 Found
124124
--- error_code: 302
125+
126+
127+
128+
=== TEST 7: internal redirects that do not clear module ctx
129+
--- http_config
130+
rewrite_by_lua_no_postpone on;
131+
--- config
132+
rewrite_by_lua_block {
133+
-- this is empty by intention
134+
}
135+
136+
location /url1 {
137+
rewrite ^ /url2;
138+
}
139+
--- request
140+
GET /url1
141+
--- response_body_like: 404 Not Found
142+
--- error_log eval
143+
qr{\[error\] .*?/html/url2".*?No such file or directory}
144+
--- error_code: 404
145+
146+
147+
148+
=== TEST 8: internal redirects that do not clear module ctx (yield in rewrite handler)
149+
--- http_config
150+
rewrite_by_lua_no_postpone on;
151+
--- config
152+
rewrite_by_lua_block {
153+
ngx.sleep(0.001)
154+
}
155+
156+
location /url1 {
157+
rewrite ^ /url2;
158+
}
159+
--- request
160+
GET /url1
161+
--- response_body_like: 404 Not Found
162+
--- error_log eval
163+
qr{\[error\] .*?/html/url2".*?No such file or directory}
164+
--- error_code: 404

0 commit comments

Comments
 (0)