Skip to content

Commit e27c8fd

Browse files
authored
add flag --keepalive_timeout (#727)
1 parent abd01fa commit e27c8fd

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

start_esp/nginx-auto.conf.template

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ http {
5454
% if large_client_header_buffers:
5555
large_client_header_buffers ${large_client_header_buffers};
5656
% endif
57+
% if keepalive_timeout:
58+
keepalive_timeout ${keepalive_timeout};
59+
% endif
5760

5861
# HTTP subrequests
5962
endpoints_resolver ${resolver};

start_esp/start_esp.py

+8
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def write_nginx_conf(ingress, nginx_conf, args):
151151
client_max_body_size=args.client_max_body_size,
152152
client_body_buffer_size=args.client_body_buffer_size,
153153
large_client_header_buffers=args.large_client_header_buffers,
154+
keepalive_timeout=args.keepalive_timeout,
154155
worker_processes=args.worker_processes,
155156
cors_preset=args.cors_preset,
156157
cors_allow_origin=args.cors_allow_origin,
@@ -643,6 +644,11 @@ def make_argparser():
643644
--large_client_header_buffers="4 32k"
644645
''')
645646

647+
parser.add_argument('--keepalive_timeout', default=None, help='''
648+
Sets the server keepalive timeout. This flag will pass to Nginx config directly.
649+
http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout.
650+
''')
651+
646652
parser.add_argument('--rewrite', action='append', help=
647653
'''Internally redirect the request uri with a pair of pattern and
648654
replacement. Pattern and replacement should be separated by whitespace.
@@ -978,6 +984,8 @@ def enforce_conflict_args(args):
978984
return "Flag --enable_backend_routing cannot be used together with --client_max_body_size."
979985
if args.large_client_header_buffers is not None:
980986
return "Flag --enable_backend_routing cannot be used together with --large_client_header_buffers."
987+
if args.keepalive_timeout is not None:
988+
return "Flag --enable_backend_routing cannot be used together with --keepalive_timeout."
981989
if args.generate_self_signed_cert:
982990
return "Flag --enable_backend_routing cannot be used together with --generate_self_signed_cert."
983991
if args.enable_strict_transport_security:

start_esp/test/start_esp_test.py

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ def test_large_client_header_buffers_output_is_as_expected(self):
148148
config_generator = self.basic_config_generator + " --large_client_header_buffers='4 32k'"
149149
self.run_test_with_expectation(expected_config_file, self.generated_nginx_config_file, config_generator)
150150

151+
def test_keepalive_timeout_output_is_as_expected(self):
152+
expected_config_file = "./start_esp/test/testdata/expected_keepalive_timeout_nginx.conf"
153+
config_generator = self.basic_config_generator + " --keepalive_timeout=600s"
154+
self.run_test_with_expectation(expected_config_file, self.generated_nginx_config_file, config_generator)
155+
151156
def test_allow_invalid_headers_arg_output_is_as_expected(self):
152157
expected_config_file = "./start_esp/test/testdata/expected_allow_invalid_headers_nginx.conf"
153158
config_generator = self.basic_config_generator + " --allow_invalid_headers"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Auto-generated by start_esp
2+
# Copyright (C) Extensible Service Proxy Authors
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions
7+
# are met:
8+
# 1. Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
# 2. Redistributions in binary form must reproduce the above copyright
11+
# notice, this list of conditions and the following disclaimer in the
12+
# documentation and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
# SUCH DAMAGE.
25+
26+
daemon off;
27+
28+
user nginx nginx;
29+
30+
pid ./start_esp/test/pid_file;
31+
32+
# Worker/connection processing limits
33+
worker_processes 1;
34+
worker_rlimit_nofile 10240;
35+
events { worker_connections 10240; }
36+
37+
# Logging to stderr enables better integration with Docker and GKE/Kubernetes.
38+
error_log stderr warn;
39+
40+
http {
41+
include /etc/nginx/mime.types;
42+
include /etc/nginx/conf/*.conf;
43+
server_tokens off;
44+
client_max_body_size 32m;
45+
client_body_buffer_size 128k;
46+
keepalive_timeout 600s;
47+
48+
# HTTP subrequests
49+
endpoints_resolver 8.8.8.8;
50+
endpoints_certificates /etc/nginx/trusted-ca-certificates.crt;
51+
52+
upstream app_server0 {
53+
server 127.0.0.1:8081;
54+
keepalive 128;
55+
}
56+
57+
set_real_ip_from 0.0.0.0/0;
58+
set_real_ip_from 0::/0;
59+
real_ip_header X-Forwarded-For;
60+
real_ip_recursive on;
61+
62+
63+
server {
64+
server_name "";
65+
resolver 8.8.8.8;
66+
67+
68+
listen 8080 backlog=16384;
69+
70+
access_log /dev/stdout;
71+
72+
73+
74+
75+
location / {
76+
# Begin Endpoints v2 Support
77+
endpoints {
78+
on;
79+
server_config /etc/nginx/server_config.pb.txt;
80+
google_authentication_secret key;
81+
metadata_server http://169.254.169.254;
82+
}
83+
# End Endpoints v2 Support
84+
85+
86+
87+
proxy_pass http://app_server0;
88+
proxy_redirect off;
89+
proxy_set_header Host $host;
90+
proxy_set_header X-Real-IP $remote_addr;
91+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
92+
proxy_set_header X-Forwarded-Host $server_name;
93+
proxy_set_header X-Google-Real-IP $remote_addr;
94+
95+
# Enable the upstream persistent connection
96+
proxy_http_version 1.1;
97+
proxy_set_header Connection "";
98+
99+
# 86400 seconds (24 hours) is the maximum a server is allowed.
100+
proxy_send_timeout 86400s;
101+
proxy_read_timeout 86400s;
102+
}
103+
104+
include /var/lib/nginx/extra/*.conf;
105+
}
106+
107+
server {
108+
# expose /nginx_status and /endpoints_status but on a different port to
109+
# avoid external visibility / conflicts with the app.
110+
listen 8090;
111+
location /nginx_status {
112+
stub_status on;
113+
access_log off;
114+
}
115+
location /endpoints_status {
116+
endpoints_status;
117+
access_log off;
118+
}
119+
location /healthz {
120+
return 200;
121+
access_log off;
122+
}
123+
location / {
124+
root /dev/null;
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)