Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apisix/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ do
pool_opt.pool_size = size

local scheme = up_conf.scheme
local pool = scheme .. "#" .. server.host .. "#" .. server.port
-- A TLS connection negotiated for HTTP/2 cannot carry HTTP/1.1 requests.
local protocol = up_conf.http_version == "2" and "https+h2" or scheme
local pool = protocol .. "#" .. server.host .. "#" .. server.port
-- other TLS schemes don't use http balancer keepalive
if (scheme == "https" or scheme == "grpcs") then
local sni = ctx.var.upstream_host
Expand Down
31 changes: 29 additions & 2 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,9 @@ http {
proxy_ssl_server_name on;
{% end %}

{% local proxy_versions = upstream_http2 and {"1.1", "2"} or {"1.1"} %}
{% for _, proxy_version in ipairs(proxy_versions) do %}
{% if proxy_version == "1.1" then %}
location / {
set $upstream_mirror_host '';
set $upstream_mirror_uri '';
Expand Down Expand Up @@ -1002,14 +1005,23 @@ http {
access_by_lua_block {
apisix.http_access_phase()
}
{% else %}
location @https_upstream_http2 {
access_by_lua_block {
apisix.https_upstream_http2_access_phase()
}
{% end %}

proxy_http_version 1.1;
proxy_http_version {* proxy_version *};
proxy_set_header Host $upstream_host;
{% if proxy_version == "1.1" then %}
proxy_set_header Upgrade $upstream_upgrade;
proxy_set_header Connection $upstream_connection;
{% end %}
proxy_set_header X-Real-IP $remote_addr;
proxy_pass_header Date;

{% if proxy_version == "1.1" then %}
### the following x-forwarded-* headers is to send to upstream server

# Take copies before neutralizing, so a trusted peer's own values can
Expand Down Expand Up @@ -1041,6 +1053,7 @@ http {
more_set_input_headers "X-Forwarded-Host: $var_x_forwarded_host";
more_set_input_headers "X-Forwarded-Port: $var_x_forwarded_port";
more_set_input_headers "Forwarded: ";
{% end %}

# X-Forwarded-Proto/Host/Port are not set here: `r->headers_in` already
# holds the values this request should carry, and proxy_pass forwards it
Expand All @@ -1052,10 +1065,12 @@ http {

{% if enabled_plugins["proxy-cache"] or enabled_plugins["graphql-proxy-cache"] then %}
### the following configuration is to cache response content from upstream server
{% if proxy_version == "1.1" then %}
set $upstream_cache_zone off;
set $upstream_cache_key '';
set $upstream_cache_bypass '';
set $upstream_no_cache '';
{% end %}

proxy_cache $upstream_cache_zone;
proxy_cache_valid any {% if proxy_cache.cache_ttl then %} {* proxy_cache.cache_ttl *} {% else %} 10s {% end %};
Expand Down Expand Up @@ -1087,6 +1102,7 @@ http {
apisix.http_log_phase()
}
}
{% end %}

location @grpc_pass {

Expand Down Expand Up @@ -1152,15 +1168,25 @@ http {
{% end %}

{% if enabled_plugins["proxy-buffering"] then %}
{% for _, proxy_version in ipairs(proxy_versions) do %}
{% if proxy_version == "1.1" then %}
location @disable_proxy_buffering {
access_by_lua_block {
apisix.disable_proxy_buffering_access_phase()
}
{% else %}
location @https_upstream_http2_unbuffered {
access_by_lua_block {
apisix.https_upstream_http2_access_phase()
}
{% end %}

proxy_http_version 1.1;
proxy_http_version {* proxy_version *};
proxy_set_header Host $upstream_host;
{% if proxy_version == "1.1" then %}
proxy_set_header Upgrade $upstream_upgrade;
proxy_set_header Connection $upstream_connection;
{% end %}
proxy_set_header X-Real-IP $remote_addr;
proxy_pass_header Date;

Expand Down Expand Up @@ -1193,6 +1219,7 @@ http {
proxy_buffering off;
}
{% end %}
{% end %}

location @websocket_pass {
content_by_lua_block {
Expand Down
2 changes: 2 additions & 0 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ Please modify "admin_key" in conf/config.yaml .
enabled_discoveries = enabled_discoveries,
enabled_plugins = enabled_plugins,
enabled_stream_plugins = enabled_stream_plugins,
upstream_http2 = version_greater_equal(or_ver, "1.29.4")
and str_find(or_info, "--with-http_v2_module", 1, true),
dubbo_upstream_multiplex_count = dubbo_upstream_multiplex_count,
status_server_addr = status_server_addr,
admin_server_addr = admin_server_addr,
Expand Down
58 changes: 48 additions & 10 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ if ngx.config.subsystem == "http" then
is_http = true
control_api_router = require("apisix.control.router")
end
local supports_upstream_http2 = is_http and ngx.config.nginx_version >= 1029004
and str_find(ngx.config.nginx_configure(),
"--with-http_v2_module", 1, true)

local ok, apisix_base_flags = pcall(require, "resty.apisix.patch")
if not ok then
Expand Down Expand Up @@ -807,6 +810,22 @@ function _M.handle_upstream(api_ctx, route, enable_websocket)
end

local up_scheme = api_ctx.upstream_scheme
if api_ctx.upstream_conf and api_ctx.upstream_conf.http_version == "2" then
if not supports_upstream_http2 then
core.log.error("upstream HTTP/2 requires NGINX 1.29.4 or newer with HTTP/2 support")
return core.response.exit(503)
end
if up_scheme ~= "https" then
core.log.error("upstream HTTP/2 requires the effective upstream scheme to be https")
return core.response.exit(503)
end

local location = ngx.ctx.disable_proxy_buffering and "@https_upstream_http2_unbuffered"
or "@https_upstream_http2"
stash_ngx_ctx()
return ngx.exec(location)
end

if up_scheme == "grpcs" or up_scheme == "grpc" then
stash_ngx_ctx()
return ngx.exec("@grpc_pass")
Expand Down Expand Up @@ -1131,6 +1150,22 @@ function _M.disable_proxy_buffering_access_phase()
end


local function restore_proxy_request_options(api_ctx)
if api_ctx.enable_mirror == true and has_mod then
apisix_ngx_client.enable_mirror()
end

if api_ctx.client_max_body_size then
local ok, err = apisix_ngx_client.set_client_max_body_size(
api_ctx.client_max_body_size)
if not ok then
core.log.error("failed to set client max body size: ", err)
core.response.exit(503)
end
end
end


function _M.grpc_access_phase()
ngx.ctx = fetch_ctx()

Expand All @@ -1145,18 +1180,21 @@ function _M.grpc_access_phase()
core.response.exit(code)
end

if api_ctx.enable_mirror == true and has_mod then
apisix_ngx_client.enable_mirror()
end
restore_proxy_request_options(api_ctx)
end

if api_ctx.client_max_body_size then
local ok, err = apisix_ngx_client.set_client_max_body_size(
api_ctx.client_max_body_size)
if not ok then
core.log.error("failed to set client max body size: ", err)
core.response.exit(503)
end

function _M.https_upstream_http2_access_phase()
ngx.ctx = fetch_ctx()

local api_ctx = ngx.ctx.api_ctx
local code, err = apisix_upstream.set_http_upstream_tls(api_ctx, api_ctx.upstream_conf)
if code then
core.log.error("failed to set HTTP/2 upstream TLS parameters: ", err)
return core.response.exit(code)
end

restore_proxy_request_options(api_ctx)
end


Expand Down
11 changes: 11 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ local upstream_schema = {
" For L4 proxy, it can be one of tcp/tls/udp." ..
" For specific protocols, it can be kafka."
},
http_version = {
description = "Explicitly use HTTP/2 with an HTTPS upstream.",
type = "string",
enum = {"2"},
},
discovery_type = {
description = "discovery type",
type = "string",
Expand Down Expand Up @@ -598,6 +603,12 @@ local upstream_schema = {
{required = {"nodes"}},
{required = {"service_name", "discovery_type"}},
},
dependencies = {
http_version = {
properties = {scheme = {enum = {"https"}}},
required = {"scheme"},
},
},
additionalProperties = false
}

Expand Down
10 changes: 5 additions & 5 deletions apisix/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ end

-- Apply upstream.tls.verify / upstream.tls.ca_certs to the current request.
-- Both settings live in the apisix-nginx-module request context, which is wiped
-- by the internal redirect to @grpc_pass, so grpcs has to apply them again from
-- `grpc_access_phase` just like the client certificate does.
-- by internal redirects, so named upstream locations must restore these
-- settings along with the client certificate.
local function set_upstream_ssl_opts(up_conf)
local tls = up_conf.tls
if not tls then
Expand Down Expand Up @@ -329,7 +329,7 @@ local function set_stream_upstream_client_cert(api_ctx, up_conf)
end


local function set_http_upstream_tls(api_ctx, up_conf)
function _M.set_http_upstream_tls(api_ctx, up_conf)
local scheme = up_conf.scheme
if scheme == "https" then
-- grpcs applies these in `set_grpcs_upstream_param` instead, after the
Expand Down Expand Up @@ -390,7 +390,7 @@ function _M.set_by_route(route, api_ctx)

if api_ctx.upstream_conf then
-- upstream_conf has been set by traffic-split plugin
return set_http_upstream_tls(api_ctx, api_ctx.upstream_conf)
return _M.set_http_upstream_tls(api_ctx, api_ctx.upstream_conf)
end

local up_conf = api_ctx.matched_upstream
Expand Down Expand Up @@ -499,7 +499,7 @@ function _M.set_by_route(route, api_ctx)
local checker = healthcheck_manager.fetch_checker(up_conf.resource_key, resource_version)
api_ctx.up_checker = checker

return set_http_upstream_tls(api_ctx, up_conf)
return _M.set_http_upstream_tls(api_ctx, up_conf)
end


Expand Down
Loading
Loading