forked from nulab/nginx-too-many-requests-retryafter-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx-1.11.x-too-many-requests-retryafter.patch
116 lines (100 loc) · 5.03 KB
/
nginx-1.11.x-too-many-requests-retryafter.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
diff -Bbu -ur nginx-1.11.0/src/http/modules/ngx_http_limit_req_module.c nginx-1.11.0-retry-after-pached/src/http/modules/ngx_http_limit_req_module.c
--- nginx-1.11.0/src/http/modules/ngx_http_limit_req_module.c 2016-05-24 11:54:42.000000000 -0400
+++ nginx-1.11.0-retry-after-pached/src/http/modules/ngx_http_limit_req_module.c 2016-05-31 10:04:59.000000000 -0400
@@ -164,6 +164,9 @@
ngx_http_limit_req_ctx_t *ctx;
ngx_http_limit_req_conf_t *lrcf;
ngx_http_limit_req_limit_t *limit, *limits;
+ ngx_table_elt_t *h;
+ ngx_uint_t rate;
+ u_char *ra;
if (r->main->limit_req_set) {
return NGX_DECLINED;
@@ -173,6 +176,7 @@
limits = lrcf->limits.elts;
excess = 0;
+ rate = 0;
rc = NGX_DECLINED;
@@ -216,6 +220,7 @@
n, rc, excess / 1000, excess % 1000);
if (rc != NGX_AGAIN) {
+ rate = ctx->rate;
break;
}
}
@@ -233,6 +238,17 @@
"limiting requests, excess: %ui.%03ui by zone \"%V\"",
excess / 1000, excess % 1000,
&limit->shm_zone->shm.name);
+
+ if ( rate > 0 && (ra = ngx_palloc(r->pool, sizeof("1000"))) != NULL){
+ h = ngx_list_push(&r->headers_out.headers);
+ if ( h != NULL ){
+ h->hash = 1;
+ ngx_str_set(&h->key, "Retry-After");
+ ngx_sprintf(ra, "%ui", (rate >= 1000) ? 1 : 1000/rate);
+ h->value.len = ngx_strlen(ra);
+ h->value.data = ra;
+ }
+ }
}
while (n--) {
@@ -720,7 +736,7 @@
NGX_LOG_INFO : conf->limit_log_level + 1;
ngx_conf_merge_uint_value(conf->status_code, prev->status_code,
- NGX_HTTP_SERVICE_UNAVAILABLE);
+ NGX_HTTP_TOO_MANY_REQUESTS);
return NGX_CONF_OK;
}
Only in nginx-1.11.0-retry-after-pached/src/http/modules: ngx_http_limit_req_module.c.orig
diff -Bbu -ur nginx-1.11.0/src/http/ngx_http_header_filter_module.c nginx-1.11.0-retry-after-pached/src/http/ngx_http_header_filter_module.c
--- nginx-1.11.0/src/http/ngx_http_header_filter_module.c 2016-05-24 11:54:43.000000000 -0400
+++ nginx-1.11.0-retry-after-pached/src/http/ngx_http_header_filter_module.c 2016-05-31 10:12:03.000000000 -0400
@@ -101,11 +101,18 @@
ngx_null_string, /* "420 unused" */
ngx_string("421 Misdirected Request"),
- /* ngx_null_string, */ /* "422 Unprocessable Entity" */
- /* ngx_null_string, */ /* "423 Locked" */
- /* ngx_null_string, */ /* "424 Failed Dependency" */
+ ngx_null_string, /* "422 Unprocessable Entity" */
+ ngx_null_string, /* "423 Locked" */
+ ngx_null_string, /* "424 Failed Dependency" */
+ ngx_null_string, /* "425 No code" */
+ ngx_null_string, /* "426 Upgrade Required" */
+ ngx_null_string, /* "427 unused" */
+ ngx_null_string, /* "428 Precondition Required" */
+ ngx_string("429 Too Many Requests"),
+ /* ngx_null_string, */ /* "430 unused" */
+ /* ngx_null_string, */ /* "431 Request Header Fields Too Large" */
-#define NGX_HTTP_LAST_4XX 422
+#define NGX_HTTP_LAST_4XX 430
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
ngx_string("500 Internal Server Error"),
diff -Bbu -ur nginx-1.11.0/src/http/ngx_http_request.h nginx-1.11.0-retry-after-pached/src/http/ngx_http_request.h
--- nginx-1.11.0/src/http/ngx_http_request.h 2016-05-24 11:54:43.000000000 -0400
+++ nginx-1.11.0-retry-after-pached/src/http/ngx_http_request.h 2016-05-31 10:04:59.000000000 -0400
@@ -97,6 +97,7 @@
#define NGX_HTTP_RANGE_NOT_SATISFIABLE 416
#define NGX_HTTP_MISDIRECTED_REQUEST 421
+#define NGX_HTTP_TOO_MANY_REQUESTS 429
/* Our own HTTP codes */
Only in nginx-1.11.0-retry-after-pached/src/http: ngx_http_request.h.orig
diff -Bbu -ur nginx-1.11.0/src/http/ngx_http_special_response.c nginx-1.11.0-retry-after-pached/src/http/ngx_http_special_response.c
--- nginx-1.11.0/src/http/ngx_http_special_response.c 2016-05-24 11:54:43.000000000 -0400
+++ nginx-1.11.0-retry-after-pached/src/http/ngx_http_special_response.c 2016-05-31 10:14:15.000000000 -0400
@@ -347,8 +347,16 @@
ngx_null_string, /* 419 */
ngx_null_string, /* 420 */
ngx_string(ngx_http_error_421_page),
+ ngx_null_string, /* 422 */
+ ngx_null_string, /* 423 */
+ ngx_null_string, /* 424 */
+ ngx_null_string, /* 425 */
+ ngx_null_string, /* 426 */
+ ngx_null_string, /* 427 */
+ ngx_null_string, /* 428 */
+ ngx_null_string, /* 429 */
-#define NGX_HTTP_LAST_4XX 422
+#define NGX_HTTP_LAST_4XX 430
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
ngx_string(ngx_http_error_494_page), /* 494, request header too large */