Skip to content

Commit 97425f8

Browse files
committed
bugfix: ngx.req.set_header/ngx.req.clear_header did not update the special field r->headers_in.x_real_ip when the ngx_realip module was enabled. thanks Matthieu Tourne for the patch.
1 parent a07df61 commit 97425f8

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/ngx_http_lua_headers_in.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
9999
offsetof(ngx_http_headers_in_t, content_length),
100100
ngx_http_set_content_length_header },
101101

102+
#if (NGX_HTTP_REALIP)
103+
{ ngx_string("X-Real-IP"),
104+
offsetof(ngx_http_headers_in_t, x_real_ip),
105+
ngx_http_set_builtin_header },
106+
#endif
107+
102108
{ ngx_null_string, 0, ngx_http_set_header }
103109
};
104110

t/028-req-header.t

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Test::Nginx::Socket;
99

1010
repeat_each(2);
1111

12-
plan tests => (2 * blocks() + 6) * repeat_each();
12+
plan tests => repeat_each() * (2 * blocks() + 10);
1313

1414
#no_diff();
1515
no_long_string();
@@ -1064,3 +1064,94 @@ Connection: Close
10641064
--- no_error_log
10651065
[error]
10661066
1067+
1068+
1069+
=== TEST 35: clear X-Real-IP
1070+
--- config
1071+
location /t {
1072+
rewrite_by_lua '
1073+
ngx.req.set_header("X-Real-IP", nil)
1074+
';
1075+
echo "X-Real-IP: $http_x_real_ip";
1076+
}
1077+
--- request
1078+
GET /t
1079+
--- more_headers
1080+
X-Real-IP: 8.8.8.8
1081+
1082+
--- stap
1083+
F(ngx_http_lua_rewrite_by_chunk) {
1084+
if (@defined($r->headers_in->x_real_ip) && $r->headers_in->x_real_ip) {
1085+
printf("rewrite: x-real-ip: %s\n",
1086+
user_string_n($r->headers_in->x_real_ip->value->data,
1087+
$r->headers_in->x_real_ip->value->len))
1088+
} else {
1089+
println("rewrite: no x-real-ip")
1090+
}
1091+
}
1092+
1093+
F(ngx_http_core_content_phase) {
1094+
if (@defined($r->headers_in->x_real_ip) && $r->headers_in->x_real_ip) {
1095+
printf("content: x-real-ip: %s\n",
1096+
user_string_n($r->headers_in->x_real_ip->value->data,
1097+
$r->headers_in->x_real_ip->value->len))
1098+
} else {
1099+
println("content: no x-real-ip")
1100+
}
1101+
}
1102+
1103+
--- stap_out
1104+
rewrite: x-real-ip: 8.8.8.8
1105+
content: no x-real-ip
1106+
1107+
--- response_body
1108+
X-Real-IP:
1109+
1110+
--- no_error_log
1111+
[error]
1112+
1113+
1114+
1115+
=== TEST 36: set custom X-Real-IP
1116+
--- config
1117+
location /t {
1118+
rewrite_by_lua '
1119+
ngx.req.set_header("X-Real-IP", "8.8.4.4")
1120+
';
1121+
echo "X-Real-IP: $http_x_real_ip";
1122+
}
1123+
--- request
1124+
GET /t
1125+
1126+
--- stap
1127+
F(ngx_http_lua_rewrite_by_chunk) {
1128+
if (@defined($r->headers_in->x_real_ip) && $r->headers_in->x_real_ip) {
1129+
printf("rewrite: x-real-ip: %s\n",
1130+
user_string_n($r->headers_in->x_real_ip->value->data,
1131+
$r->headers_in->x_real_ip->value->len))
1132+
} else {
1133+
println("rewrite: no x-real-ip")
1134+
}
1135+
1136+
}
1137+
1138+
F(ngx_http_core_content_phase) {
1139+
if (@defined($r->headers_in->x_real_ip) && $r->headers_in->x_real_ip) {
1140+
printf("content: x-real-ip: %s\n",
1141+
user_string_n($r->headers_in->x_real_ip->value->data,
1142+
$r->headers_in->x_real_ip->value->len))
1143+
} else {
1144+
println("content: no x-real-ip")
1145+
}
1146+
}
1147+
1148+
--- stap_out
1149+
rewrite: no x-real-ip
1150+
content: x-real-ip: 8.8.4.4
1151+
1152+
--- response_body
1153+
X-Real-IP: 8.8.4.4
1154+
1155+
--- no_error_log
1156+
[error]
1157+

0 commit comments

Comments
 (0)