Skip to content

Commit

Permalink
perf: no longer generate unnecessary nginx conf for better performanc…
Browse files Browse the repository at this point in the history
…e. (apache#1968)

* perf: no longer generate unnecessary nginx conf for better performance.
* benchmark: sync nginx.conf for fake-apisix.

> Is this PR backward compatible?

Disable two plugins by default(proxy-cache, proxy-mirror), if the user wants to enable them, need to modify the conf/config.yaml by manual.
  • Loading branch information
membphis authored Aug 4, 2020
1 parent 2073087 commit d32b5cf
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 9 deletions.
29 changes: 28 additions & 1 deletion benchmark/fake-apisix/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ http {
apisix.http_balancer_phase()
}

keepalive 32;
keepalive 320;
}

server {
Expand Down Expand Up @@ -100,6 +100,33 @@ http {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass_header Server;
proxy_pass_header Date;

### the following x-forwarded-* headers is to send to upstream server

set $var_x_forwarded_for $remote_addr;
set $var_x_forwarded_proto $scheme;
set $var_x_forwarded_host $host;
set $var_x_forwarded_port $server_port;

if ($http_x_forwarded_for != "") {
set $var_x_forwarded_for "${http_x_forwarded_for}, ${realip_remote_addr}";
}
if ($http_x_forwarded_proto != "") {
set $var_x_forwarded_proto $http_x_forwarded_proto;
}
if ($http_x_forwarded_host != "") {
set $var_x_forwarded_host $http_x_forwarded_host;
}
if ($http_x_forwarded_port != "") {
set $var_x_forwarded_port $http_x_forwarded_port;
}

proxy_set_header X-Forwarded-For $var_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $var_x_forwarded_proto;
proxy_set_header X-Forwarded-Host $var_x_forwarded_host;
proxy_set_header X-Forwarded-Port $var_x_forwarded_port;

# proxy pass
proxy_pass $upstream_scheme://apisix_backend$upstream_uri;

header_filter_by_lua_block {
Expand Down
6 changes: 4 additions & 2 deletions benchmark/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mkdir -p benchmark/fake-apisix/logs

sudo openresty -p $PWD/benchmark/server || exit 1

make init

trap 'onCtrlC' INT
function onCtrlC () {
sudo killall wrk
Expand Down Expand Up @@ -57,7 +59,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:80": 1
"127.0.0.1:1980": 1
}
}
}'
Expand Down Expand Up @@ -90,7 +92,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:80": 1
"127.0.0.1:1980": 1
}
}
}'
Expand Down
2 changes: 1 addition & 1 deletion benchmark/server/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ worker_shutdown_timeout 3;

http {
server {
listen 80;
listen 1980;

access_log off;
location = /hello {
Expand Down
21 changes: 18 additions & 3 deletions bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ http {
{% end %}
{% end %}
{% if proxy_cache then %}
{% if enabled_plugins["proxy-cache"] then %}
# for proxy cache
{% for _, cache in ipairs(proxy_cache.zones) do %}
proxy_cache_path {* cache.disk_path *} levels={* cache.cache_levels *} keys_zone={* cache.name *}:{* cache.memory_size *} inactive=1d max_size={* cache.disk_size *};
{% end %}
{% end %}
{% if proxy_cache then %}
{% if enabled_plugins["proxy-cache"] then %}
# for proxy cache
map $upstream_cache_zone $upstream_cache_zone_info {
{% for _, cache in ipairs(proxy_cache.zones) do %}
Expand Down Expand Up @@ -459,7 +459,7 @@ http {
proxy_set_header X-Forwarded-Host $var_x_forwarded_host;
proxy_set_header X-Forwarded-Port $var_x_forwarded_port;
{% if proxy_cache then %}
{% if enabled_plugins["proxy-cache"] then %}
### the following configuration is to cache response content from upstream server
set $upstream_cache_zone off;
Expand Down Expand Up @@ -487,7 +487,10 @@ http {
{% end %}
proxy_pass $upstream_scheme://apisix_backend$upstream_uri;
{% if enabled_plugins["proxy-mirror"] then %}
mirror /proxy_mirror;
{% end %}
header_filter_by_lua_block {
apisix.http_header_filter_phase()
Expand Down Expand Up @@ -525,6 +528,7 @@ http {
}
}
{% if enabled_plugins["proxy-mirror"] then %}
location = /proxy_mirror {
internal;
Expand All @@ -534,6 +538,7 @@ http {
proxy_pass $upstream_mirror_host$request_uri;
}
{% end %}
}
}
]=]
Expand Down Expand Up @@ -686,6 +691,15 @@ local function init()
with_module_status = false
end

local enabled_plugins = {}
for i, name in ipairs(yaml_conf.plugins) do
enabled_plugins[name] = true
end

if enabled_plugins["proxy-cache"] and not yaml_conf.apisix.proxy_cache then
error("missing apisix.proxy_cache for plugin proxy-cache")
end

-- Using template.render
local sys_conf = {
lua_path = pkg_path_org,
Expand All @@ -694,6 +708,7 @@ local function init()
apisix_lua_home = apisix_home,
with_module_status = with_module_status,
error_log = {level = "warn"},
enabled_plugins = enabled_plugins,
}

if not yaml_conf.apisix then
Expand Down
4 changes: 2 additions & 2 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ plugins: # plugin list
- fault-injection
- udp-logger
- wolf-rbac
- proxy-cache
- tcp-logger
- proxy-mirror
- kafka-logger
- cors
- consumer-restriction
Expand All @@ -177,6 +175,8 @@ plugins: # plugin list
- authz-keycloak
- uri-blocker
- request-validation
# - proxy-cache
# - proxy-mirror

stream_plugins:
- mqtt-proxy
2 changes: 2 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ my $test2_key = read_file("conf/cert/test2.key");
$yaml_config =~ s/node_listen: 9080/node_listen: 1984/;
$yaml_config =~ s/ # stream_proxy:/ stream_proxy:\n tcp:\n - 9100/;
$yaml_config =~ s/admin_key:/disable_admin_key:/;
$yaml_config =~ s/ # - proxy-cache/ - proxy-cache/;
$yaml_config =~ s/ # - proxy-mirror/ - proxy-mirror/;

my $etcd_enable_auth = $ENV{"ETCD_ENABLE_AUTH"} || "false";

Expand Down
2 changes: 2 additions & 0 deletions t/debug/debug-mode.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ sub read_file($) {
our $yaml_config = read_file("conf/config.yaml");
$yaml_config =~ s/node_listen: 9080/node_listen: 1984/;
$yaml_config =~ s/enable_debug: false/enable_debug: true/;
$yaml_config =~ s/ # - proxy-cache/ - proxy-cache/;
$yaml_config =~ s/ # - proxy-mirror/ - proxy-mirror/;


run_tests;
Expand Down

0 comments on commit d32b5cf

Please sign in to comment.