Skip to content

Commit 8e79530

Browse files
committed
HTTP/2: ngx_http_v2_proxy_module.
The module allows to use HTTP/2 protocol for proxying. HTTP/2 proxying is enabled by specifying "proxy_http_version 2.0". Example: server { listen 8000; location / { proxy_http_version 2.0; proxy_pass https://127.0.0.1:8443; } } server { listen 8443 ssl http2; ssl_certificate certs/example.com.crt; ssl_certificate_key certs/example.com.key; location / { return 200 foo; } }
1 parent c42e715 commit 8e79530

File tree

5 files changed

+3965
-2
lines changed

5 files changed

+3965
-2
lines changed

auto/modules

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,17 @@ if [ $HTTP = YES ]; then
782782
. auto/module
783783
fi
784784

785+
if [ $HTTP_V2 = YES ]; then
786+
ngx_module_name=ngx_http_v2_proxy_module
787+
ngx_module_incs=
788+
ngx_module_deps=
789+
ngx_module_srcs=src/http/v2/ngx_http_v2_proxy_module.c
790+
ngx_module_libs=
791+
ngx_module_link=$HTTP_V2
792+
793+
. auto/module
794+
fi
795+
785796
if [ $HTTP_PERL != NO ]; then
786797
ngx_module_name=ngx_http_perl_module
787798
ngx_module_incs=src/http/modules/perl

src/http/modules/ngx_http_proxy_module.c

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ static ngx_conf_post_t ngx_http_proxy_ssl_conf_command_post =
161161
static ngx_conf_enum_t ngx_http_proxy_http_version[] = {
162162
{ ngx_string("1.0"), NGX_HTTP_VERSION_10 },
163163
{ ngx_string("1.1"), NGX_HTTP_VERSION_11 },
164+
#if (NGX_HTTP_V2)
165+
{ ngx_string("2.0"), NGX_HTTP_VERSION_20 },
166+
#endif
164167
{ ngx_null_string, 0 }
165168
};
166169

@@ -830,6 +833,14 @@ ngx_http_proxy_handler(ngx_http_request_t *r)
830833
ngx_http_proxy_main_conf_t *pmcf;
831834
#endif
832835

836+
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
837+
838+
#if (NGX_HTTP_V2)
839+
if (plcf->http_version == NGX_HTTP_VERSION_20) {
840+
return ngx_http_v2_proxy_handler(r);
841+
}
842+
#endif
843+
833844
if (ngx_http_upstream_create(r) != NGX_OK) {
834845
return NGX_HTTP_INTERNAL_SERVER_ERROR;
835846
}
@@ -841,8 +852,6 @@ ngx_http_proxy_handler(ngx_http_request_t *r)
841852

842853
ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);
843854

844-
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
845-
846855
u = r->upstream;
847856

848857
if (plcf->proxy_lengths == NULL) {
@@ -3456,6 +3465,8 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
34563465
* conf->ssl_ciphers = { 0, NULL };
34573466
* conf->ssl_trusted_certificate = { 0, NULL };
34583467
* conf->ssl_crl = { 0, NULL };
3468+
* conf->host = { 0, NULL };
3469+
* conf->host_set = 0;
34593470
*/
34603471

34613472
conf->upstream.store = NGX_CONF_UNSET;
@@ -3980,6 +3991,9 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
39803991

39813992
#if (NGX_HTTP_SSL)
39823993
conf->ssl = prev->ssl;
3994+
#endif
3995+
#if (NGX_HTTP_SSL)
3996+
conf->host = prev->host;
39833997
#endif
39843998
}
39853999

@@ -4019,6 +4033,9 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
40194033
conf->headers = prev->headers;
40204034
#if (NGX_HTTP_CACHE)
40214035
conf->headers_cache = prev->headers_cache;
4036+
#endif
4037+
#if (NGX_HTTP_V2)
4038+
conf->host_set = prev->host_set;
40224039
#endif
40234040
}
40244041

@@ -4051,6 +4068,9 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
40514068
prev->headers = conf->headers;
40524069
#if (NGX_HTTP_CACHE)
40534070
prev->headers_cache = conf->headers_cache;
4071+
#endif
4072+
#if (NGX_HTTP_V2)
4073+
conf->host_set = prev->host_set;
40544074
#endif
40554075
}
40564076

@@ -4104,6 +4124,14 @@ ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
41044124
src = conf->headers_source->elts;
41054125
for (i = 0; i < conf->headers_source->nelts; i++) {
41064126

4127+
#if (NGX_HTTP_V2)
4128+
if (src[i].key.len == 4
4129+
&& ngx_strncasecmp(src[i].key.data, (u_char *) "Host", 4) == 0)
4130+
{
4131+
conf->host_set = 1;
4132+
}
4133+
#endif
4134+
41074135
s = ngx_array_push(&headers_merged);
41084136
if (s == NULL) {
41094137
return NGX_ERROR;
@@ -4343,6 +4371,27 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
43434371

43444372
plcf->url = *url;
43454373

4374+
#if (NGX_HTTP_V2)
4375+
4376+
if (plcf->http_version == NGX_HTTP_VERSION_20) {
4377+
4378+
if (u.family != AF_UNIX) {
4379+
4380+
if (u.no_port) {
4381+
plcf->host = u.host;
4382+
4383+
} else {
4384+
plcf->host.len = u.host.len + 1 + u.port_text.len;
4385+
plcf->host.data = u.host.data;
4386+
}
4387+
4388+
} else {
4389+
ngx_str_set(&plcf->host, "localhost");
4390+
}
4391+
}
4392+
4393+
#endif
4394+
43464395
return NGX_CONF_OK;
43474396
}
43484397

@@ -5274,6 +5323,21 @@ ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
52745323
return NGX_ERROR;
52755324
}
52765325

5326+
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5327+
5328+
if (plcf->http_version == NGX_HTTP_VERSION_20) {
5329+
if (SSL_CTX_set_alpn_protos(plcf->upstream.ssl->ctx,
5330+
(u_char *) "\x02h2", 3)
5331+
!= 0)
5332+
{
5333+
ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
5334+
"SSL_CTX_set_alpn_protos() failed");
5335+
return NGX_ERROR;
5336+
}
5337+
}
5338+
5339+
#endif
5340+
52775341
if (ngx_ssl_conf_commands(cf, plcf->upstream.ssl, plcf->ssl_conf_commands)
52785342
!= NGX_OK)
52795343
{

src/http/modules/ngx_http_proxy_module.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ typedef struct {
117117
ngx_str_t ssl_crl;
118118
ngx_array_t *ssl_conf_commands;
119119
#endif
120+
121+
#if (NGX_HTTP_V2)
122+
ngx_str_t host;
123+
ngx_uint_t host_set;
124+
#endif
120125
} ngx_http_proxy_loc_conf_t;
121126

122127

src/http/v2/ngx_http_v2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ ngx_int_t ngx_http_v2_table_size(ngx_http_v2_connection_t *h2c, size_t size);
416416
u_char *ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len,
417417
u_char *tmp, ngx_uint_t lower);
418418

419+
ngx_int_t ngx_http_v2_proxy_handler(ngx_http_request_t *r);
420+
419421

420422
extern ngx_module_t ngx_http_v2_module;
421423

0 commit comments

Comments
 (0)