nginxtra 1.6.3.9 → 1.8.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/nginxtra +1 -1
- data/bin/nginxtra_rails +1 -1
- data/lib/nginxtra/version.rb +1 -1
- data/vendor/nginx/CHANGES +358 -14
- data/vendor/nginx/CHANGES.ru +372 -18
- data/vendor/nginx/LICENSE +2 -2
- data/vendor/nginx/auto/cc/clang +5 -0
- data/vendor/nginx/auto/cc/gcc +5 -0
- data/vendor/nginx/auto/lib/google-perftools/conf +1 -1
- data/vendor/nginx/auto/lib/openssl/make +0 -5
- data/vendor/nginx/auto/lib/perl/conf +9 -1
- data/vendor/nginx/auto/make +1 -1
- data/vendor/nginx/auto/modules +11 -0
- data/vendor/nginx/auto/options +10 -2
- data/vendor/nginx/auto/os/darwin +0 -1
- data/vendor/nginx/auto/os/freebsd +6 -23
- data/vendor/nginx/auto/sources +16 -14
- data/vendor/nginx/auto/summary +3 -24
- data/vendor/nginx/auto/threads +20 -0
- data/vendor/nginx/auto/types/sizeof +2 -12
- data/vendor/nginx/auto/unix +50 -6
- data/vendor/nginx/configure +5 -0
- data/vendor/nginx/contrib/vim/syntax/nginx.vim +183 -50
- data/vendor/nginx/src/core/nginx.c +21 -9
- data/vendor/nginx/src/core/nginx.h +8 -2
- data/vendor/nginx/src/core/ngx_buf.c +88 -0
- data/vendor/nginx/src/core/ngx_buf.h +15 -1
- data/vendor/nginx/src/core/ngx_conf_file.c +4 -1
- data/vendor/nginx/src/core/ngx_connection.c +25 -66
- data/vendor/nginx/src/core/ngx_connection.h +1 -3
- data/vendor/nginx/src/core/ngx_core.h +11 -3
- data/vendor/nginx/src/core/ngx_crypt.c +1 -1
- data/vendor/nginx/src/core/ngx_cycle.c +7 -1
- data/vendor/nginx/src/core/ngx_cycle.h +6 -2
- data/vendor/nginx/src/core/ngx_file.c +13 -5
- data/vendor/nginx/src/core/ngx_file.h +6 -0
- data/vendor/nginx/src/core/ngx_log.c +215 -21
- data/vendor/nginx/src/core/ngx_log.h +9 -1
- data/vendor/nginx/src/core/ngx_output_chain.c +104 -15
- data/vendor/nginx/src/core/ngx_palloc.c +3 -7
- data/vendor/nginx/src/core/ngx_rbtree.c +2 -4
- data/vendor/nginx/src/core/ngx_rbtree.h +2 -4
- data/vendor/nginx/src/core/ngx_regex.c +14 -6
- data/vendor/nginx/src/core/ngx_resolver.c +16 -23
- data/vendor/nginx/src/core/ngx_resolver.h +8 -7
- data/vendor/nginx/src/core/ngx_shmtx.c +1 -1
- data/vendor/nginx/src/core/ngx_slab.c +89 -2
- data/vendor/nginx/src/core/ngx_slab.h +3 -0
- data/vendor/nginx/src/core/ngx_string.c +58 -2
- data/vendor/nginx/src/core/ngx_string.h +1 -0
- data/vendor/nginx/src/core/ngx_syslog.c +374 -0
- data/vendor/nginx/src/core/ngx_syslog.h +30 -0
- data/vendor/nginx/src/core/ngx_thread_pool.c +630 -0
- data/vendor/nginx/src/core/ngx_thread_pool.h +36 -0
- data/vendor/nginx/src/core/ngx_times.c +19 -2
- data/vendor/nginx/src/core/ngx_times.h +1 -0
- data/vendor/nginx/src/event/modules/ngx_aio_module.c +1 -1
- data/vendor/nginx/src/event/modules/ngx_devpoll_module.c +9 -24
- data/vendor/nginx/src/event/modules/ngx_epoll_module.c +152 -28
- data/vendor/nginx/src/event/modules/ngx_eventport_module.c +43 -25
- data/vendor/nginx/src/event/modules/ngx_kqueue_module.c +86 -156
- data/vendor/nginx/src/event/modules/ngx_poll_module.c +21 -37
- data/vendor/nginx/src/event/modules/ngx_rtsig_module.c +15 -27
- data/vendor/nginx/src/event/modules/ngx_select_module.c +10 -12
- data/vendor/nginx/src/event/modules/ngx_win32_select_module.c +7 -9
- data/vendor/nginx/src/event/ngx_event.c +5 -33
- data/vendor/nginx/src/event/ngx_event.h +15 -50
- data/vendor/nginx/src/event/ngx_event_accept.c +11 -10
- data/vendor/nginx/src/event/ngx_event_connect.c +0 -11
- data/vendor/nginx/src/event/ngx_event_connect.h +1 -4
- data/vendor/nginx/src/event/ngx_event_openssl.c +622 -38
- data/vendor/nginx/src/event/ngx_event_openssl.h +20 -2
- data/vendor/nginx/src/event/ngx_event_openssl_stapling.c +5 -1
- data/vendor/nginx/src/event/ngx_event_pipe.c +45 -19
- data/vendor/nginx/src/event/ngx_event_pipe.h +3 -0
- data/vendor/nginx/src/event/ngx_event_posted.c +7 -145
- data/vendor/nginx/src/event/ngx_event_posted.h +12 -39
- data/vendor/nginx/src/event/ngx_event_timer.c +50 -70
- data/vendor/nginx/src/event/ngx_event_timer.h +2 -14
- data/vendor/nginx/src/http/modules/ngx_http_addition_filter_module.c +1 -1
- data/vendor/nginx/src/http/modules/ngx_http_autoindex_module.c +416 -71
- data/vendor/nginx/src/http/modules/ngx_http_charset_filter_module.c +19 -15
- data/vendor/nginx/src/http/modules/ngx_http_dav_module.c +16 -4
- data/vendor/nginx/src/http/modules/ngx_http_fastcgi_module.c +601 -134
- data/vendor/nginx/src/http/modules/ngx_http_geo_module.c +1 -1
- data/vendor/nginx/src/http/modules/ngx_http_geoip_module.c +9 -3
- data/vendor/nginx/src/http/modules/ngx_http_gunzip_filter_module.c +9 -3
- data/vendor/nginx/src/http/modules/ngx_http_gzip_filter_module.c +9 -3
- data/vendor/nginx/src/http/modules/ngx_http_gzip_static_module.c +0 -2
- data/vendor/nginx/src/http/modules/ngx_http_headers_filter_module.c +197 -91
- data/vendor/nginx/src/http/modules/ngx_http_image_filter_module.c +1 -0
- data/vendor/nginx/src/http/modules/ngx_http_limit_conn_module.c +65 -162
- data/vendor/nginx/src/http/modules/ngx_http_limit_req_module.c +53 -67
- data/vendor/nginx/src/http/modules/ngx_http_log_module.c +128 -23
- data/vendor/nginx/src/http/modules/ngx_http_memcached_module.c +25 -6
- data/vendor/nginx/src/http/modules/ngx_http_mp4_module.c +1 -1
- data/vendor/nginx/src/http/modules/ngx_http_not_modified_filter_module.c +39 -13
- data/vendor/nginx/src/http/modules/ngx_http_proxy_module.c +697 -141
- data/vendor/nginx/src/http/modules/ngx_http_rewrite_module.c +5 -1
- data/vendor/nginx/src/http/modules/ngx_http_scgi_module.c +282 -125
- data/vendor/nginx/src/http/modules/ngx_http_ssi_filter_module.c +4 -1
- data/vendor/nginx/src/http/modules/ngx_http_ssl_module.c +44 -1
- data/vendor/nginx/src/http/modules/ngx_http_ssl_module.h +2 -0
- data/vendor/nginx/src/http/modules/ngx_http_stub_status_module.c +10 -8
- data/vendor/nginx/src/http/modules/ngx_http_sub_filter_module.c +18 -3
- data/vendor/nginx/src/http/modules/ngx_http_upstream_hash_module.c +641 -0
- data/vendor/nginx/src/http/modules/ngx_http_upstream_ip_hash_module.c +1 -1
- data/vendor/nginx/src/http/modules/ngx_http_upstream_keepalive_module.c +3 -21
- data/vendor/nginx/src/http/modules/ngx_http_upstream_least_conn_module.c +0 -5
- data/vendor/nginx/src/http/modules/ngx_http_uwsgi_module.c +449 -125
- data/vendor/nginx/src/http/modules/ngx_http_xslt_filter_module.c +4 -2
- data/vendor/nginx/src/http/modules/perl/ngx_http_perl_module.c +2 -1
- data/vendor/nginx/src/http/ngx_http.c +10 -5
- data/vendor/nginx/src/http/ngx_http.h +4 -4
- data/vendor/nginx/src/http/ngx_http_cache.h +26 -1
- data/vendor/nginx/src/http/ngx_http_copy_filter_module.c +109 -68
- data/vendor/nginx/src/http/ngx_http_core_module.c +191 -46
- data/vendor/nginx/src/http/ngx_http_core_module.h +16 -4
- data/vendor/nginx/src/http/ngx_http_file_cache.c +584 -67
- data/vendor/nginx/src/http/ngx_http_parse.c +55 -4
- data/vendor/nginx/src/http/ngx_http_request.c +14 -6
- data/vendor/nginx/src/http/ngx_http_request.h +12 -4
- data/vendor/nginx/src/http/ngx_http_request_body.c +114 -28
- data/vendor/nginx/src/http/ngx_http_spdy.c +383 -229
- data/vendor/nginx/src/http/ngx_http_spdy.h +8 -5
- data/vendor/nginx/src/http/ngx_http_spdy_filter_module.c +12 -4
- data/vendor/nginx/src/http/ngx_http_special_response.c +2 -2
- data/vendor/nginx/src/http/ngx_http_upstream.c +808 -132
- data/vendor/nginx/src/http/ngx_http_upstream.h +33 -3
- data/vendor/nginx/src/http/ngx_http_upstream_round_robin.c +72 -65
- data/vendor/nginx/src/http/ngx_http_upstream_round_robin.h +1 -2
- data/vendor/nginx/src/http/ngx_http_variables.c +47 -3
- data/vendor/nginx/src/http/ngx_http_write_filter_module.c +15 -6
- data/vendor/nginx/src/mail/ngx_mail.c +2 -3
- data/vendor/nginx/src/mail/ngx_mail.h +2 -0
- data/vendor/nginx/src/mail/ngx_mail_auth_http_module.c +140 -11
- data/vendor/nginx/src/mail/ngx_mail_core_module.c +3 -3
- data/vendor/nginx/src/mail/ngx_mail_handler.c +79 -2
- data/vendor/nginx/src/mail/ngx_mail_imap_module.c +3 -1
- data/vendor/nginx/src/mail/ngx_mail_pop3_module.c +3 -1
- data/vendor/nginx/src/mail/ngx_mail_smtp_module.c +3 -1
- data/vendor/nginx/src/mail/ngx_mail_ssl_module.c +125 -1
- data/vendor/nginx/src/mail/ngx_mail_ssl_module.h +8 -0
- data/vendor/nginx/src/misc/ngx_cpp_test_module.cpp +1 -1
- data/vendor/nginx/src/os/unix/ngx_aio_read_chain.c +1 -1
- data/vendor/nginx/src/os/unix/ngx_channel.c +0 -7
- data/vendor/nginx/src/os/unix/ngx_darwin_config.h +0 -3
- data/vendor/nginx/src/os/unix/ngx_darwin_sendfile_chain.c +44 -208
- data/vendor/nginx/src/os/unix/ngx_file_aio_read.c +25 -17
- data/vendor/nginx/src/os/unix/ngx_files.c +109 -0
- data/vendor/nginx/src/os/unix/ngx_files.h +6 -0
- data/vendor/nginx/src/os/unix/ngx_freebsd_config.h +0 -6
- data/vendor/nginx/src/os/unix/ngx_freebsd_sendfile_chain.c +78 -206
- data/vendor/nginx/src/os/unix/ngx_linux_aio_read.c +25 -14
- data/vendor/nginx/src/os/unix/ngx_linux_config.h +4 -1
- data/vendor/nginx/src/os/unix/ngx_linux_sendfile_chain.c +235 -194
- data/vendor/nginx/src/os/unix/ngx_os.h +25 -3
- data/vendor/nginx/src/os/unix/ngx_posix_init.c +4 -2
- data/vendor/nginx/src/os/unix/ngx_process_cycle.c +13 -195
- data/vendor/nginx/src/os/unix/ngx_process_cycle.h +0 -1
- data/vendor/nginx/src/os/unix/ngx_readv_chain.c +27 -108
- data/vendor/nginx/src/os/unix/ngx_setproctitle.h +2 -2
- data/vendor/nginx/src/os/unix/ngx_solaris_sendfilev_chain.c +12 -67
- data/vendor/nginx/src/os/unix/ngx_thread.h +26 -83
- data/vendor/nginx/src/os/unix/ngx_thread_cond.c +87 -0
- data/vendor/nginx/src/os/unix/ngx_thread_id.c +70 -0
- data/vendor/nginx/src/os/unix/ngx_thread_mutex.c +174 -0
- data/vendor/nginx/src/os/unix/ngx_user.c +2 -20
- data/vendor/nginx/src/os/unix/ngx_writev_chain.c +129 -98
- metadata +16 -17
- data/vendor/nginx/auto/lib/zlib/patch.zlib.h +0 -10
- data/vendor/nginx/src/event/ngx_event_busy_lock.c +0 -286
- data/vendor/nginx/src/event/ngx_event_busy_lock.h +0 -65
- data/vendor/nginx/src/event/ngx_event_mutex.c +0 -70
- data/vendor/nginx/src/http/ngx_http_busy_lock.c +0 -307
- data/vendor/nginx/src/http/ngx_http_busy_lock.h +0 -54
- data/vendor/nginx/src/os/unix/ngx_freebsd_rfork_thread.c +0 -756
- data/vendor/nginx/src/os/unix/ngx_freebsd_rfork_thread.h +0 -122
- data/vendor/nginx/src/os/unix/ngx_pthread_thread.c +0 -278
- data/vendor/nginx/src/os/unix/rfork_thread.S +0 -73
@@ -369,10 +369,13 @@ ngx_http_ssi_header_filter(ngx_http_request_t *r)
|
|
369
369
|
if (r == r->main) {
|
370
370
|
ngx_http_clear_content_length(r);
|
371
371
|
ngx_http_clear_accept_ranges(r);
|
372
|
-
ngx_http_clear_etag(r);
|
373
372
|
|
374
373
|
if (!slcf->last_modified) {
|
375
374
|
ngx_http_clear_last_modified(r);
|
375
|
+
ngx_http_clear_etag(r);
|
376
|
+
|
377
|
+
} else {
|
378
|
+
ngx_http_weak_etag(r);
|
376
379
|
}
|
377
380
|
}
|
378
381
|
|
@@ -43,6 +43,8 @@ static char *ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf,
|
|
43
43
|
|
44
44
|
static char *ngx_http_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd,
|
45
45
|
void *conf);
|
46
|
+
static char *ngx_http_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd,
|
47
|
+
void *conf);
|
46
48
|
static char *ngx_http_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
47
49
|
void *conf);
|
48
50
|
|
@@ -91,6 +93,13 @@ static ngx_command_t ngx_http_ssl_commands[] = {
|
|
91
93
|
offsetof(ngx_http_ssl_srv_conf_t, certificate_key),
|
92
94
|
NULL },
|
93
95
|
|
96
|
+
{ ngx_string("ssl_password_file"),
|
97
|
+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
98
|
+
ngx_http_ssl_password_file,
|
99
|
+
NGX_HTTP_SRV_CONF_OFFSET,
|
100
|
+
0,
|
101
|
+
NULL },
|
102
|
+
|
94
103
|
{ ngx_string("ssl_dhparam"),
|
95
104
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
96
105
|
ngx_conf_set_str_slot,
|
@@ -273,6 +282,9 @@ static ngx_http_variable_t ngx_http_ssl_vars[] = {
|
|
273
282
|
{ ngx_string("ssl_session_reused"), NULL, ngx_http_ssl_variable,
|
274
283
|
(uintptr_t) ngx_ssl_get_session_reused, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
275
284
|
|
285
|
+
{ ngx_string("ssl_server_name"), NULL, ngx_http_ssl_variable,
|
286
|
+
(uintptr_t) ngx_ssl_get_server_name, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
287
|
+
|
276
288
|
{ ngx_string("ssl_client_cert"), NULL, ngx_http_ssl_variable,
|
277
289
|
(uintptr_t) ngx_ssl_get_certificate, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
278
290
|
|
@@ -289,6 +301,9 @@ static ngx_http_variable_t ngx_http_ssl_vars[] = {
|
|
289
301
|
{ ngx_string("ssl_client_serial"), NULL, ngx_http_ssl_variable,
|
290
302
|
(uintptr_t) ngx_ssl_get_serial_number, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
291
303
|
|
304
|
+
{ ngx_string("ssl_client_fingerprint"), NULL, ngx_http_ssl_variable,
|
305
|
+
(uintptr_t) ngx_ssl_get_fingerprint, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
306
|
+
|
292
307
|
{ ngx_string("ssl_client_verify"), NULL, ngx_http_ssl_variable,
|
293
308
|
(uintptr_t) ngx_ssl_get_client_verify, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
294
309
|
|
@@ -508,6 +523,7 @@ ngx_http_ssl_create_srv_conf(ngx_conf_t *cf)
|
|
508
523
|
sscf->buffer_size = NGX_CONF_UNSET_SIZE;
|
509
524
|
sscf->verify = NGX_CONF_UNSET_UINT;
|
510
525
|
sscf->verify_depth = NGX_CONF_UNSET_UINT;
|
526
|
+
sscf->passwords = NGX_CONF_UNSET_PTR;
|
511
527
|
sscf->builtin_session_cache = NGX_CONF_UNSET;
|
512
528
|
sscf->session_timeout = NGX_CONF_UNSET;
|
513
529
|
sscf->session_tickets = NGX_CONF_UNSET;
|
@@ -557,6 +573,8 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
557
573
|
ngx_conf_merge_str_value(conf->certificate, prev->certificate, "");
|
558
574
|
ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, "");
|
559
575
|
|
576
|
+
ngx_conf_merge_ptr_value(conf->passwords, prev->passwords, NULL);
|
577
|
+
|
560
578
|
ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");
|
561
579
|
|
562
580
|
ngx_conf_merge_str_value(conf->client_certificate, prev->client_certificate,
|
@@ -646,7 +664,7 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
646
664
|
cln->data = &conf->ssl;
|
647
665
|
|
648
666
|
if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
|
649
|
-
&conf->certificate_key)
|
667
|
+
&conf->certificate_key, conf->passwords)
|
650
668
|
!= NGX_OK)
|
651
669
|
{
|
652
670
|
return NGX_CONF_ERROR;
|
@@ -697,8 +715,10 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
697
715
|
SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
|
698
716
|
}
|
699
717
|
|
718
|
+
#ifndef LIBRESSL_VERSION_NUMBER
|
700
719
|
/* a temporary 512-bit RSA key is required for export versions of MSIE */
|
701
720
|
SSL_CTX_set_tmp_rsa_callback(conf->ssl.ctx, ngx_ssl_rsa512_key_callback);
|
721
|
+
#endif
|
702
722
|
|
703
723
|
if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
|
704
724
|
return NGX_CONF_ERROR;
|
@@ -775,6 +795,29 @@ ngx_http_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
775
795
|
}
|
776
796
|
|
777
797
|
|
798
|
+
static char *
|
799
|
+
ngx_http_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
800
|
+
{
|
801
|
+
ngx_http_ssl_srv_conf_t *sscf = conf;
|
802
|
+
|
803
|
+
ngx_str_t *value;
|
804
|
+
|
805
|
+
if (sscf->passwords != NGX_CONF_UNSET_PTR) {
|
806
|
+
return "is duplicate";
|
807
|
+
}
|
808
|
+
|
809
|
+
value = cf->args->elts;
|
810
|
+
|
811
|
+
sscf->passwords = ngx_ssl_read_password_file(cf, &value[1]);
|
812
|
+
|
813
|
+
if (sscf->passwords == NULL) {
|
814
|
+
return NGX_CONF_ERROR;
|
815
|
+
}
|
816
|
+
|
817
|
+
return NGX_CONF_OK;
|
818
|
+
}
|
819
|
+
|
820
|
+
|
778
821
|
static char *
|
779
822
|
ngx_http_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
780
823
|
{
|
@@ -10,18 +10,19 @@
|
|
10
10
|
#include <ngx_http.h>
|
11
11
|
|
12
12
|
|
13
|
+
static ngx_int_t ngx_http_stub_status_handler(ngx_http_request_t *r);
|
13
14
|
static ngx_int_t ngx_http_stub_status_variable(ngx_http_request_t *r,
|
14
15
|
ngx_http_variable_value_t *v, uintptr_t data);
|
15
16
|
static ngx_int_t ngx_http_stub_status_add_variables(ngx_conf_t *cf);
|
17
|
+
static char *ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd,
|
18
|
+
void *conf);
|
16
19
|
|
17
|
-
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
|
18
|
-
void *conf);
|
19
20
|
|
20
21
|
static ngx_command_t ngx_http_status_commands[] = {
|
21
22
|
|
22
23
|
{ ngx_string("stub_status"),
|
23
|
-
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|
|
24
|
-
|
24
|
+
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
|
25
|
+
ngx_http_set_stub_status,
|
25
26
|
0,
|
26
27
|
0,
|
27
28
|
NULL },
|
@@ -30,7 +31,6 @@ static ngx_command_t ngx_http_status_commands[] = {
|
|
30
31
|
};
|
31
32
|
|
32
33
|
|
33
|
-
|
34
34
|
static ngx_http_module_t ngx_http_stub_status_module_ctx = {
|
35
35
|
ngx_http_stub_status_add_variables, /* preconfiguration */
|
36
36
|
NULL, /* postconfiguration */
|
@@ -80,7 +80,8 @@ static ngx_http_variable_t ngx_http_stub_status_vars[] = {
|
|
80
80
|
};
|
81
81
|
|
82
82
|
|
83
|
-
static ngx_int_t
|
83
|
+
static ngx_int_t
|
84
|
+
ngx_http_stub_status_handler(ngx_http_request_t *r)
|
84
85
|
{
|
85
86
|
size_t size;
|
86
87
|
ngx_int_t rc;
|
@@ -223,12 +224,13 @@ ngx_http_stub_status_add_variables(ngx_conf_t *cf)
|
|
223
224
|
}
|
224
225
|
|
225
226
|
|
226
|
-
static char *
|
227
|
+
static char *
|
228
|
+
ngx_http_set_stub_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
227
229
|
{
|
228
230
|
ngx_http_core_loc_conf_t *clcf;
|
229
231
|
|
230
232
|
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
231
|
-
clcf->handler =
|
233
|
+
clcf->handler = ngx_http_stub_status_handler;
|
232
234
|
|
233
235
|
return NGX_CONF_OK;
|
234
236
|
}
|
@@ -175,10 +175,13 @@ ngx_http_sub_header_filter(ngx_http_request_t *r)
|
|
175
175
|
|
176
176
|
if (r == r->main) {
|
177
177
|
ngx_http_clear_content_length(r);
|
178
|
-
ngx_http_clear_etag(r);
|
179
178
|
|
180
179
|
if (!slcf->last_modified) {
|
181
180
|
ngx_http_clear_last_modified(r);
|
181
|
+
ngx_http_clear_etag(r);
|
182
|
+
|
183
|
+
} else {
|
184
|
+
ngx_http_weak_etag(r);
|
182
185
|
}
|
183
186
|
}
|
184
187
|
|
@@ -305,6 +308,7 @@ ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
305
308
|
b->last = ctx->copy_end;
|
306
309
|
b->shadow = NULL;
|
307
310
|
b->last_buf = 0;
|
311
|
+
b->last_in_chain = 0;
|
308
312
|
b->recycled = 0;
|
309
313
|
|
310
314
|
if (b->in_file) {
|
@@ -374,7 +378,9 @@ ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
374
378
|
continue;
|
375
379
|
}
|
376
380
|
|
377
|
-
if (ctx->
|
381
|
+
if (ctx->looked.len
|
382
|
+
&& (ctx->buf->last_buf || ctx->buf->last_in_chain))
|
383
|
+
{
|
378
384
|
cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
|
379
385
|
if (cl == NULL) {
|
380
386
|
return NGX_ERROR;
|
@@ -394,7 +400,7 @@ ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
394
400
|
ctx->looked.len = 0;
|
395
401
|
}
|
396
402
|
|
397
|
-
if (ctx->buf->last_buf || ctx->buf->flush
|
403
|
+
if (ctx->buf->last_buf || ctx->buf->flush || ctx->buf->sync
|
398
404
|
|| ngx_buf_in_memory(ctx->buf))
|
399
405
|
{
|
400
406
|
if (b == NULL) {
|
@@ -414,6 +420,7 @@ ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
414
420
|
}
|
415
421
|
|
416
422
|
b->last_buf = ctx->buf->last_buf;
|
423
|
+
b->last_in_chain = ctx->buf->last_in_chain;
|
417
424
|
b->flush = ctx->buf->flush;
|
418
425
|
b->shadow = ctx->buf;
|
419
426
|
|
@@ -539,6 +546,14 @@ ngx_http_sub_parse(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx)
|
|
539
546
|
|
540
547
|
for ( ;; ) {
|
541
548
|
if (ch == match) {
|
549
|
+
|
550
|
+
if (ctx->match.len == 1) {
|
551
|
+
ctx->pos = p + 1;
|
552
|
+
ctx->copy_end = p;
|
553
|
+
|
554
|
+
return NGX_OK;
|
555
|
+
}
|
556
|
+
|
542
557
|
copy_end = p;
|
543
558
|
ctx->looked.data[0] = *p;
|
544
559
|
looked = 1;
|
@@ -0,0 +1,641 @@
|
|
1
|
+
|
2
|
+
/*
|
3
|
+
* Copyright (C) Roman Arutyunyan
|
4
|
+
* Copyright (C) Nginx, Inc.
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#include <ngx_config.h>
|
9
|
+
#include <ngx_core.h>
|
10
|
+
#include <ngx_http.h>
|
11
|
+
|
12
|
+
|
13
|
+
typedef struct {
|
14
|
+
uint32_t hash;
|
15
|
+
ngx_str_t *server;
|
16
|
+
} ngx_http_upstream_chash_point_t;
|
17
|
+
|
18
|
+
|
19
|
+
typedef struct {
|
20
|
+
ngx_uint_t number;
|
21
|
+
ngx_http_upstream_chash_point_t point[1];
|
22
|
+
} ngx_http_upstream_chash_points_t;
|
23
|
+
|
24
|
+
|
25
|
+
typedef struct {
|
26
|
+
ngx_http_complex_value_t key;
|
27
|
+
ngx_http_upstream_chash_points_t *points;
|
28
|
+
} ngx_http_upstream_hash_srv_conf_t;
|
29
|
+
|
30
|
+
|
31
|
+
typedef struct {
|
32
|
+
/* the round robin data must be first */
|
33
|
+
ngx_http_upstream_rr_peer_data_t rrp;
|
34
|
+
ngx_http_upstream_hash_srv_conf_t *conf;
|
35
|
+
ngx_str_t key;
|
36
|
+
ngx_uint_t tries;
|
37
|
+
ngx_uint_t rehash;
|
38
|
+
uint32_t hash;
|
39
|
+
ngx_event_get_peer_pt get_rr_peer;
|
40
|
+
} ngx_http_upstream_hash_peer_data_t;
|
41
|
+
|
42
|
+
|
43
|
+
static ngx_int_t ngx_http_upstream_init_hash(ngx_conf_t *cf,
|
44
|
+
ngx_http_upstream_srv_conf_t *us);
|
45
|
+
static ngx_int_t ngx_http_upstream_init_hash_peer(ngx_http_request_t *r,
|
46
|
+
ngx_http_upstream_srv_conf_t *us);
|
47
|
+
static ngx_int_t ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc,
|
48
|
+
void *data);
|
49
|
+
|
50
|
+
static ngx_int_t ngx_http_upstream_init_chash(ngx_conf_t *cf,
|
51
|
+
ngx_http_upstream_srv_conf_t *us);
|
52
|
+
static int ngx_libc_cdecl
|
53
|
+
ngx_http_upstream_chash_cmp_points(const void *one, const void *two);
|
54
|
+
static ngx_uint_t ngx_http_upstream_find_chash_point(
|
55
|
+
ngx_http_upstream_chash_points_t *points, uint32_t hash);
|
56
|
+
static ngx_int_t ngx_http_upstream_init_chash_peer(ngx_http_request_t *r,
|
57
|
+
ngx_http_upstream_srv_conf_t *us);
|
58
|
+
static ngx_int_t ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc,
|
59
|
+
void *data);
|
60
|
+
|
61
|
+
static void *ngx_http_upstream_hash_create_conf(ngx_conf_t *cf);
|
62
|
+
static char *ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd,
|
63
|
+
void *conf);
|
64
|
+
|
65
|
+
|
66
|
+
static ngx_command_t ngx_http_upstream_hash_commands[] = {
|
67
|
+
|
68
|
+
{ ngx_string("hash"),
|
69
|
+
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE12,
|
70
|
+
ngx_http_upstream_hash,
|
71
|
+
NGX_HTTP_SRV_CONF_OFFSET,
|
72
|
+
0,
|
73
|
+
NULL },
|
74
|
+
|
75
|
+
ngx_null_command
|
76
|
+
};
|
77
|
+
|
78
|
+
|
79
|
+
static ngx_http_module_t ngx_http_upstream_hash_module_ctx = {
|
80
|
+
NULL, /* preconfiguration */
|
81
|
+
NULL, /* postconfiguration */
|
82
|
+
|
83
|
+
NULL, /* create main configuration */
|
84
|
+
NULL, /* init main configuration */
|
85
|
+
|
86
|
+
ngx_http_upstream_hash_create_conf, /* create server configuration */
|
87
|
+
NULL, /* merge server configuration */
|
88
|
+
|
89
|
+
NULL, /* create location configuration */
|
90
|
+
NULL /* merge location configuration */
|
91
|
+
};
|
92
|
+
|
93
|
+
|
94
|
+
ngx_module_t ngx_http_upstream_hash_module = {
|
95
|
+
NGX_MODULE_V1,
|
96
|
+
&ngx_http_upstream_hash_module_ctx, /* module context */
|
97
|
+
ngx_http_upstream_hash_commands, /* module directives */
|
98
|
+
NGX_HTTP_MODULE, /* module type */
|
99
|
+
NULL, /* init master */
|
100
|
+
NULL, /* init module */
|
101
|
+
NULL, /* init process */
|
102
|
+
NULL, /* init thread */
|
103
|
+
NULL, /* exit thread */
|
104
|
+
NULL, /* exit process */
|
105
|
+
NULL, /* exit master */
|
106
|
+
NGX_MODULE_V1_PADDING
|
107
|
+
};
|
108
|
+
|
109
|
+
|
110
|
+
static ngx_int_t
|
111
|
+
ngx_http_upstream_init_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
|
112
|
+
{
|
113
|
+
if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
|
114
|
+
return NGX_ERROR;
|
115
|
+
}
|
116
|
+
|
117
|
+
us->peer.init = ngx_http_upstream_init_hash_peer;
|
118
|
+
|
119
|
+
return NGX_OK;
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
static ngx_int_t
|
124
|
+
ngx_http_upstream_init_hash_peer(ngx_http_request_t *r,
|
125
|
+
ngx_http_upstream_srv_conf_t *us)
|
126
|
+
{
|
127
|
+
ngx_http_upstream_hash_srv_conf_t *hcf;
|
128
|
+
ngx_http_upstream_hash_peer_data_t *hp;
|
129
|
+
|
130
|
+
hp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_hash_peer_data_t));
|
131
|
+
if (hp == NULL) {
|
132
|
+
return NGX_ERROR;
|
133
|
+
}
|
134
|
+
|
135
|
+
r->upstream->peer.data = &hp->rrp;
|
136
|
+
|
137
|
+
if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
|
138
|
+
return NGX_ERROR;
|
139
|
+
}
|
140
|
+
|
141
|
+
r->upstream->peer.get = ngx_http_upstream_get_hash_peer;
|
142
|
+
|
143
|
+
hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
|
144
|
+
|
145
|
+
if (ngx_http_complex_value(r, &hcf->key, &hp->key) != NGX_OK) {
|
146
|
+
return NGX_ERROR;
|
147
|
+
}
|
148
|
+
|
149
|
+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
150
|
+
"upstream hash key:\"%V\"", &hp->key);
|
151
|
+
|
152
|
+
hp->conf = hcf;
|
153
|
+
hp->tries = 0;
|
154
|
+
hp->rehash = 0;
|
155
|
+
hp->hash = 0;
|
156
|
+
hp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
|
157
|
+
|
158
|
+
return NGX_OK;
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
static ngx_int_t
|
163
|
+
ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
|
164
|
+
{
|
165
|
+
ngx_http_upstream_hash_peer_data_t *hp = data;
|
166
|
+
|
167
|
+
time_t now;
|
168
|
+
u_char buf[NGX_INT_T_LEN];
|
169
|
+
size_t size;
|
170
|
+
uint32_t hash;
|
171
|
+
ngx_int_t w;
|
172
|
+
uintptr_t m;
|
173
|
+
ngx_uint_t i, n, p;
|
174
|
+
ngx_http_upstream_rr_peer_t *peer;
|
175
|
+
|
176
|
+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
177
|
+
"get hash peer, try: %ui", pc->tries);
|
178
|
+
|
179
|
+
if (hp->tries > 20 || hp->rrp.peers->single) {
|
180
|
+
return hp->get_rr_peer(pc, &hp->rrp);
|
181
|
+
}
|
182
|
+
|
183
|
+
now = ngx_time();
|
184
|
+
|
185
|
+
pc->cached = 0;
|
186
|
+
pc->connection = NULL;
|
187
|
+
|
188
|
+
for ( ;; ) {
|
189
|
+
|
190
|
+
/*
|
191
|
+
* Hash expression is compatible with Cache::Memcached:
|
192
|
+
* ((crc32([REHASH] KEY) >> 16) & 0x7fff) + PREV_HASH
|
193
|
+
* with REHASH omitted at the first iteration.
|
194
|
+
*/
|
195
|
+
|
196
|
+
ngx_crc32_init(hash);
|
197
|
+
|
198
|
+
if (hp->rehash > 0) {
|
199
|
+
size = ngx_sprintf(buf, "%ui", hp->rehash) - buf;
|
200
|
+
ngx_crc32_update(&hash, buf, size);
|
201
|
+
}
|
202
|
+
|
203
|
+
ngx_crc32_update(&hash, hp->key.data, hp->key.len);
|
204
|
+
ngx_crc32_final(hash);
|
205
|
+
|
206
|
+
hash = (hash >> 16) & 0x7fff;
|
207
|
+
|
208
|
+
hp->hash += hash;
|
209
|
+
hp->rehash++;
|
210
|
+
|
211
|
+
if (!hp->rrp.peers->weighted) {
|
212
|
+
p = hp->hash % hp->rrp.peers->number;
|
213
|
+
|
214
|
+
} else {
|
215
|
+
w = hp->hash % hp->rrp.peers->total_weight;
|
216
|
+
|
217
|
+
for (i = 0; i < hp->rrp.peers->number; i++) {
|
218
|
+
w -= hp->rrp.peers->peer[i].weight;
|
219
|
+
if (w < 0) {
|
220
|
+
break;
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
p = i;
|
225
|
+
}
|
226
|
+
|
227
|
+
n = p / (8 * sizeof(uintptr_t));
|
228
|
+
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
|
229
|
+
|
230
|
+
if (hp->rrp.tried[n] & m) {
|
231
|
+
goto next;
|
232
|
+
}
|
233
|
+
|
234
|
+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
235
|
+
"get hash peer, value:%uD, peer:%ui", hp->hash, p);
|
236
|
+
|
237
|
+
peer = &hp->rrp.peers->peer[p];
|
238
|
+
|
239
|
+
if (peer->down) {
|
240
|
+
goto next;
|
241
|
+
}
|
242
|
+
|
243
|
+
if (peer->max_fails
|
244
|
+
&& peer->fails >= peer->max_fails
|
245
|
+
&& now - peer->checked <= peer->fail_timeout)
|
246
|
+
{
|
247
|
+
goto next;
|
248
|
+
}
|
249
|
+
|
250
|
+
break;
|
251
|
+
|
252
|
+
next:
|
253
|
+
|
254
|
+
if (++hp->tries > 20) {
|
255
|
+
return hp->get_rr_peer(pc, &hp->rrp);
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
hp->rrp.current = p;
|
260
|
+
|
261
|
+
pc->sockaddr = peer->sockaddr;
|
262
|
+
pc->socklen = peer->socklen;
|
263
|
+
pc->name = &peer->name;
|
264
|
+
|
265
|
+
if (now - peer->checked > peer->fail_timeout) {
|
266
|
+
peer->checked = now;
|
267
|
+
}
|
268
|
+
|
269
|
+
hp->rrp.tried[n] |= m;
|
270
|
+
|
271
|
+
return NGX_OK;
|
272
|
+
}
|
273
|
+
|
274
|
+
|
275
|
+
static ngx_int_t
|
276
|
+
ngx_http_upstream_init_chash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
|
277
|
+
{
|
278
|
+
u_char *host, *port, c;
|
279
|
+
size_t host_len, port_len, size;
|
280
|
+
uint32_t hash, base_hash, prev_hash;
|
281
|
+
ngx_str_t *server;
|
282
|
+
ngx_uint_t npoints, i, j;
|
283
|
+
ngx_http_upstream_rr_peer_t *peer;
|
284
|
+
ngx_http_upstream_rr_peers_t *peers;
|
285
|
+
ngx_http_upstream_chash_points_t *points;
|
286
|
+
ngx_http_upstream_hash_srv_conf_t *hcf;
|
287
|
+
|
288
|
+
if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
|
289
|
+
return NGX_ERROR;
|
290
|
+
}
|
291
|
+
|
292
|
+
us->peer.init = ngx_http_upstream_init_chash_peer;
|
293
|
+
|
294
|
+
peers = us->peer.data;
|
295
|
+
npoints = peers->total_weight * 160;
|
296
|
+
|
297
|
+
size = sizeof(ngx_http_upstream_chash_points_t)
|
298
|
+
+ sizeof(ngx_http_upstream_chash_point_t) * (npoints - 1);
|
299
|
+
|
300
|
+
points = ngx_palloc(cf->pool, size);
|
301
|
+
if (points == NULL) {
|
302
|
+
return NGX_ERROR;
|
303
|
+
}
|
304
|
+
|
305
|
+
points->number = 0;
|
306
|
+
|
307
|
+
for (i = 0; i < peers->number; i++) {
|
308
|
+
peer = &peers->peer[i];
|
309
|
+
server = &peer->server;
|
310
|
+
|
311
|
+
/*
|
312
|
+
* Hash expression is compatible with Cache::Memcached::Fast:
|
313
|
+
* crc32(HOST \0 PORT PREV_HASH).
|
314
|
+
*/
|
315
|
+
|
316
|
+
if (server->len >= 5
|
317
|
+
&& ngx_strncasecmp(server->data, (u_char *) "unix:", 5) == 0)
|
318
|
+
{
|
319
|
+
host = server->data + 5;
|
320
|
+
host_len = server->len - 5;
|
321
|
+
port = NULL;
|
322
|
+
port_len = 0;
|
323
|
+
goto done;
|
324
|
+
}
|
325
|
+
|
326
|
+
for (j = 0; j < server->len; j++) {
|
327
|
+
c = server->data[server->len - j - 1];
|
328
|
+
|
329
|
+
if (c == ':') {
|
330
|
+
host = server->data;
|
331
|
+
host_len = server->len - j - 1;
|
332
|
+
port = server->data + server->len - j;
|
333
|
+
port_len = j;
|
334
|
+
goto done;
|
335
|
+
}
|
336
|
+
|
337
|
+
if (c < '0' || c > '9') {
|
338
|
+
break;
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
host = server->data;
|
343
|
+
host_len = server->len;
|
344
|
+
port = NULL;
|
345
|
+
port_len = 0;
|
346
|
+
|
347
|
+
done:
|
348
|
+
|
349
|
+
ngx_crc32_init(base_hash);
|
350
|
+
ngx_crc32_update(&base_hash, host, host_len);
|
351
|
+
ngx_crc32_update(&base_hash, (u_char *) "", 1);
|
352
|
+
ngx_crc32_update(&base_hash, port, port_len);
|
353
|
+
|
354
|
+
prev_hash = 0;
|
355
|
+
npoints = peer->weight * 160;
|
356
|
+
|
357
|
+
for (j = 0; j < npoints; j++) {
|
358
|
+
hash = base_hash;
|
359
|
+
|
360
|
+
ngx_crc32_update(&hash, (u_char *) &prev_hash, sizeof(uint32_t));
|
361
|
+
ngx_crc32_final(hash);
|
362
|
+
|
363
|
+
points->point[points->number].hash = hash;
|
364
|
+
points->point[points->number].server = server;
|
365
|
+
points->number++;
|
366
|
+
|
367
|
+
prev_hash = hash;
|
368
|
+
}
|
369
|
+
}
|
370
|
+
|
371
|
+
ngx_qsort(points->point,
|
372
|
+
points->number,
|
373
|
+
sizeof(ngx_http_upstream_chash_point_t),
|
374
|
+
ngx_http_upstream_chash_cmp_points);
|
375
|
+
|
376
|
+
for (i = 0, j = 1; j < points->number; j++) {
|
377
|
+
if (points->point[i].hash != points->point[j].hash) {
|
378
|
+
points->point[++i] = points->point[j];
|
379
|
+
}
|
380
|
+
}
|
381
|
+
|
382
|
+
points->number = i + 1;
|
383
|
+
|
384
|
+
hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
|
385
|
+
hcf->points = points;
|
386
|
+
|
387
|
+
return NGX_OK;
|
388
|
+
}
|
389
|
+
|
390
|
+
|
391
|
+
static int ngx_libc_cdecl
|
392
|
+
ngx_http_upstream_chash_cmp_points(const void *one, const void *two)
|
393
|
+
{
|
394
|
+
ngx_http_upstream_chash_point_t *first =
|
395
|
+
(ngx_http_upstream_chash_point_t *) one;
|
396
|
+
ngx_http_upstream_chash_point_t *second =
|
397
|
+
(ngx_http_upstream_chash_point_t *) two;
|
398
|
+
|
399
|
+
if (first->hash < second->hash) {
|
400
|
+
return -1;
|
401
|
+
|
402
|
+
} else if (first->hash > second->hash) {
|
403
|
+
return 1;
|
404
|
+
|
405
|
+
} else {
|
406
|
+
return 0;
|
407
|
+
}
|
408
|
+
}
|
409
|
+
|
410
|
+
|
411
|
+
static ngx_uint_t
|
412
|
+
ngx_http_upstream_find_chash_point(ngx_http_upstream_chash_points_t *points,
|
413
|
+
uint32_t hash)
|
414
|
+
{
|
415
|
+
ngx_uint_t i, j, k;
|
416
|
+
ngx_http_upstream_chash_point_t *point;
|
417
|
+
|
418
|
+
/* find first point >= hash */
|
419
|
+
|
420
|
+
point = &points->point[0];
|
421
|
+
|
422
|
+
i = 0;
|
423
|
+
j = points->number;
|
424
|
+
|
425
|
+
while (i < j) {
|
426
|
+
k = (i + j) / 2;
|
427
|
+
|
428
|
+
if (hash > point[k].hash) {
|
429
|
+
i = k + 1;
|
430
|
+
|
431
|
+
} else if (hash < point[k].hash) {
|
432
|
+
j = k;
|
433
|
+
|
434
|
+
} else {
|
435
|
+
return k;
|
436
|
+
}
|
437
|
+
}
|
438
|
+
|
439
|
+
return i;
|
440
|
+
}
|
441
|
+
|
442
|
+
|
443
|
+
static ngx_int_t
|
444
|
+
ngx_http_upstream_init_chash_peer(ngx_http_request_t *r,
|
445
|
+
ngx_http_upstream_srv_conf_t *us)
|
446
|
+
{
|
447
|
+
uint32_t hash;
|
448
|
+
ngx_http_upstream_hash_srv_conf_t *hcf;
|
449
|
+
ngx_http_upstream_hash_peer_data_t *hp;
|
450
|
+
|
451
|
+
if (ngx_http_upstream_init_hash_peer(r, us) != NGX_OK) {
|
452
|
+
return NGX_ERROR;
|
453
|
+
}
|
454
|
+
|
455
|
+
r->upstream->peer.get = ngx_http_upstream_get_chash_peer;
|
456
|
+
|
457
|
+
hp = r->upstream->peer.data;
|
458
|
+
hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
|
459
|
+
|
460
|
+
hash = ngx_crc32_long(hp->key.data, hp->key.len);
|
461
|
+
hp->hash = ngx_http_upstream_find_chash_point(hcf->points, hash);
|
462
|
+
|
463
|
+
return NGX_OK;
|
464
|
+
}
|
465
|
+
|
466
|
+
|
467
|
+
static ngx_int_t
|
468
|
+
ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
469
|
+
{
|
470
|
+
ngx_http_upstream_hash_peer_data_t *hp = data;
|
471
|
+
|
472
|
+
time_t now;
|
473
|
+
intptr_t m;
|
474
|
+
ngx_str_t *server;
|
475
|
+
ngx_int_t total;
|
476
|
+
ngx_uint_t i, n;
|
477
|
+
ngx_http_upstream_rr_peer_t *peer, *best;
|
478
|
+
ngx_http_upstream_chash_point_t *point;
|
479
|
+
ngx_http_upstream_chash_points_t *points;
|
480
|
+
ngx_http_upstream_hash_srv_conf_t *hcf;
|
481
|
+
|
482
|
+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
483
|
+
"get consistent hash peer, try: %ui", pc->tries);
|
484
|
+
|
485
|
+
pc->cached = 0;
|
486
|
+
pc->connection = NULL;
|
487
|
+
|
488
|
+
now = ngx_time();
|
489
|
+
hcf = hp->conf;
|
490
|
+
|
491
|
+
points = hcf->points;
|
492
|
+
point = &points->point[0];
|
493
|
+
|
494
|
+
for ( ;; ) {
|
495
|
+
server = point[hp->hash % points->number].server;
|
496
|
+
|
497
|
+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
498
|
+
"consistent hash peer:%uD, server:\"%V\"",
|
499
|
+
hp->hash, server);
|
500
|
+
|
501
|
+
best = NULL;
|
502
|
+
total = 0;
|
503
|
+
|
504
|
+
for (i = 0; i < hp->rrp.peers->number; i++) {
|
505
|
+
|
506
|
+
n = i / (8 * sizeof(uintptr_t));
|
507
|
+
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
508
|
+
|
509
|
+
if (hp->rrp.tried[n] & m) {
|
510
|
+
continue;
|
511
|
+
}
|
512
|
+
|
513
|
+
peer = &hp->rrp.peers->peer[i];
|
514
|
+
|
515
|
+
if (peer->down) {
|
516
|
+
continue;
|
517
|
+
}
|
518
|
+
|
519
|
+
if (peer->server.len != server->len
|
520
|
+
|| ngx_strncmp(peer->server.data, server->data, server->len)
|
521
|
+
!= 0)
|
522
|
+
{
|
523
|
+
continue;
|
524
|
+
}
|
525
|
+
|
526
|
+
if (peer->max_fails
|
527
|
+
&& peer->fails >= peer->max_fails
|
528
|
+
&& now - peer->checked <= peer->fail_timeout)
|
529
|
+
{
|
530
|
+
continue;
|
531
|
+
}
|
532
|
+
|
533
|
+
peer->current_weight += peer->effective_weight;
|
534
|
+
total += peer->effective_weight;
|
535
|
+
|
536
|
+
if (peer->effective_weight < peer->weight) {
|
537
|
+
peer->effective_weight++;
|
538
|
+
}
|
539
|
+
|
540
|
+
if (best == NULL || peer->current_weight > best->current_weight) {
|
541
|
+
best = peer;
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
if (best) {
|
546
|
+
best->current_weight -= total;
|
547
|
+
|
548
|
+
i = best - &hp->rrp.peers->peer[0];
|
549
|
+
|
550
|
+
hp->rrp.current = i;
|
551
|
+
|
552
|
+
n = i / (8 * sizeof(uintptr_t));
|
553
|
+
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
554
|
+
|
555
|
+
hp->rrp.tried[n] |= m;
|
556
|
+
|
557
|
+
if (now - best->checked > best->fail_timeout) {
|
558
|
+
best->checked = now;
|
559
|
+
}
|
560
|
+
|
561
|
+
pc->sockaddr = best->sockaddr;
|
562
|
+
pc->socklen = best->socklen;
|
563
|
+
pc->name = &best->name;
|
564
|
+
|
565
|
+
return NGX_OK;
|
566
|
+
}
|
567
|
+
|
568
|
+
hp->hash++;
|
569
|
+
hp->tries++;
|
570
|
+
|
571
|
+
if (hp->tries >= points->number) {
|
572
|
+
return NGX_BUSY;
|
573
|
+
}
|
574
|
+
}
|
575
|
+
}
|
576
|
+
|
577
|
+
|
578
|
+
static void *
|
579
|
+
ngx_http_upstream_hash_create_conf(ngx_conf_t *cf)
|
580
|
+
{
|
581
|
+
ngx_http_upstream_hash_srv_conf_t *conf;
|
582
|
+
|
583
|
+
conf = ngx_palloc(cf->pool, sizeof(ngx_http_upstream_hash_srv_conf_t));
|
584
|
+
if (conf == NULL) {
|
585
|
+
return NULL;
|
586
|
+
}
|
587
|
+
|
588
|
+
conf->points = NULL;
|
589
|
+
|
590
|
+
return conf;
|
591
|
+
}
|
592
|
+
|
593
|
+
|
594
|
+
static char *
|
595
|
+
ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
596
|
+
{
|
597
|
+
ngx_http_upstream_hash_srv_conf_t *hcf = conf;
|
598
|
+
|
599
|
+
ngx_str_t *value;
|
600
|
+
ngx_http_upstream_srv_conf_t *uscf;
|
601
|
+
ngx_http_compile_complex_value_t ccv;
|
602
|
+
|
603
|
+
value = cf->args->elts;
|
604
|
+
|
605
|
+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
606
|
+
|
607
|
+
ccv.cf = cf;
|
608
|
+
ccv.value = &value[1];
|
609
|
+
ccv.complex_value = &hcf->key;
|
610
|
+
|
611
|
+
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
612
|
+
return NGX_CONF_ERROR;
|
613
|
+
}
|
614
|
+
|
615
|
+
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
|
616
|
+
|
617
|
+
if (uscf->peer.init_upstream) {
|
618
|
+
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
619
|
+
"load balancing method redefined");
|
620
|
+
}
|
621
|
+
|
622
|
+
uscf->flags = NGX_HTTP_UPSTREAM_CREATE
|
623
|
+
|NGX_HTTP_UPSTREAM_WEIGHT
|
624
|
+
|NGX_HTTP_UPSTREAM_MAX_FAILS
|
625
|
+
|NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
|
626
|
+
|NGX_HTTP_UPSTREAM_DOWN;
|
627
|
+
|
628
|
+
if (cf->args->nelts == 2) {
|
629
|
+
uscf->peer.init_upstream = ngx_http_upstream_init_hash;
|
630
|
+
|
631
|
+
} else if (ngx_strcmp(value[2].data, "consistent") == 0) {
|
632
|
+
uscf->peer.init_upstream = ngx_http_upstream_init_chash;
|
633
|
+
|
634
|
+
} else {
|
635
|
+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
636
|
+
"invalid parameter \"%V\"", &value[2]);
|
637
|
+
return NGX_CONF_ERROR;
|
638
|
+
}
|
639
|
+
|
640
|
+
return NGX_CONF_OK;
|
641
|
+
}
|