-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnginx.tmpl
236 lines (195 loc) · 8.23 KB
/
nginx.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
worker_processes {{ .WorkerProcesses }};
daemon off;
error_log stderr {{ .LogLevel }};
pid {{ .WorkingDir }}/nginx.pid;
{{ if .OpenTracingPlugin }}
load_module modules/ngx_http_opentracing_module.so;
{{ end }}
events {
# Accept connections as fast as possible.
multi_accept on;
# Includes both proxy and client connections.
# So e.g. 4096 = 2048 persistent client connections to backends per worker.
worker_connections {{ .WorkerConnections }};
# Use most optimal non-blocking selector on linux.
# Should be selected by default on linux, we just make it explicit here.
use epoll;
}
http {
default_type text/html;
# Track extended virtual host stats.
vhost_traffic_status_zone shared:vhost_traffic_status:{{ .VhostStatsSharedMemory }}m;
# Server names hash bucket sizes. Set based on nginx log messages.
{{ if gt .ServerNamesHashBucketSize 0 }}server_names_hash_bucket_size {{ .ServerNamesHashBucketSize }};{{ end }}
{{ if gt .ServerNamesHashMaxSize 0 }}server_names_hash_max_size {{ .ServerNamesHashMaxSize }};{{ end }}
# Keep alive time for client connections. Don't limit by number of requests.
keepalive_timeout {{ .KeepaliveSeconds }}s;
keepalive_requests 2147483647;
# Optimize for latency over throughput for persistent connections.
tcp_nodelay on;
# Disable nginx version leakage to external clients.
server_tokens off;
{{ if .ClientHeaderBufferSize }}
# Sets buffer size for reading client request header.
client_header_buffer_size {{ .ClientHeaderBufferSize }}k;
{{ end }}
{{ if .ClientBodyBufferSize }}
# Sets buffer size for reading client request bodies.
client_body_buffer_size {{ .ClientBodyBufferSize }}k;
{{ end }}
{{ if .LargeClientHeaderBufferBlocks }}
{{ if .ClientHeaderBufferSize }}
# Sets the maximum number of buffers used for reading large client request header.
# The size would be same as client_header_buffer_size. A request line cannot exceed the size of one buffer
large_client_header_buffers {{ .LargeClientHeaderBufferBlocks }} {{ .ClientHeaderBufferSize }}k;
{{ end }}
{{ end }}
# Obtain client IP from frontend
{{ range .TrustedFrontends }} set_real_ip_from {{ . }};
{{ end }}
real_ip_header {{ if .ProxyProtocol }}proxy_protocol{{ else }}X-Forwarded-For{{ end }};
real_ip_recursive on;
# Log format tracking timings
log_format upstream_info '$remote_addr - $remote_user [$time_iso8601] '
'"$request" $status{{.AccessLogHeaders}} $body_bytes_sent'
'"$http_referer" "$http_user_agent" '
'"$host" uip="$upstream_addr" ust="$upstream_status" '
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
# Access logs
access_log {{ if .AccessLog }}{{ .AccessLogDir }}/access.log upstream_info buffer=32k flush=1m{{ else }}off{{ end }};
# Disable all logging of 404s - to prevent spam when error log is enabled.
log_not_found off;
# Enable keepalive to backend.
proxy_http_version 1.1;
proxy_set_header Connection "";
# Mitigate httpoxy vulnerability.
proxy_set_header Proxy "";
# Add headers for proxy information.
map $http_x_forwarded_proto $frontend_scheme {
default $http_x_forwarded_proto;
'' $scheme;
}
map $http_x_forwarded_port $frontend_port {
default $http_x_forwarded_port;
'' $server_port;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host:$frontend_port;
proxy_set_header X-Forwarded-Proto $frontend_scheme;
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
# Timeout to backend services on initial connect.
proxy_connect_timeout {{ .BackendConnectTimeoutSeconds }}s;
# Disable buffering, as we'll be interacting with ELBs with http listeners, which we assume will
# quickly consume and generate responses and requests.
# This should be enabled if nginx will directly serve traffic externally to unknown clients.
proxy_buffering off;
# Don't mess with redirects.
proxy_redirect off;
{{ if .OpenTracingPlugin }}
# Load a vendor tracer
opentracing_load_tracer {{ .OpenTracingPlugin }} {{ .OpenTracingConfig }};
opentracing on;
opentracing_propagate_context;
opentracing_trace_locations off;
{{ end }}
# Start ingresses
{{- $keepalive := .BackendKeepalives }}
{{- $proxyprotocol := .ProxyProtocol }}
{{- range $upstream := .Upstreams }}
upstream {{ $upstream.ID }} {
server {{ $upstream.Server }} max_conns={{ $upstream.MaxConnections }};
keepalive {{ $keepalive }};
}
{{ end }}
{{- $IngressPorts := .Ports }}
{{- $SSLPath := .SSLPath }}
{{define "HTTPSConf"}}
# https://mozilla.github.io/server-side-tls/ssl-config-generator/ - Nginx, Modern Profile + TLSv1, TLSv1.1
ssl_certificate {{ . }}.crt;
ssl_certificate_key {{ . }}.key;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
{{ end }}
{{- range $entry := .Servers }}
# ingress: {{ $entry.Name }}
{{- range $portConf := $IngressPorts }}
server {
listen {{ $portConf.Port }}{{- if eq $portConf.Name "https" }} ssl{{ end }}{{ if $proxyprotocol }} proxy_protocol{{ end }};
server_name {{ $entry.ServerName }};
{{- if eq $portConf.Name "https" }}
{{ template "HTTPSConf" $SSLPath }}
{{- end }}
# disable any limits to avoid HTTP 413 for large uploads
client_max_body_size 0;
{{- range $location := $entry.Locations }}
location {{ if $location.Path }}{{ $location.Path }}{{ end }} {
{{- if $location.StripPath }}
# Strip location path when proxying.
# Beware this can cause issues with url encoded characters.
proxy_pass http://{{ $location.UpstreamID }}/;
{{- else }}
# Keep original path when proxying.
proxy_pass http://{{ $location.UpstreamID }};
{{- end }}
# Set display name for vhost stats.
vhost_traffic_status_filter_by_set_key {{ $location.Path }}::$proxy_host $server_name;
# Close proxy connections after backend keepalive time.
proxy_read_timeout {{ $location.BackendTimeoutSeconds }}s;
proxy_send_timeout {{ $location.BackendTimeoutSeconds }}s;
proxy_buffer_size {{ $location.ProxyBufferSize }}k;
proxy_buffers {{ $location.ProxyBufferBlocks }} {{ $location.ProxyBufferSize }}k;
# Allow localhost for debugging
allow 127.0.0.1;
# Restrict clients
{{ range $location.Allow }}allow {{ . }};
{{ end }}
deny all;
}
{{- end }}
}
{{- end }}
{{- end }}
# End ingresses
# Default backend
{{- range $portConf := $IngressPorts }}
server {
listen {{ $portConf.Port }}{{- if eq $portConf.Name "https" }} ssl{{ end }} default_server;
{{- if eq $portConf.Name "https" }}
{{ template "HTTPSConf" $SSLPath }}
{{- end }}
location / {
return 404;
}
}
{{- end }}
# Status port. This should be firewalled to only allow internal access.
server {
{{ if .OpenTracingPlugin }}
opentracing off;
{{ end }}
listen {{ .HealthPort }} default_server reuseport;
vhost_traffic_status off;
location /health {
access_log off;
return 200;
}
location /basic_status {
access_log off;
stub_status;
}
location /status {
access_log off;
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
location / {
return 404;
}
}
}