Skip to content

Commit a16dda5

Browse files
committed
updated error log message and make it more readable
1 parent 3ff0ec7 commit a16dda5

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Table of Contents
1818
* [build as a dynamic module](#build-as-a-dynamic-module)
1919
* [build OpenResty](#build-openresty)
2020
* [Test Suite](#test-suite)
21+
* [Error Log](#error-log)
2122
* [Directive](#directive)
2223
* [proxy_connect](#proxy_connect)
2324
* [proxy_connect_allow](#proxy_connect_allow)
@@ -337,6 +338,18 @@ $ export TEST_NGINX_BINARY=/path/to/nginx/binary
337338
$ prove -v -I /path/to/nginx-tests/lib /path/to/ngx_http_proxy_connect_module/t/
338339
```
339340

341+
Error Log
342+
=========
343+
344+
This module logs its own error message beginning with `"proxy_connect:"` string.
345+
Some typical error logs are shown as following:
346+
347+
* The proxy_connect module tries to establish tunnel connection with backend server, but the TCP connection timeout occurs.
348+
349+
```
350+
2019/08/07 17:27:20 [error] 19257#0: *1 proxy_connect: upstream connect timed out (peer:216.58.200.4:443) while connecting to upstream, client: 127.0.0.1, server: , request: "CONNECT www.google.com:443 HTTP/1.1", host: "www.google.com:443"
351+
```
352+
340353
Variables
341354
=========
342355

ngx_http_proxy_connect_module.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ ngx_http_proxy_connect_test_connect(ngx_connection_t *c)
373373

374374
c->log->action = "connecting to upstream";
375375
(void) ngx_connection_error(c, err,
376-
"kevent() reported that connect() failed");
376+
"proxy_connet: upstream connect failed (kevent)");
377377
return NGX_ERROR;
378378
}
379379

@@ -395,8 +395,9 @@ ngx_http_proxy_connect_test_connect(ngx_connection_t *c)
395395
}
396396

397397
if (err) {
398-
c->log->action = "connecting to upstream (proxy_connect)";
399-
(void) ngx_connection_error(c, err, "connect() failed");
398+
c->log->action = "connecting to upstream";
399+
(void) ngx_connection_error(c, err,
400+
"proxy_connect: upstream connect failed");
400401
return NGX_ERROR;
401402
}
402403
}
@@ -740,7 +741,8 @@ ngx_http_proxy_connect_read_downstream(ngx_http_request_t *r)
740741

741742
if (r->connection->read->timedout) {
742743
r->connection->timedout = 1;
743-
ngx_connection_error(r->connection, NGX_ETIMEDOUT, "client timed out");
744+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
745+
"proxy_connect: client read timed out");
744746
ngx_http_proxy_connect_finalize_request(r, ctx->u,
745747
NGX_HTTP_REQUEST_TIME_OUT);
746748
return;
@@ -759,7 +761,8 @@ ngx_http_proxy_connect_write_downstream(ngx_http_request_t *r)
759761

760762
if (r->connection->write->timedout) {
761763
r->connection->timedout = 1;
762-
ngx_connection_error(r->connection, NGX_ETIMEDOUT, "client timed out");
764+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
765+
"proxy_connect: client write timed out");
763766
ngx_http_proxy_connect_finalize_request(r, ctx->u,
764767
NGX_HTTP_REQUEST_TIME_OUT);
765768
return;
@@ -784,7 +787,9 @@ ngx_http_proxy_connect_read_upstream(ngx_http_request_t *r,
784787
c = u->peer.connection;
785788

786789
if (c->read->timedout) {
787-
ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
790+
ngx_log_error(NGX_LOG_ERR, c->log, 0,
791+
"proxy_connect: upstream read timed out (peer:%V)",
792+
u->peer.name);
788793
ngx_http_proxy_connect_finalize_request(r, u, NGX_HTTP_GATEWAY_TIME_OUT);
789794
return;
790795
}
@@ -837,8 +842,9 @@ ngx_http_proxy_connect_write_upstream(ngx_http_request_t *r,
837842
"proxy_connect upstream write handler");
838843

839844
if (c->write->timedout) {
840-
ngx_connection_error(c, NGX_ETIMEDOUT,
841-
"upstream timed out (proxy_connect)");
845+
ngx_log_error(NGX_LOG_ERR, c->log, 0,
846+
"proxy_connect: upstream %s timed out (peer:%V)",
847+
u->connected ? "write" : "connect", u->peer.name);
842848
ngx_http_proxy_connect_finalize_request(r, u,
843849
NGX_HTTP_GATEWAY_TIME_OUT);
844850
return;
@@ -890,8 +896,8 @@ ngx_http_proxy_connect_send_handler(ngx_http_request_t *r)
890896

891897
if (c->write->timedout) {
892898
c->timedout = 1;
893-
ngx_connection_error(c, NGX_ETIMEDOUT,
894-
"client timed out (proxy_connect)");
899+
ngx_log_error(NGX_LOG_ERR, c->log, 0,
900+
"proxy_connect: client write timed out");
895901
ngx_http_proxy_connect_finalize_request(r, ctx->u,
896902
NGX_HTTP_REQUEST_TIME_OUT);
897903
return;
@@ -947,7 +953,7 @@ ngx_http_proxy_connect_process_connect(ngx_http_request_t *r,
947953

948954
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_connect_module);
949955

950-
r->connection->log->action = "connecting to upstream (proxy_connect)";
956+
r->connection->log->action = "connecting to upstream";
951957

952958
if (ngx_http_proxy_connect_set_local(r, u, u->conf->local) != NGX_OK) {
953959
ngx_http_proxy_connect_finalize_request(r, u, NGX_HTTP_INTERNAL_SERVER_ERROR);
@@ -975,13 +981,15 @@ ngx_http_proxy_connect_process_connect(ngx_http_request_t *r,
975981
}
976982

977983
if (rc == NGX_BUSY) {
978-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live connection");
984+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
985+
"proxy_connect: no live connection");
979986
ngx_http_proxy_connect_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);
980987
return;
981988
}
982989

983990
if (rc == NGX_DECLINED) {
984-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "connection error");
991+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
992+
"proxy_connect: connection error");
985993
ngx_http_proxy_connect_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);
986994
return;
987995
}
@@ -1046,7 +1054,7 @@ ngx_http_proxy_connect_resolve_handler(ngx_resolver_ctx_t *ctx)
10461054

10471055
if (ctx->state) {
10481056
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1049-
"%V could not be resolved (%i: %s)",
1057+
"proxy_connect: %V could not be resolved (%i: %s)",
10501058
&ctx->name, ctx->state,
10511059
ngx_resolver_strerror(ctx->state));
10521060

@@ -1403,12 +1411,13 @@ ngx_http_proxy_connect_handler(ngx_http_request_t *r)
14031411
url.no_resolve = 1;
14041412

14051413
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1406-
"connect handler: parse url: %V" , &url);
1414+
"connect handler: parse url: %V" , &url.url);
14071415

14081416
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
14091417
if (url.err) {
14101418
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1411-
"%s in connect host \"%V\"", url.err, &url.url);
1419+
"proxy_connect: %s in connect host \"%V\"",
1420+
url.err, &url.url);
14121421
return NGX_HTTP_FORBIDDEN;
14131422
}
14141423

@@ -1469,13 +1478,14 @@ ngx_http_proxy_connect_handler(ngx_http_request_t *r)
14691478
rctx = ngx_resolve_start(clcf->resolver, &temp);
14701479
if (rctx == NULL) {
14711480
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1472-
"failed to start the resolver");
1481+
"proxy_connect: failed to start the resolver");
14731482
return NGX_HTTP_INTERNAL_SERVER_ERROR;
14741483
}
14751484

14761485
if (rctx == NGX_NO_RESOLVER) {
14771486
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1478-
"no resolver defined to resolve %V", &r->connect_host);
1487+
"proxy_connect: no resolver defined to resolve %V",
1488+
&r->connect_host);
14791489
return NGX_HTTP_BAD_GATEWAY;
14801490
}
14811491

@@ -1797,7 +1807,7 @@ ngx_http_proxy_connect_set_local(ngx_http_request_t *r,
17971807

17981808
if (rc != NGX_OK) {
17991809
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1800-
"invalid local address \"%V\"", &val);
1810+
"proxy_connect: invalid local address \"%V\"", &val);
18011811
return NGX_OK;
18021812
}
18031813

@@ -1960,7 +1970,8 @@ ngx_http_proxy_connect_variable_set_time(ngx_http_request_t *r,
19601970

19611971
if (ms == (ngx_msec_t) NGX_ERROR) {
19621972
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1963-
"[proxy_connect] invalid msec \"%V\" (ctx offset=%ui)", &s, data);
1973+
"proxy_connect: invalid msec \"%V\" (ctx offset=%ui)",
1974+
&s, data);
19641975
return;
19651976
}
19661977

@@ -1969,7 +1980,7 @@ ngx_http_proxy_connect_variable_set_time(ngx_http_request_t *r,
19691980
if (ctx == NULL) {
19701981
#if 0
19711982
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1972-
"[proxy_connect] no ctx found");
1983+
"proxy_connect: no ctx found");
19731984
#endif
19741985
return;
19751986
}

t/http_proxy_connect_timeout.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ http {
8383
' c:$proxy_connect_connect_timeout,s:$proxy_connect_send_timeout,r:$proxy_connect_read_timeout';
8484
8585
access_log %%TESTDIR%%/connect.log connect;
86+
error_log %%TESTDIR%%/connect_error.log error;
8687
8788
resolver 127.0.0.1:18085 ipv6=off; # NOTE: cannot connect ipv6 address ::1 in mac os x.
8889
@@ -120,6 +121,13 @@ http {
120121
access_log off;
121122
root %%TESTDIR%%/;
122123
}
124+
125+
# used to output error.log
126+
location = /connect_error.log {
127+
access_log off;
128+
root %%TESTDIR%%/;
129+
}
130+
123131
}
124132
}
125133
@@ -144,12 +152,17 @@ if ($@) {
144152
#}
145153

146154
my $log;
155+
my $errlog;
147156

148157
TODO: {
149158
local $TODO = '# This case will pass, if connecting 8.8.8.8 timed out.';
150159
like(http_connect_request('test-connect-timeout.com', '8888', '/'), qr/504/, 'connect timed out: set $var');
151160
$log = http_get('/connect.log');
152-
like($log, qr/"CONNECT test-connect-timeout.com:8888 HTTP\/1.1" 504 .+ c:1,s:102,r:103/, 'connect timed out log: get $var & status=504');
161+
like($log, qr/"CONNECT test-connect-timeout.com:8888 HTTP\/1.1" 504 .+ c:1,s:102,r:103/,
162+
'connect timed out log: get $var & status=504');
163+
$errlog = http_get('/connect_error.log');
164+
like($errlog, qr/proxy_connect: upstream connect timed out \(peer:8\.8\.8\.8:8888\) while connecting to upstream/,
165+
'connect timed out error log');
153166
}
154167

155168
http_connect_request('test-read-timeout.com', '8888', '/');

0 commit comments

Comments
 (0)