2017-08-03 14:51:39 +00:00
{{ $ all := . }}
2017-08-17 17:05:01 +00:00
{{ $ servers := .Servers }}
2017-04-28 20:41:53 +00:00
{{ $ cfg := .Cfg }}
2017-04-09 18:03:27 +00:00
{{ $ IsIPV6Enabled := .IsIPV6Enabled }}
2017-02-26 22:01:07 +00:00
{{ $ healthzURI := .HealthzURI }}
{{ $ backends := .Backends }}
{{ $ proxyHeaders := .ProxySetHeaders }}
2017-05-18 10:21:03 +00:00
{{ $ addHeaders := .AddHeaders }}
2017-10-08 14:52:02 +00:00
{{ if $ cfg . EnableModsecurity }}
load_module /etc/nginx/modules/ngx_http_modsecurity_module.so;
{{ end }}
2017-10-24 20:49:30 +00:00
{{ if $ cfg . EnableOpentracing }}
load_module /etc/nginx/modules/ngx_http_opentracing_module.so;
{{ end }}
{{ if (and $ cfg . EnableOpentracing (ne $ cfg . ZipkinCollectorHost "")) }}
load_module /etc/nginx/modules/ngx_http_zipkin_module.so;
{{ end }}
2016-02-22 00:13:08 +00:00
daemon off;
2016-11-16 18:24:26 +00:00
worker_processes {{ $ cfg . WorkerProcesses }};
2016-02-22 00:13:08 +00:00
pid /run/nginx.pid;
2017-01-19 02:31:33 +00:00
{{ if ne .MaxOpenFiles 0 }}
worker_rlimit_nofile {{ .MaxOpenFiles }};
{{ end}}
2016-02-22 00:13:08 +00:00
2017-08-08 18:17:49 +00:00
{{/* http://nginx.org/en/docs/ngx_core_module.html #worker _shutdown_timeout * / } }
{{/* avoid waiting too long during a reload */}}
2017-08-29 09:49:18 +00:00
worker_shutdown_timeout {{ $ cfg . WorkerShutdownTimeout }} ;
2017-08-08 18:17:49 +00:00
2016-02-22 00:13:08 +00:00
events {
2016-03-22 18:01:04 +00:00
multi_accept on;
2016-11-16 18:24:26 +00:00
worker_connections {{ $ cfg . MaxWorkerConnections }};
2017-03-03 01:44:45 +00:00
use epoll;
2016-02-22 00:13:08 +00:00
}
http {
2016-06-17 22:26:08 +00:00
{{/* we use the value of the header X-Forwarded-For to be able to use the geo_ip module */}}
2016-11-16 18:24:26 +00:00
{{ if $ cfg . UseProxyProtocol }}
2016-07-05 16:37:54 +00:00
real_ip_header proxy_protocol;
{{ else }}
2017-09-07 21:11:23 +00:00
real_ip_header {{ $ cfg . ForwardedForHeader }};
2016-11-10 22:56:29 +00:00
{{ end }}
2017-03-03 01:44:45 +00:00
2016-06-17 22:26:08 +00:00
real_ip_recursive on;
2017-08-10 15:41:14 +00:00
{{ range $ trusted_ip := $ cfg . ProxyRealIPCIDR }}
set_real_ip_from {{ $ trusted_ip }};
{{ end }}
2016-06-17 22:26:08 +00:00
2016-05-30 18:44:02 +00:00
{{/* databases used to determine the country depending on the client IP address */}}
{{/* http://nginx.org/en/docs/http/ngx_http_geoip_module.html */}}
{{/* this is require to calculate traffic for individual country using GeoIP in the status page */}}
geoip_country /etc/nginx/GeoIP.dat;
geoip_city /etc/nginx/GeoLiteCity.dat;
2016-06-17 22:26:08 +00:00
geoip_proxy_recursive on;
2016-05-30 18:44:02 +00:00
2016-11-16 18:24:26 +00:00
{{ if $ cfg . EnableVtsStatus }}
vhost_traffic_status_zone shared:vhost_traffic_status:{{ $ cfg . VtsStatusZoneSize }};
2017-09-15 08:13:04 +00:00
vhost_traffic_status_filter_by_set_key {{ $ cfg . VtsDefaultFilterKey }};
2016-11-10 22:56:29 +00:00
{{ end }}
2016-02-22 00:13:08 +00:00
2016-03-22 18:01:04 +00:00
sendfile on;
2017-09-08 21:00:52 +00:00
2016-03-22 18:01:04 +00:00
aio threads;
2017-09-07 16:40:10 +00:00
aio_write on;
2016-03-22 18:01:04 +00:00
tcp_nopush on;
tcp_nodelay on;
2017-03-03 01:44:45 +00:00
2016-03-22 18:01:04 +00:00
log_subrequest on;
reset_timedout_connection on;
2016-02-22 00:13:08 +00:00
2017-05-17 06:23:18 +00:00
keepalive_timeout {{ $ cfg . KeepAlive }}s;
keepalive_requests {{ $ cfg . KeepAliveRequests }};
2016-02-22 00:13:08 +00:00
2017-01-20 22:14:59 +00:00
client_header_buffer_size {{ $ cfg . ClientHeaderBufferSize }};
2017-08-24 12:30:12 +00:00
client_header_timeout {{ $ cfg . ClientHeaderTimeout }}s;
2017-01-20 22:14:59 +00:00
large_client_header_buffers {{ $ cfg . LargeClientHeaderBuffers }};
2017-05-17 06:23:18 +00:00
client_body_buffer_size {{ $ cfg . ClientBodyBufferSize }};
2017-08-24 12:30:12 +00:00
client_body_timeout {{ $ cfg . ClientBodyTimeout }}s;
2017-04-28 20:41:53 +00:00
2017-03-10 13:01:26 +00:00
http2_max_field_size {{ $ cfg . HTTP2MaxFieldSize }};
http2_max_header_size {{ $ cfg . HTTP2MaxHeaderSize }};
2017-01-20 22:14:59 +00:00
2016-11-10 22:56:29 +00:00
types_hash_max_size 2048;
2016-11-16 18:24:26 +00:00
server_names_hash_max_size {{ $ cfg . ServerNameHashMaxSize }};
server_names_hash_bucket_size {{ $ cfg . ServerNameHashBucketSize }};
map_hash_bucket_size {{ $ cfg . MapHashBucketSize }};
2016-02-22 00:13:08 +00:00
2017-07-13 11:45:02 +00:00
proxy_headers_hash_max_size {{ $ cfg . ProxyHeadersHashMaxSize }};
proxy_headers_hash_bucket_size {{ $ cfg . ProxyHeadersHashBucketSize }};
2017-05-28 20:05:49 +00:00
variables_hash_bucket_size {{ $ cfg . VariablesHashBucketSize }};
variables_hash_max_size {{ $ cfg . VariablesHashMaxSize }};
2017-05-24 04:25:42 +00:00
underscores_in_headers {{ if $ cfg . EnableUnderscoresInHeaders }}on{{ else }}off{{ end }};
ignore_invalid_headers {{ if $ cfg . IgnoreInvalidHeaders }}on{{ else }}off{{ end }};
2017-04-20 21:12:16 +00:00
2017-10-24 20:49:30 +00:00
{{ if $ cfg . EnableOpentracing }}
2017-09-17 21:44:01 +00:00
opentracing on;
2017-10-24 20:49:30 +00:00
{{ end }}
2017-09-17 21:44:01 +00:00
2017-10-24 20:49:30 +00:00
{{ if (and $ cfg . EnableOpentracing (ne $ cfg . ZipkinCollectorHost "")) }}
2017-09-17 21:44:01 +00:00
zipkin_collector_host {{ $ cfg . ZipkinCollectorHost }};
zipkin_collector_port {{ $ cfg . ZipkinCollectorPort }};
zipkin_service_name {{ $ cfg . ZipkinServiceName }};
{{ end }}
2016-02-22 00:13:08 +00:00
include /etc/nginx/mime.types;
2016-04-02 20:41:41 +00:00
default_type text/html;
2017-10-30 19:32:47 +00:00
2017-11-12 13:33:18 +00:00
{{ if $ cfg . EnableBrotli }}
2017-11-02 01:54:22 +00:00
brotli on;
brotli_comp_level {{ $ cfg . BrotliLevel }};
brotli_types {{ $ cfg . BrotliTypes }};
2017-10-30 19:32:47 +00:00
{{ end }}
2016-11-16 18:24:26 +00:00
{{ if $ cfg . UseGzip }}
2016-02-22 00:13:08 +00:00
gzip on;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_min_length 256;
2017-03-03 01:44:45 +00:00
gzip_types {{ $ cfg . GzipTypes }};
2016-02-22 00:13:08 +00:00
gzip_proxied any;
2017-10-29 13:54:25 +00:00
gzip_vary on;
2016-11-10 22:56:29 +00:00
{{ end }}
2016-02-22 00:13:08 +00:00
2017-05-18 10:21:03 +00:00
# Custom headers for response
{{ range $k, $v := $ addHeaders }}
add_header {{ $k }} "{{ $v }}";
{{ end }}
2017-02-27 10:00:31 +00:00
server_tokens {{ if $ cfg . ShowServerTokens }}on{{ else }}off{{ end }};
2018-01-17 12:26:53 +00:00
{{ if not $ cfg . ShowServerTokens }}
more_set_headers "Server: ";
{{ end }}
2016-02-22 00:13:08 +00:00
2017-05-11 18:04:19 +00:00
# disable warnings
uninitialized_variable_warn off;
2017-08-25 23:49:44 +00:00
# Additional available variables:
# $ namespace
# $ ingress_name
# $ service_name
2017-06-15 03:05:04 +00:00
log_format upstreaminfo {{ if $ cfg . LogFormatEscapeJSON }}escape=json {{ end }}'{{ buildLogFormatUpstream $ cfg }}';
2016-02-22 00:13:08 +00:00
2016-06-18 22:04:07 +00:00
{{/* map urls that should not appear in access.log */}}
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html #access _log * / } }
2017-02-17 21:21:46 +00:00
map $ request_uri $ loggable {
2016-11-16 18:24:26 +00:00
{{ range $ reqUri := $ cfg . SkipAccessLogURLs }}
2016-06-18 22:04:07 +00:00
{{ $ reqUri }} 0;{{ end }}
default 1;
}
2017-02-09 23:20:12 +00:00
{{ if $ cfg . DisableAccessLog }}
access_log off;
{{ else }}
2017-08-23 14:57:28 +00:00
access_log {{ $ cfg . AccessLogPath }} upstreaminfo if= $ loggable ;
2017-02-09 23:20:12 +00:00
{{ end }}
2017-08-23 14:57:28 +00:00
error_log {{ $ cfg . ErrorLogPath }} {{ $ cfg . ErrorLogLevel }};
2016-02-22 00:13:08 +00:00
2016-12-22 03:00:27 +00:00
{{ buildResolvers $ cfg . Resolver }}
2016-02-22 00:13:08 +00:00
2016-09-29 21:42:46 +00:00
{{/* Whenever nginx proxies a request without a "Connection" header, the "Connection" header is set to "close" */}}
{{/* when making the target request. This means that you cannot simply use */}}
{{/* "proxy_set_header Connection $ http_connection " for WebSocket support because in this case, the */}}
{{/* "Connection" header would be set to "" whenever the original request did not have a "Connection" header, */}}
{{/* which would mean no "Connection" header would be in the target request. Since this would deviate from */}}
{{/* normal nginx behavior we have to use this approach. */}}
# Retain the default nginx handling of requests without a "Connection" header
2016-02-22 00:13:08 +00:00
map $ http_upgrade $ connection_upgrade {
2016-11-16 18:24:26 +00:00
default upgrade;
'' close;
2016-02-22 00:13:08 +00:00
}
2017-09-17 18:03:05 +00:00
map {{ buildForwardedFor $ cfg . ForwardedForHeader }} $ the_real_ip {
2017-10-06 01:55:10 +00:00
{{ if $ cfg . UseProxyProtocol }}
# Get IP address from Proxy Protocol
default $ proxy_protocol_addr ;
{{ else }}
default $ remote_addr ;
{{ end }}
2017-09-17 18:03:05 +00:00
}
2016-02-22 00:13:08 +00:00
# trust http_x_forwarded_proto headers correctly indicate ssl offloading
2016-03-26 21:25:51 +00:00
map $ http_x_forwarded_proto $ pass_access_scheme {
2016-11-16 18:24:26 +00:00
default $ http_x_forwarded_proto ;
'' $ scheme ;
2016-02-22 00:13:08 +00:00
}
2017-12-21 15:44:08 +00:00
# validate $ pass_access_scheme and $ scheme are http to force a redirect
map " $ scheme : $ pass_access_scheme " $ redirect_to_https {
default 0;
"http:http" 1;
2017-12-27 01:53:43 +00:00
"http:https" 1;
2017-12-21 15:44:08 +00:00
}
2016-12-26 13:56:22 +00:00
map $ http_x_forwarded_port $ pass_server_port {
2017-09-17 18:03:05 +00:00
default $ http_x_forwarded_port ;
'' $ server_port ;
2016-12-26 13:56:22 +00:00
}
2017-09-17 18:03:05 +00:00
map $ http_x_forwarded_host $ best_http_host {
default $ http_x_forwarded_host ;
'' $ this_host ;
}
2017-08-21 01:34:31 +00:00
{{ if $ all . IsSSLPassthroughEnabled }}
2017-08-24 13:33:26 +00:00
# map port {{ $ all . ListenPorts . SSLProxy }} to 443 for header X-Forwarded-Port
2016-12-26 13:56:22 +00:00
map $ pass_server_port $ pass_port {
2017-08-24 13:33:26 +00:00
{{ $ all . ListenPorts . SSLProxy }} 443;
2016-12-26 13:56:22 +00:00
default $ pass_server_port ;
}
2017-08-21 01:34:31 +00:00
{{ else }}
map $ pass_server_port $ pass_port {
2018-01-02 17:48:42 +00:00
{{ $ all . ListenPorts . HTTPS }} 443;
2017-08-21 01:34:31 +00:00
default $ pass_server_port ;
}
{{ end }}
2016-12-26 13:56:22 +00:00
2017-03-16 08:46:29 +00:00
# Obtain best http host
2017-06-27 14:08:14 +00:00
map $ http_host $ this_host {
2017-03-16 08:46:29 +00:00
default $ http_host ;
'' $ host ;
}
2017-10-09 09:10:58 +00:00
{{ if $ cfg . ComputeFullForwardedFor }}
2017-10-06 12:52:26 +00:00
# We can't use $ proxy_add_x_forwarded_for because the realip module
2017-10-09 09:10:58 +00:00
# replaces the remote_addr too soon
map $ http_x_forwarded_for $ full_x_forwarded_for {
2017-10-28 15:02:16 +00:00
{{ if $ all . Cfg . UseProxyProtocol }}
default " $ http_x_forwarded_for , $ proxy_protocol_addr ";
'' " $ proxy_protocol_addr ";
{{ else }}
2017-10-06 12:52:26 +00:00
default " $ http_x_forwarded_for , $ realip_remote_addr ";
'' " $ realip_remote_addr ";
2017-10-28 15:02:16 +00:00
{{ end}}
2017-10-06 12:52:26 +00:00
}
2017-10-09 09:10:58 +00:00
{{ end }}
2017-10-06 12:52:26 +00:00
2016-02-22 00:13:08 +00:00
server_name_in_redirect off;
2016-11-16 18:24:26 +00:00
port_in_redirect off;
2016-02-22 00:13:08 +00:00
2016-11-16 18:24:26 +00:00
ssl_protocols {{ $ cfg . SSLProtocols }};
2016-02-22 00:13:08 +00:00
# turn on session caching to drastically improve performance
2016-11-16 18:24:26 +00:00
{{ if $ cfg . SSLSessionCache }}
ssl_session_cache builtin:1000 shared:SSL:{{ $ cfg . SSLSessionCacheSize }};
ssl_session_timeout {{ $ cfg . SSLSessionTimeout }};
2016-02-22 00:13:08 +00:00
{{ end }}
# allow configuring ssl session tickets
2016-11-16 18:24:26 +00:00
ssl_session_tickets {{ if $ cfg . SSLSessionTickets }}on{{ else }}off{{ end }};
2016-02-22 00:13:08 +00:00
2017-10-08 22:37:19 +00:00
{{ if not (empty $ cfg . SSLSessionTicketKey ) }}
ssl_session_ticket_key /etc/nginx/tickets.key;
{{ end }}
2016-02-22 00:13:08 +00:00
# slightly reduce the time-to-first-byte
2016-11-16 18:24:26 +00:00
ssl_buffer_size {{ $ cfg . SSLBufferSize }};
2016-02-22 00:13:08 +00:00
2016-11-16 18:24:26 +00:00
{{ if not (empty $ cfg . SSLCiphers ) }}
2016-02-22 00:13:08 +00:00
# allow configuring custom ssl ciphers
2016-11-16 18:24:26 +00:00
ssl_ciphers '{{ $ cfg . SSLCiphers }}';
2016-02-22 00:13:08 +00:00
ssl_prefer_server_ciphers on;
{{ end }}
2016-11-16 18:24:26 +00:00
{{ if not (empty $ cfg . SSLDHParam ) }}
2016-02-22 00:13:08 +00:00
# allow custom DH file http://nginx.org/en/docs/http/ngx_http_ssl_module.html #ssl _dhparam
2016-11-16 18:24:26 +00:00
ssl_dhparam {{ $ cfg . SSLDHParam }};
2016-02-22 00:13:08 +00:00
{{ end }}
2016-11-16 18:24:26 +00:00
{{ if not $ cfg . EnableDynamicTLSRecords }}
2016-06-18 21:03:27 +00:00
ssl_dyn_rec_size_lo 0;
{{ end }}
2017-03-31 02:23:14 +00:00
ssl_ecdh_curve {{ $ cfg . SSLECDHCurve }};
2016-11-16 18:24:26 +00:00
{{ if .CustomErrors }}
2016-03-15 15:31:39 +00:00
# Custom error pages
2016-02-22 00:13:08 +00:00
proxy_intercept_errors on;
2016-06-01 14:39:12 +00:00
{{ end }}
2016-02-22 00:13:08 +00:00
2016-11-16 18:24:26 +00:00
{{ range $ errCode := $ cfg . CustomHTTPErrors }}
2016-06-01 14:39:12 +00:00
error_page {{ $ errCode }} = @custom_{{ $ errCode }};{{ end }}
2016-02-22 00:13:08 +00:00
2017-04-25 01:14:38 +00:00
proxy_ssl_session_reuse on;
2017-05-28 21:40:25 +00:00
{{ if $ cfg . AllowBackendServerHeader }}
proxy_pass_header Server;
{{ end }}
2017-10-01 14:45:33 +00:00
{{ if not (empty $ cfg . HTTPSnippet ) }}
# Custom code snippet configured in the configuration configmap
2017-10-05 01:51:02 +00:00
{{ $ cfg . HTTPSnippet }}
2017-10-01 14:45:33 +00:00
{{ end }}
2017-06-16 00:43:17 +00:00
{{ range $ name , $ upstream := $ backends }}
{{ if eq $ upstream . SessionAffinity . AffinityType "cookie" }}
upstream sticky-{{ $ upstream . Name }} {
sticky hash={{ $ upstream . SessionAffinity . CookieSessionAffinity . Hash }} name={{ $ upstream . SessionAffinity . CookieSessionAffinity . Name }} httponly;
{{ if (gt $ cfg . UpstreamKeepaliveConnections 0) }}
keepalive {{ $ cfg . UpstreamKeepaliveConnections }};
{{ end }}
{{ range $ server := $ upstream . Endpoints }}server {{ $ server . Address | formatIP }}:{{ $ server . Port }} max_fails={{ $ server . MaxFails }} fail_timeout={{ $ server . FailTimeout }};
{{ end }}
2017-09-29 12:57:16 +00:00
2017-06-16 00:43:17 +00:00
}
2017-08-25 23:49:44 +00:00
2017-06-16 00:43:17 +00:00
{{ end }}
2017-09-29 12:57:16 +00:00
2017-06-16 00:43:17 +00:00
upstream {{ $ upstream . Name }} {
2017-09-30 21:29:16 +00:00
{{ if $ upstream . UpstreamHashBy }}
hash {{ $ upstream . UpstreamHashBy }} consistent;
2017-12-27 10:48:06 +00:00
{{ else }}
# Load balance algorithm; empty for round robin, which is the default
{{ if ne $ cfg . LoadBalanceAlgorithm "round_robin" }}{{ $ cfg . LoadBalanceAlgorithm }};{{ end }}
2017-09-30 21:29:16 +00:00
{{ end }}
2017-06-22 03:39:06 +00:00
{{ if (gt $ cfg . UpstreamKeepaliveConnections 0) }}
keepalive {{ $ cfg . UpstreamKeepaliveConnections }};
{{ end }}
2017-06-09 03:11:00 +00:00
{{ range $ server := $ upstream . Endpoints }}server {{ $ server . Address | formatIP }}:{{ $ server . Port }} max_fails={{ $ server . MaxFails }} fail_timeout={{ $ server . FailTimeout }};
2016-04-28 04:03:59 +00:00
{{ end }}
2016-03-15 15:31:39 +00:00
}
2017-08-25 23:49:44 +00:00
2016-05-16 20:29:33 +00:00
{{ end }}
2016-03-15 15:31:39 +00:00
2017-05-20 23:32:03 +00:00
{{/* build the maps that will be use to validate the Whitelist */}}
2017-08-17 17:05:01 +00:00
{{ range $ index , $ server := $ servers }}
2017-05-20 23:32:03 +00:00
{{ range $ location := $ server . Locations }}
{{ $ path := buildLocation $ location }}
{{ if isLocationAllowed $ location }}
{{ if gt (len $ location . Whitelist . CIDR ) 0 }}
2017-08-22 12:33:56 +00:00
# Deny for {{ print $ server . Hostname $ path }}
2017-06-02 03:30:22 +00:00
geo $ the_real_ip {{ buildDenyVariable (print $ server . Hostname "_" $ path ) }} {
2017-05-20 23:32:03 +00:00
default 1;
{{ range $ ip := $ location . Whitelist . CIDR }}
{{ $ ip }} 0;{{ end }}
}
{{ end }}
{{ end }}
2017-08-23 00:47:29 +00:00
{{ end }}
{{ end }}
2017-08-22 12:33:56 +00:00
2017-08-23 00:47:29 +00:00
{{ range $ rl := (filterRateLimits $ servers ) }}
# Ratelimit {{ $ rl . Name }}
2017-09-08 21:00:52 +00:00
geo $ the_real_ip $ whitelist_ {{ $ rl . ID }} {
2017-08-21 19:36:31 +00:00
default 0;
2017-08-23 00:47:29 +00:00
{{ range $ ip := $ rl . Whitelist }}
2017-08-21 19:36:31 +00:00
{{ $ ip }} 1;{{ end }}
}
2017-08-23 00:47:29 +00:00
# Ratelimit {{ $ rl . Name }}
map $ whitelist_ {{ $ rl . ID }} $ limit_ {{ $ rl . ID }} {
2017-08-21 19:36:31 +00:00
0 {{ $ cfg . LimitConnZoneVariable }};
1 "";
}
{{ end }}
2017-05-20 23:32:03 +00:00
2016-05-27 21:03:54 +00:00
{{/* build all the required rate limit zones. Each annotation requires a dedicated zone */}}
{{/* 1MB -> 16 thousand 64-byte states or about 8 thousand 128-byte states */}}
2017-08-21 19:36:31 +00:00
{{ range $ zone := (buildRateLimitZones $ servers ) }}
2016-05-27 21:03:54 +00:00
{{ $ zone }}
{{ end }}
2017-08-19 21:13:02 +00:00
{{/* Build server redirects (from/to www) */}}
{{ range $ hostname , $ to := .RedirectServers }}
server {
2017-08-25 02:24:32 +00:00
{{ range $ address := $ all . Cfg . BindAddressIpv4 }}
listen {{ $ address }}:{{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }};
listen {{ $ address }}:{{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol{{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }} ssl;
{{ else }}
2017-08-24 13:33:26 +00:00
listen {{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }};
listen {{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol{{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }} ssl;
2017-08-25 02:24:32 +00:00
{{ end }}
2017-08-19 21:13:02 +00:00
{{ if $ IsIPV6Enabled }}
2017-08-25 02:24:32 +00:00
{{ range $ address := $ all . Cfg . BindAddressIpv6 }}
listen {{ $ address }}:{{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }};
listen {{ $ address }}:{{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol{{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }};
{{ else }}
2017-08-24 13:33:26 +00:00
listen [::]:{{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }};
listen [::]:{{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol{{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }};
2017-08-21 01:34:31 +00:00
{{ end }}
2017-08-25 02:24:32 +00:00
{{ end }}
2017-08-19 21:13:02 +00:00
server_name {{ $ hostname }};
2017-11-29 20:16:45 +00:00
{{ if ne $ all . ListenPorts . HTTPS 443 }}
{{ $ redirect_port := (printf ":%v" $ all . ListenPorts . HTTPS ) }}
2017-11-30 14:59:39 +00:00
return {{ $ all . Cfg . HTTPRedirectCode }} $ scheme ://{{ $ to }}{{ $ redirect_port }} $ request_uri ;
2017-11-29 20:16:45 +00:00
{{ else }}
2017-11-30 14:59:39 +00:00
return {{ $ all . Cfg . HTTPRedirectCode }} $ scheme ://{{ $ to }} $ request_uri ;
2017-11-29 20:16:45 +00:00
{{ end }}
2017-08-19 21:13:02 +00:00
}
{{ end }}
2017-08-17 17:05:01 +00:00
{{ range $ index , $ server := $ servers }}
2017-09-18 23:53:26 +00:00
2017-11-12 16:52:55 +00:00
## start server {{ $server.Hostname }}
2016-03-15 02:29:13 +00:00
server {
2017-10-11 13:24:33 +00:00
server_name {{ $ server . Hostname }} {{ $ server . Alias }};
2017-08-15 06:23:19 +00:00
{{ template "SERVER" serverConfig $ all $ server }}
2017-08-25 23:49:44 +00:00
2017-10-01 14:45:33 +00:00
{{ if not (empty $ cfg . ServerSnippet ) }}
# Custom code snippet configured in the configuration configmap
2017-10-05 01:51:02 +00:00
{{ $ cfg . ServerSnippet }}
2017-10-01 14:45:33 +00:00
{{ end }}
2017-08-25 23:49:44 +00:00
2017-08-03 14:51:39 +00:00
{{ template "CUSTOM_ERRORS" $ all }}
2016-03-15 15:31:39 +00:00
}
2017-11-12 16:52:55 +00:00
## end server {{ $server.Hostname }}
2017-08-15 06:23:19 +00:00
{{ end }}
2017-03-03 01:44:45 +00:00
2016-07-21 15:40:47 +00:00
# default server, used for NGINX healthcheck and access to nginx stats
2016-02-22 00:13:08 +00:00
server {
2017-08-24 13:33:26 +00:00
# Use the port {{ $ all . ListenPorts . Status }} (random value just to avoid known ports) as default port for nginx.
2016-07-21 15:40:47 +00:00
# Changing this value requires a change in:
2017-10-13 13:55:03 +00:00
# https://github.com/kubernetes/ingress-nginx/blob/master/controllers/nginx/pkg/cmd/controller/nginx.go
2017-09-12 13:27:51 +00:00
listen {{ $ all . ListenPorts . Status }} default_server reuseport backlog={{ $ all . BacklogSize }};
2017-08-31 17:28:20 +00:00
{{ if $ IsIPV6Enabled }}listen [::]:{{ $ all . ListenPorts . Status }} default_server reuseport backlog={{ $ all . BacklogSize }};{{ end }}
2017-05-14 00:59:30 +00:00
set $ proxy_upstream_name "-";
2016-02-22 00:13:08 +00:00
2016-11-16 18:24:26 +00:00
location {{ $ healthzURI }} {
2016-02-22 00:13:08 +00:00
access_log off;
return 200;
}
2017-03-03 01:44:45 +00:00
2016-03-26 21:25:51 +00:00
location /nginx_status {
2017-03-12 15:27:05 +00:00
set $ proxy_upstream_name "internal";
2016-11-16 18:24:26 +00:00
{{ if $ cfg . EnableVtsStatus }}
2016-03-26 21:25:51 +00:00
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
{{ else }}
2016-03-22 18:01:04 +00:00
access_log off;
2016-02-22 00:13:08 +00:00
stub_status on;
2016-11-10 22:56:29 +00:00
{{ end }}
2016-02-22 00:13:08 +00:00
}
2016-03-16 14:12:45 +00:00
location / {
2016-11-16 18:24:26 +00:00
{{ if .CustomErrors }}
2017-09-08 23:18:11 +00:00
proxy_set_header X-Code 404;
2016-05-23 23:15:13 +00:00
{{ end }}
2017-09-08 23:18:11 +00:00
set $ proxy_upstream_name "upstream-default-backend";
proxy_pass http://upstream-default-backend;
2016-09-29 15:02:45 +00:00
}
2017-08-25 23:49:44 +00:00
{{ template "CUSTOM_ERRORS" $ all }}
2016-09-29 19:01:09 +00:00
}
2016-02-22 00:13:08 +00:00
}
stream {
2017-02-27 10:00:31 +00:00
log_format log_stream {{ $ cfg . LogFormatStream }};
2016-11-10 22:56:29 +00:00
2017-02-09 23:20:12 +00:00
{{ if $ cfg . DisableAccessLog }}
access_log off;
{{ else }}
2017-08-23 14:57:28 +00:00
access_log {{ $ cfg . AccessLogPath }} log_stream;
2017-02-09 23:20:12 +00:00
{{ end }}
2017-08-23 14:57:28 +00:00
error_log {{ $ cfg . ErrorLogPath }};
2016-11-10 22:56:29 +00:00
2017-03-03 01:44:45 +00:00
# TCP services
2017-01-09 00:31:16 +00:00
{{ range $i, $ tcpServer := .TCPBackends }}
2017-07-02 20:46:15 +00:00
upstream tcp-{{ $ tcpServer . Port }}-{{ $ tcpServer . Backend . Namespace }}-{{ $ tcpServer . Backend . Name }}-{{ $ tcpServer . Backend . Port }} {
2017-02-24 21:46:39 +00:00
{{ range $j, $ endpoint := $ tcpServer . Endpoints }}
server {{ $ endpoint . Address }}:{{ $ endpoint . Port }};
{{ end }}
}
server {
2017-08-25 02:24:32 +00:00
{{ range $ address := $ all . Cfg . BindAddressIpv4 }}
2017-09-27 14:10:16 +00:00
listen {{ $ address }}:{{ $ tcpServer . Port }}{{ if $ tcpServer . Backend . ProxyProtocol . Decode }} proxy_protocol{{ end }};
2017-08-25 02:24:32 +00:00
{{ else }}
2017-09-27 14:10:16 +00:00
listen {{ $ tcpServer . Port }}{{ if $ tcpServer . Backend . ProxyProtocol . Decode }} proxy_protocol{{ end }};
2017-08-25 02:24:32 +00:00
{{ end }}
{{ if $ IsIPV6Enabled }}
{{ range $ address := $ all . Cfg . BindAddressIpv6 }}
2017-09-27 14:10:16 +00:00
listen {{ $ address }}:{{ $ tcpServer . Port }}{{ if $ tcpServer . Backend . ProxyProtocol . Decode }} proxy_protocol{{ end }};
2017-08-25 02:24:32 +00:00
{{ else }}
2017-09-27 14:10:16 +00:00
listen [::]:{{ $ tcpServer . Port }}{{ if $ tcpServer . Backend . ProxyProtocol . Decode }} proxy_protocol{{ end }};
2017-08-25 02:24:32 +00:00
{{ end }}
{{ end }}
2017-08-21 01:34:31 +00:00
proxy_timeout {{ $ cfg . ProxyStreamTimeout }};
2017-07-02 20:46:15 +00:00
proxy_pass tcp-{{ $ tcpServer . Port }}-{{ $ tcpServer . Backend . Namespace }}-{{ $ tcpServer . Backend . Name }}-{{ $ tcpServer . Backend . Port }};
2017-09-27 14:10:16 +00:00
{{ if $ tcpServer . Backend . ProxyProtocol . Encode }}
proxy_protocol on;
{{ end }}
2017-02-24 21:46:39 +00:00
}
2017-07-02 20:46:15 +00:00
2017-01-09 00:31:16 +00:00
{{ end }}
2016-11-10 22:56:29 +00:00
2017-02-24 21:46:39 +00:00
# UDP services
{{ range $i, $ udpServer := .UDPBackends }}
2017-07-02 20:46:15 +00:00
upstream udp-{{ $ udpServer . Port }}-{{ $ udpServer . Backend . Namespace }}-{{ $ udpServer . Backend . Name }}-{{ $ udpServer . Backend . Port }} {
2017-02-24 21:46:39 +00:00
{{ range $j, $ endpoint := $ udpServer . Endpoints }}
server {{ $ endpoint . Address }}:{{ $ endpoint . Port }};
{{ end }}
}
2017-03-03 01:44:45 +00:00
2017-02-24 21:46:39 +00:00
server {
2017-08-25 02:24:32 +00:00
{{ range $ address := $ all . Cfg . BindAddressIpv4 }}
listen {{ $ address }}:{{ $ udpServer . Port }} udp;
{{ else }}
2017-03-15 17:45:21 +00:00
listen {{ $ udpServer . Port }} udp;
2017-08-25 02:24:32 +00:00
{{ end }}
{{ if $ IsIPV6Enabled }}
{{ range $ address := $ all . Cfg . BindAddressIpv6 }}
listen {{ $ address }}:{{ $ udpServer . Port }} udp;
{{ else }}
listen [::]:{{ $ udpServer . Port }} udp;
{{ end }}
{{ end }}
2017-11-30 20:53:23 +00:00
proxy_responses {{ $ cfg . ProxyStreamResponses }};
2017-08-21 01:34:31 +00:00
proxy_timeout {{ $ cfg . ProxyStreamTimeout }};
2017-07-02 20:46:15 +00:00
proxy_pass udp-{{ $ udpServer . Port }}-{{ $ udpServer . Backend . Namespace }}-{{ $ udpServer . Backend . Name }}-{{ $ udpServer . Backend . Port }};
2017-02-24 21:46:39 +00:00
}
2017-08-25 23:49:44 +00:00
2017-01-09 00:31:16 +00:00
{{ end }}
2016-02-22 00:13:08 +00:00
}
{{/* definition of templates to avoid repetitions */}}
{{ define "CUSTOM_ERRORS" }}
2017-09-08 23:18:11 +00:00
{{ $ proxySetHeaders := .ProxySetHeaders }}
2017-08-03 14:51:39 +00:00
{{ range $ errCode := .Cfg.CustomHTTPErrors }}
2016-05-23 23:15:13 +00:00
location @custom_{{ $ errCode }} {
2016-03-22 18:01:04 +00:00
internal;
2017-09-07 23:39:58 +00:00
2017-09-28 13:07:02 +00:00
proxy_intercept_errors off;
2017-09-08 23:18:11 +00:00
proxy_set_header X-Code {{ $ errCode }};
proxy_set_header X-Format $ http_accept ;
proxy_set_header X-Original-URI $ request_uri ;
proxy_set_header X-Namespace $ namespace ;
proxy_set_header X-Ingress-Name $ ingress_name ;
proxy_set_header X-Service-Name $ service_name ;
2017-09-07 23:39:58 +00:00
2017-09-28 13:07:02 +00:00
rewrite (.*) / break;
2017-09-08 23:18:11 +00:00
proxy_pass http://upstream-default-backend;
2017-03-03 01:44:45 +00:00
}
2016-05-23 23:15:13 +00:00
{{ end }}
2016-02-22 00:13:08 +00:00
{{ end }}
2016-09-22 18:00:09 +00:00
{{/* CORS support from https://michielkalkman.com/snippets/nginx-cors-open-configuration.html */}}
{{ define "CORS" }}
2017-10-19 20:03:02 +00:00
{{ $ cors := .CorsConfig }}
# Cors Preflight methods needs additional options and different Return Code
2016-09-22 18:00:09 +00:00
if ( $ request_method = 'OPTIONS') {
2017-11-12 04:58:52 +00:00
add_header 'Access-Control-Allow-Origin' '{{ $ cors . CorsAllowOrigin }}' always;
{{ if $ cors . CorsAllowCredentials }} add_header 'Access-Control-Allow-Credentials' '{{ $ cors . CorsAllowCredentials }}' always; {{ end }}
add_header 'Access-Control-Allow-Methods' '{{ $ cors . CorsAllowMethods }}' always;
add_header 'Access-Control-Allow-Headers' '{{ $ cors . CorsAllowHeaders }}' always;
2018-01-09 11:19:42 +00:00
add_header 'Access-Control-Max-Age' {{ $ cors . CorsMaxAge }};
2016-09-22 18:00:09 +00:00
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
2017-03-23 15:07:09 +00:00
2017-11-12 04:58:52 +00:00
add_header 'Access-Control-Allow-Origin' '{{ $ cors . CorsAllowOrigin }}' always;
{{ if $ cors . CorsAllowCredentials }} add_header 'Access-Control-Allow-Credentials' '{{ $ cors . CorsAllowCredentials }}' always; {{ end }}
add_header 'Access-Control-Allow-Methods' '{{ $ cors . CorsAllowMethods }}' always;
add_header 'Access-Control-Allow-Headers' '{{ $ cors . CorsAllowHeaders }}' always;
2017-10-19 20:03:02 +00:00
2016-09-22 18:00:09 +00:00
{{ end }}
2017-08-15 06:23:19 +00:00
{{/* definition of server-template to avoid repetitions with server-alias */}}
{{ define "SERVER" }}
{{ $ all := .First }}
{{ $ server := .Second }}
2017-08-25 02:24:32 +00:00
{{ range $ address := $ all . Cfg . BindAddressIpv4 }}
listen {{ $ address }}:{{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{end}};
{{ else }}
2017-08-24 13:33:26 +00:00
listen {{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{end}};
2017-08-25 02:24:32 +00:00
{{ end }}
{{ if $ all . IsIPV6Enabled }}
{{ range $ address := $ all . Cfg . BindAddressIpv6 }}
listen {{ $ address }}:{{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{ end }};
{{ else }}
listen [::]:{{ $ all . ListenPorts . HTTP }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{ end }};
{{ end }}
{{ end }}
2017-08-15 06:23:19 +00:00
set $ proxy_upstream_name "-";
2017-08-24 13:33:26 +00:00
{{/* Listen on {{ $ all . ListenPorts . SSLProxy }} because port {{ $ all . ListenPorts . HTTPS }} is used in the TLS sni server */}}
2017-08-15 06:23:19 +00:00
{{/* This listener must always have proxy_protocol enabled, because the SNI listener forwards on source IP info in it. */}}
2017-08-25 02:24:32 +00:00
{{ if not (empty $ server . SSLCertificate ) }}
{{ range $ address := $ all . Cfg . BindAddressIpv4 }}
listen {{ $ address }}:{{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol {{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }} {{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{end}} ssl {{ if $ all . Cfg . UseHTTP2 }}http2{{ end }};
{{ else }}
listen {{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol {{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }} {{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{end}} ssl {{ if $ all . Cfg . UseHTTP2 }}http2{{ end }};
{{ end }}
{{ if $ all . IsIPV6Enabled }}
{{ range $ address := $ all . Cfg . BindAddressIpv6 }}
{{ if not (empty $ server . SSLCertificate ) }}listen {{ $ address }}:{{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol{{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }}{{ end }} {{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{end}} ssl {{ if $ all . Cfg . UseHTTP2 }}http2{{ end }};
{{ else }}
{{ if not (empty $ server . SSLCertificate ) }}listen [::]:{{ if $ all . IsSSLPassthroughEnabled }}{{ $ all . ListenPorts . SSLProxy }} proxy_protocol{{ else }}{{ $ all . ListenPorts . HTTPS }}{{ if $ all . Cfg . UseProxyProtocol }} proxy_protocol{{ end }}{{ end }}{{ end }} {{ if eq $ server . Hostname "_"}} default_server reuseport backlog={{ $ all . BacklogSize }}{{end}} ssl {{ if $ all . Cfg . UseHTTP2 }}http2{{ end }};
{{ end }}
{{ end }}
2017-08-24 13:33:26 +00:00
{{/* comment PEM sha is required to detect changes in the generated configuration and force a reload */}}
2017-08-15 06:23:19 +00:00
# PEM sha: {{ $ server . SSLPemChecksum }}
ssl_certificate {{ $ server . SSLCertificate }};
ssl_certificate_key {{ $ server . SSLCertificate }};
2017-10-04 20:11:03 +00:00
{{ if not (empty $ server . SSLFullChainCertificate )}}
2017-10-09 12:16:09 +00:00
ssl_trusted_certificate {{ $ server . SSLFullChainCertificate }};
2017-10-04 20:11:03 +00:00
ssl_stapling on;
ssl_stapling_verify on;
{{ end }}
2017-08-15 06:23:19 +00:00
{{ end }}
{{ if (and (not (empty $ server . SSLCertificate )) $ all . Cfg . HSTS ) }}
more_set_headers "Strict-Transport-Security: max-age={{ $ all . Cfg . HSTSMaxAge }}{{ if $ all . Cfg . HSTSIncludeSubdomains }}; includeSubDomains{{ end }};{{ if $ all . Cfg . HSTSPreload }} preload{{ end }}";
{{ end }}
2017-08-22 20:16:59 +00:00
{{ if not (empty $ server . CertificateAuth . CAFileName ) }}
# PEM sha: {{ $ server . CertificateAuth . PemSHA }}
ssl_client_certificate {{ $ server . CertificateAuth . CAFileName }};
2017-10-05 11:26:07 +00:00
ssl_verify_client {{ $ server . CertificateAuth . VerifyClient }};
2017-08-22 20:16:59 +00:00
ssl_verify_depth {{ $ server . CertificateAuth . ValidationDepth }};
2017-09-03 20:12:03 +00:00
{{ if not (empty $ server . CertificateAuth . ErrorPage )}}
error_page 495 496 = {{ $ server . CertificateAuth . ErrorPage }};
{{ end }}
2017-08-22 20:16:59 +00:00
{{ end }}
2017-09-20 09:35:16 +00:00
{{ if not (empty $ server . ServerSnippet ) }}
{{ $ server . ServerSnippet }}
{{ end }}
2017-10-09 12:16:09 +00:00
2017-08-15 06:23:19 +00:00
{{ range $ location := $ server . Locations }}
{{ $ path := buildLocation $ location }}
{{ $ authPath := buildAuthLocation $ location }}
2017-08-19 21:13:02 +00:00
{{ if not (empty $ location . Rewrite . AppRoot )}}
2017-08-15 06:23:19 +00:00
if ( $ uri = /) {
2017-08-31 06:58:01 +00:00
return 302 {{ $ location . Rewrite . AppRoot }};
2017-08-15 06:23:19 +00:00
}
{{ end }}
{{ if not (empty $ authPath ) }}
location = {{ $ authPath }} {
2017-08-31 06:58:01 +00:00
internal;
2017-10-15 20:20:33 +00:00
set $ proxy_upstream_name "external-authentication";
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
proxy_pass_request_body off;
proxy_set_header Content-Length "";
2017-10-15 20:20:33 +00:00
2017-08-31 06:58:01 +00:00
{{ if not (empty $ location . ExternalAuth . Method ) }}
proxy_method {{ $ location . ExternalAuth . Method }};
2017-10-04 13:59:14 +00:00
proxy_set_header X-Original-URI $ request_uri ;
proxy_set_header X-Scheme $ pass_access_scheme ;
2017-08-31 06:58:01 +00:00
{{ end }}
2017-10-15 20:20:33 +00:00
2017-10-04 13:59:14 +00:00
proxy_set_header Host {{ $ location . ExternalAuth . Host }};
proxy_set_header X-Original-URL $ scheme :// $ http_host $ request_uri ;
2017-11-08 11:14:04 +00:00
proxy_set_header X-Original-Method $ request_method ;
2017-10-04 13:59:14 +00:00
proxy_set_header X-Auth-Request-Redirect $ request_uri ;
2017-10-15 20:20:33 +00:00
proxy_set_header X-Sent-From "nginx-ingress-controller";
2017-08-15 06:23:19 +00:00
2017-12-02 15:05:13 +00:00
proxy_http_version 1.1;
2017-10-15 20:20:33 +00:00
proxy_ssl_server_name on;
proxy_pass_request_headers on;
2017-08-31 06:58:01 +00:00
client_max_body_size "{{ $ location . Proxy . BodySize }}";
{{ if isValidClientBodyBufferSize $ location . ClientBodyBufferSize }}
client_body_buffer_size {{ $ location . ClientBodyBufferSize }};
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
set $ target {{ $ location . ExternalAuth . URL }};
proxy_pass $ target ;
2017-08-15 06:23:19 +00:00
}
{{ end }}
2017-10-15 20:20:33 +00:00
2017-10-06 01:55:10 +00:00
location {{ $ path }} {
2018-01-02 17:48:42 +00:00
port_in_redirect {{ if $ location . UsePortInRedirects }}on{{ else }}off{{ end }};
2017-09-13 06:35:05 +00:00
{{ if $ all . Cfg . EnableVtsStatus }}{{ if $ location . VtsFilterKey }} vhost_traffic_status_filter_by_set_key {{ $ location . VtsFilterKey }};{{ end }}{{ end }}
2017-08-31 06:58:01 +00:00
set $ proxy_upstream_name "{{ buildUpstreamName $ server . Hostname $ all . Backends $ location }}";
{{ $ ing := (getIngressInformation $ location . Ingress $ path ) }}
{{/* $ ing . Metadata contains the Ingress metadata */}}
set $ namespace "{{ $ ing . Namespace }}";
set $ ingress_name "{{ $ ing . Rule }}";
set $ service_name "{{ $ ing . Service }}";
2018-01-02 17:48:42 +00:00
{{/* redirect to HTTPS can be achieved forcing the redirect or having a SSL Certificate configured for the server */}}
2017-08-31 06:58:01 +00:00
{{ if (or $ location . Rewrite . ForceSSLRedirect (and (not (empty $ server . SSLCertificate )) $ location . Rewrite . SSLRedirect )) }}
# enforce ssl on server side
2018-01-17 12:20:41 +00:00
if ( $ redirect_to_https ) {
2018-01-02 17:48:42 +00:00
{{ if $ location . UsePortInRedirects }}
2018-01-17 12:20:41 +00:00
# using custom ports require a different rewrite directive
2018-01-02 17:48:42 +00:00
{{ $ redirect_port := (printf ":%v" $ all . ListenPorts . HTTPS ) }}
2018-01-17 12:20:41 +00:00
error_page 497 ={{ $ all . Cfg . HTTPRedirectCode }} https:// $ best_http_host {{ $ redirect_port }} $ request_uri ;
return 497;
2017-11-29 20:16:45 +00:00
{{ else }}
2017-11-30 14:59:39 +00:00
return {{ $ all . Cfg . HTTPRedirectCode }} https:// $ best_http_host $ request_uri ;
2017-11-29 20:16:45 +00:00
{{ end }}
2017-08-31 06:58:01 +00:00
}
{{ end }}
2017-08-15 06:23:19 +00:00
2017-10-08 14:52:02 +00:00
{{ if $ all . Cfg . EnableModsecurity }}
modsecurity on;
modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf;
{{ if $ all . Cfg . EnableOWASPCoreRules }}
modsecurity_rules_file /etc/nginx/owasp-modsecurity-crs/nginx-modsecurity.conf;
{{ end }}
{{ end }}
2017-08-31 06:58:01 +00:00
{{ if isLocationAllowed $ location }}
{{ if gt (len $ location . Whitelist . CIDR ) 0 }}
if ({{ buildDenyVariable (print $ server . Hostname "_" $ path ) }}) {
return 403;
}
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{ if not (empty $ authPath ) }}
# this location requires authentication
2017-10-04 13:59:14 +00:00
auth_request {{ $ authPath }};
auth_request_set $ auth_cookie $ upstream_http_set_cookie ;
add_header Set-Cookie $ auth_cookie ;
2017-08-31 06:58:01 +00:00
{{- range $ idx , $ line := buildAuthResponseHeaders $ location }}
{{ $ line }}
{{- end }}
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{ if not (empty $ location . ExternalAuth . SigninURL ) }}
2017-10-05 04:55:42 +00:00
error_page 401 = {{ buildAuthSignURL $ location . ExternalAuth . SigninURL }};
2017-08-31 06:58:01 +00:00
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{/* if the location contains a rate limit annotation, create one */}}
{{ $ limits := buildRateLimit $ location }}
{{ range $ limit := $ limits }}
{{ $ limit }}{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{ if $ location . BasicDigestAuth . Secured }}
{{ if eq $ location . BasicDigestAuth . Type "basic" }}
auth_basic "{{ $ location . BasicDigestAuth . Realm }}";
auth_basic_user_file {{ $ location . BasicDigestAuth . File }};
{{ else }}
auth_digest "{{ $ location . BasicDigestAuth . Realm }}";
auth_digest_user_file {{ $ location . BasicDigestAuth . File }};
{{ end }}
proxy_set_header Authorization "";
{{ end }}
2017-08-15 06:23:19 +00:00
2017-10-19 20:03:02 +00:00
{{ if $ location . CorsConfig . CorsEnabled }}
{{ template "CORS" $ location }}
2017-08-31 06:58:01 +00:00
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{ if not (empty $ location . Redirect . URL ) }}
if ( $ uri ~* {{ $ path }}) {
return {{ $ location . Redirect . Code }} {{ $ location . Redirect . URL }};
}
{{ end }}
2017-08-19 21:13:02 +00:00
2017-08-31 06:58:01 +00:00
client_max_body_size "{{ $ location . Proxy . BodySize }}";
{{ if isValidClientBodyBufferSize $ location . ClientBodyBufferSize }}
client_body_buffer_size {{ $ location . ClientBodyBufferSize }};
{{ end }}
2017-08-15 06:23:19 +00:00
2017-05-15 19:17:58 +00:00
{{/* By default use vhost as Host to upstream, but allow overrides */}}
{{ if not (empty $ location . UpstreamVhost ) }}
proxy_set_header Host "{{ $ location . UpstreamVhost }}";
{{ else }}
2017-08-31 06:58:01 +00:00
proxy_set_header Host $ best_http_host ;
2017-05-15 19:17:58 +00:00
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
# Pass the extracted client certificate to the backend
{{ if not (empty $ server . CertificateAuth . CAFileName ) }}
2017-11-18 00:28:45 +00:00
{{ if $ server . CertificateAuth . PassCertToUpstream }}
2017-11-20 17:15:31 +00:00
proxy_set_header ssl-client-cert $ ssl_client_escaped_cert ;
2017-11-18 00:28:45 +00:00
{{ else }}
proxy_set_header ssl-client-cert "";
{{ end }}
2017-10-09 11:13:50 +00:00
proxy_set_header ssl-client-verify $ ssl_client_verify ;
2017-10-31 15:50:06 +00:00
proxy_set_header ssl-client-dn $ ssl_client_s_dn ;
2017-10-22 22:52:54 +00:00
{{ else }}
proxy_set_header ssl-client-cert "";
proxy_set_header ssl-client-verify "";
2017-10-31 15:50:06 +00:00
proxy_set_header ssl-client-dn "";
2017-08-31 06:58:01 +00:00
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
# Allow websocket connections
proxy_set_header Upgrade $ http_upgrade ;
proxy_set_header Connection $ connection_upgrade ;
proxy_set_header X-Real-IP $ the_real_ip ;
2017-10-09 09:10:58 +00:00
{{ if $ all . Cfg . ComputeFullForwardedFor }}
proxy_set_header X-Forwarded-For $ full_x_forwarded_for ;
{{ else }}
proxy_set_header X-Forwarded-For $ the_real_ip ;
{{ end }}
2017-08-31 06:58:01 +00:00
proxy_set_header X-Forwarded-Host $ best_http_host ;
proxy_set_header X-Forwarded-Port $ pass_port ;
proxy_set_header X-Forwarded-Proto $ pass_access_scheme ;
proxy_set_header X-Original-URI $ request_uri ;
proxy_set_header X-Scheme $ pass_access_scheme ;
2017-10-06 01:55:10 +00:00
# Pass the original X-Forwarded-For
proxy_set_header X-Original-Forwarded-For {{ buildForwardedFor $ all . Cfg . ForwardedForHeader }};
2017-08-31 06:58:01 +00:00
# mitigate HTTPoxy Vulnerability
# https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
proxy_set_header Proxy "";
# Custom headers to proxied server
{{ range $k, $v := $ all . ProxySetHeaders }}
proxy_set_header {{ $k }} "{{ $v }}";
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
proxy_connect_timeout {{ $ location . Proxy . ConnectTimeout }}s;
proxy_send_timeout {{ $ location . Proxy . SendTimeout }}s;
proxy_read_timeout {{ $ location . Proxy . ReadTimeout }}s;
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
proxy_buffering off;
proxy_buffer_size "{{ $ location . Proxy . BufferSize }}";
proxy_buffers 4 "{{ $ location . Proxy . BufferSize }}";
2017-09-01 15:43:11 +00:00
proxy_request_buffering "{{ $ location . Proxy . RequestBuffering }}";
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
proxy_http_version 1.1;
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
proxy_cookie_domain {{ $ location . Proxy . CookieDomain }};
proxy_cookie_path {{ $ location . Proxy . CookiePath }};
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
# In case of errors try the next upstream server before returning an error
2017-10-10 10:18:45 +00:00
proxy_next_upstream {{ buildNextUpstream $ location . Proxy . NextUpstream $ all . Cfg . RetryNonIdempotent }};
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{/* rewrite only works if the content is not compressed */}}
{{ if $ location . Rewrite . AddBaseURL }}
proxy_set_header Accept-Encoding "";
{{ end }}
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
{{/* Add any additional configuration defined */}}
{{ $ location . ConfigurationSnippet }}
2017-08-15 06:23:19 +00:00
2017-10-01 14:45:33 +00:00
{{ if not (empty $ all . Cfg . LocationSnippet ) }}
# Custom code snippet configured in the configuration configmap
2017-10-05 01:51:02 +00:00
{{ $ all . Cfg . LocationSnippet }}
2017-10-01 14:45:33 +00:00
{{ end }}
2017-08-31 06:58:01 +00:00
{{/* if we are sending the request to a custom default backend, we add the required headers */}}
{{ if (hasPrefix $ location . Backend "custom-default-backend-") }}
proxy_set_header X-Code 503;
proxy_set_header X-Format $ http_accept ;
proxy_set_header X-Namespace $ namespace ;
proxy_set_header X-Ingress-Name $ ingress_name ;
proxy_set_header X-Service-Name $ service_name ;
{{ end }}
2017-08-25 23:49:44 +00:00
2017-10-09 12:16:09 +00:00
{{ if not (empty $ location . Backend ) }}
2017-08-31 06:58:01 +00:00
{{ buildProxyPass $ server . Hostname $ all . Backends $ location }}
2018-01-02 11:34:20 +00:00
{{ if (or (eq $ location . Proxy . ProxyRedirectFrom "default") (eq $ location . Proxy . ProxyRedirectFrom "off")) }}
proxy_redirect {{ $ location . Proxy . ProxyRedirectFrom }};
{{ else }}
proxy_redirect {{ $ location . Proxy . ProxyRedirectFrom }} {{ $ location . Proxy . ProxyRedirectTo }};
{{ end }}
2017-08-31 06:58:01 +00:00
{{ else }}
2017-10-09 12:16:09 +00:00
# No endpoints available for the request
return 503;
{{ end }}
{{ else }}
2017-08-31 06:58:01 +00:00
# Location denied. Reason: {{ $ location . Denied }}
return 503;
{{ end }}
2017-08-15 06:23:19 +00:00
}
2017-10-12 18:30:05 +00:00
2017-08-15 06:23:19 +00:00
{{ end }}
{{ if eq $ server . Hostname "_" }}
2017-08-24 13:33:26 +00:00
# health checks in cloud providers require the use of port {{ $ all . ListenPorts . HTTP }}
2017-08-15 06:23:19 +00:00
location {{ $ all . HealthzURI }} {
2017-08-31 06:58:01 +00:00
access_log off;
return 200;
2017-08-15 06:23:19 +00:00
}
# this is required to avoid error if nginx is being monitored
# with an external software (like sysdig)
location /nginx_status {
2017-08-31 06:58:01 +00:00
allow 127.0.0.1;
{{ if $ all . IsIPV6Enabled }}allow ::1;{{ end }}
deny all;
2017-08-15 06:23:19 +00:00
2017-08-31 06:58:01 +00:00
access_log off;
stub_status on;
2017-08-15 06:23:19 +00:00
}
{{ end }}
{{ end }}