ingress-nginx-helm/controllers/nginx/nginx.tmpl

321 lines
10 KiB
Cheetah
Raw Normal View History

{{ $cfg := .cfg }}
2016-02-22 00:13:08 +00:00
daemon off;
2016-03-19 23:29:29 +00:00
worker_processes {{ $cfg.workerProcesses }};
2016-02-22 00:13:08 +00:00
pid /run/nginx.pid;
worker_rlimit_nofile 131072;
pcre_jit on;
2016-02-22 00:13:08 +00:00
events {
multi_accept on;
worker_connections {{ $cfg.maxWorkerConnections }};
use epoll;
2016-02-22 00:13:08 +00:00
}
http {
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;
{{- if $cfg.enableVtsStatus }}
vhost_traffic_status_zone shared:vhost_traffic_status:{{ $cfg.vtsStatusZoneSize }};
vhost_traffic_status_filter_by_set_key $geoip_country_code country::*;
{{ end -}}
2016-02-22 00:13:08 +00:00
# lus sectrion to return proper error codes when custom pages are used
lua_package_path '.?.lua;./etc/nginx/lua/?.lua;/etc/nginx/lua/vendor/lua-resty-http/lib/?.lua;';
init_by_lua_block {
2016-02-22 00:13:08 +00:00
require("error_page")
}
sendfile on;
aio threads;
tcp_nopush on;
tcp_nodelay on;
log_subrequest on;
reset_timedout_connection on;
2016-02-22 00:13:08 +00:00
2016-03-19 23:29:29 +00:00
keepalive_timeout {{ $cfg.keepAlive }}s;
2016-02-22 00:13:08 +00:00
types_hash_max_size 2048;
2016-03-19 23:29:29 +00:00
server_names_hash_max_size {{ $cfg.serverNameHashMaxSize }};
server_names_hash_bucket_size {{ $cfg.serverNameHashBucketSize }};
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;
2016-05-16 20:29:33 +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;
2016-03-19 23:29:29 +00:00
gzip_types {{ $cfg.gzipTypes }};
2016-02-22 00:13:08 +00:00
gzip_proxied any;
2016-05-16 20:29:33 +00:00
{{- end }}
2016-02-22 00:13:08 +00:00
2016-03-19 23:29:29 +00:00
client_max_body_size "{{ $cfg.bodySize }}";
2016-02-22 00:13:08 +00:00
2016-05-16 20:29:33 +00:00
{{ if $cfg.useProxyProtocol -}}
set_real_ip_from {{ $cfg.proxyRealIpCidr }};
real_ip_header proxy_protocol;
2016-05-16 20:29:33 +00:00
{{ end -}}
2016-02-22 00:13:08 +00:00
2016-03-19 23:29:29 +00:00
log_format upstreaminfo '{{ if $cfg.useProxyProtocol }}$proxy_protocol_addr{{ else }}$remote_addr{{ end }} - '
'[$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
2016-02-22 00:13:08 +00:00
'$request_length $request_time $upstream_addr $upstream_response_length $upstream_response_time $upstream_status';
access_log /var/log/nginx/access.log upstreaminfo;
2016-03-19 23:29:29 +00:00
error_log /var/log/nginx/error.log {{ $cfg.errorLogLevel }};
2016-02-22 00:13:08 +00:00
{{ if not (empty .defResolver) }}# Custom dns resolver.
resolver {{ .defResolver }} valid=30s;
{{ end }}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# trust http_x_forwarded_proto headers correctly indicate ssl offloading
map $http_x_forwarded_proto $pass_access_scheme {
2016-02-22 00:13:08 +00:00
default $http_x_forwarded_proto;
'' $scheme;
}
# Map a response error watching the header Content-Type
map $http_accept $httpAccept {
default html;
application/json json;
application/xml xml;
text/plain text;
}
map $httpAccept $httpReturnType {
default text/html;
json application/json;
xml application/xml;
text text/plain;
}
server_name_in_redirect off;
port_in_redirect off;
2016-03-19 23:29:29 +00:00
ssl_protocols {{ $cfg.sslProtocols }};
2016-02-22 00:13:08 +00:00
# turn on session caching to drastically improve performance
2016-03-19 23:29:29 +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-03-19 23:29:29 +00:00
ssl_session_tickets {{ if $cfg.sslSessionTickets }}on{{ else }}off{{ end }};
2016-02-22 00:13:08 +00:00
# slightly reduce the time-to-first-byte
2016-03-19 23:29:29 +00:00
ssl_buffer_size {{ $cfg.sslBufferSize }};
2016-02-22 00:13:08 +00:00
2016-03-19 23:29:29 +00:00
{{ if not (empty $cfg.sslCiphers) }}
2016-02-22 00:13:08 +00:00
# allow configuring custom ssl ciphers
2016-03-19 23:29:29 +00:00
ssl_ciphers '{{ $cfg.sslCiphers }}';
2016-02-22 00:13:08 +00:00
ssl_prefer_server_ciphers on;
{{ end }}
{{ if not (empty .sslDHParam) }}
# allow custom DH file http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_dhparam
ssl_dhparam {{ .sslDHParam }};
{{ end }}
2016-05-23 23:15:13 +00:00
{{- if .customErrors }}
# Custom error pages
2016-02-22 00:13:08 +00:00
proxy_intercept_errors on;
2016-05-23 23:15:13 +00:00
{{ end -}}
2016-02-22 00:13:08 +00:00
2016-05-23 23:15:13 +00:00
{{- range $errCode := $cfg.customHttpErrors }}
error_page {{ $errCode }} = @custom_{{ $errCode }};
{{ end }}
2016-02-22 00:13:08 +00:00
# In case of errors try the next upstream server before returning an error
proxy_next_upstream error timeout invalid_header http_502 http_503 http_504 {{ if $cfg.retryNonIdempotent }}non_idempotent{{ end }};
2016-02-22 00:13:08 +00:00
{{range $name, $upstream := .upstreams}}
upstream {{$upstream.Name}} {
2016-05-16 20:29:33 +00:00
{{ if $cfg.enableStickySessions -}}
2016-04-28 04:03:59 +00:00
sticky hash=sha1 httponly;
2016-05-16 20:29:33 +00:00
{{ else -}}
least_conn;
2016-05-16 20:29:33 +00:00
{{- end }}
{{ range $server := $upstream.Backends }}server {{ $server.Address }}:{{ $server.Port }} max_fails={{ $server.MaxFails }} fail_timeout={{ $server.FailTimeout }};
2016-04-28 04:03:59 +00:00
{{ end }}
}
2016-05-16 20:29:33 +00:00
{{ end }}
{{/* 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 */}}
{{ $zone := range (buildRateLimitZones .servers) }}
{{ $zone }}
{{ end }}
{{ range $server := .servers }}
server {
2016-05-23 23:15:13 +00:00
server_name {{ $server.Name }};
2016-04-16 22:36:45 +00:00
listen 80{{ if $cfg.useProxyProtocol }} proxy_protocol{{ end }};
2016-05-12 10:48:37 +00:00
{{ if $server.SSL }}listen 443 {{ if $cfg.useProxyProtocol }}proxy_protocol{{ end }} ssl {{ if $cfg.useHttp2 }}http2{{ end }};
{{/* comment PEM sha is required to detect changes in the generated configuration and force a reload */}}
# PEM sha: {{ $server.SSLPemChecksum }}
ssl_certificate {{ $server.SSLCertificate }};
2016-05-16 20:29:33 +00:00
ssl_certificate_key {{ $server.SSLCertificateKey }};
{{ end }}
2016-05-16 20:29:33 +00:00
{{ if (and $server.SSL $cfg.hsts) -}}
if ($scheme = http) {
return 301 https://$host$request_uri;
}
2016-04-15 12:35:39 +00:00
2016-04-16 22:36:45 +00:00
more_set_headers "Strict-Transport-Security: max-age={{ $cfg.hstsMaxAge }}{{ if $cfg.hstsIncludeSubdomains }}; includeSubDomains{{ end }}; preload";
2016-05-16 20:29:33 +00:00
{{ end -}}
2016-04-02 20:41:41 +00:00
2016-05-30 18:44:02 +00:00
{{ if $cfg.enableVtsStatus }}vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;{{ end }}
{{- range $location := $server.Locations }}
{{- $path := buildLocation $location }}
location {{ $path }} {
location {{ $path }} {
proxy_set_header Host $host;
# Pass Real IP
proxy_set_header X-Real-IP $remote_addr;
# Allow websocket connections
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
2016-04-16 22:36:45 +00:00
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $pass_access_scheme;
proxy_connect_timeout {{ $cfg.proxyConnectTimeout }}s;
proxy_send_timeout {{ $cfg.proxySendTimeout }}s;
proxy_read_timeout {{ $cfg.proxyReadTimeout }}s;
proxy_redirect off;
proxy_buffering off;
proxy_http_version 1.1;
{{/* rewrite only works if the content is not compressed */}}
{{ if $location.Redirect.AddBaseURL -}}
proxy_set_header Accept-Encoding "";
{{- end }}
{{- buildProxyPass $location }}
}
{{ end }}
{{ if eq $server.Name "_" }}
# this is required to avoid error if nginx is being monitored
# with an external software (like sysdig)
location /nginx_status {
allow 127.0.0.1;
deny all;
access_log off;
stub_status on;
}
{{ end }}
{{ template "CUSTOM_ERRORS" $cfg }}
}
{{ end }}
2016-02-22 00:13:08 +00:00
# default server, including healthcheck
server {
2016-04-16 22:36:45 +00:00
listen 8080 default_server reuseport;
2016-02-22 00:13:08 +00:00
location /healthz {
access_log off;
return 200;
}
location /nginx_status {
2016-05-16 20:29:33 +00:00
{{ if $cfg.enableVtsStatus -}}
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
{{ else }}
access_log off;
2016-02-22 00:13:08 +00:00
stub_status on;
2016-05-16 20:29:33 +00:00
{{- end }}
2016-02-22 00:13:08 +00:00
}
location / {
proxy_pass http://upstream-default-backend;
2016-02-22 00:13:08 +00:00
}
2016-05-23 23:15:13 +00:00
{{- template "CUSTOM_ERRORS" $cfg }}
2016-02-22 00:13:08 +00:00
}
# default server for services without endpoints
server {
2016-03-19 23:29:29 +00:00
listen 8181;
location / {
2016-05-23 23:15:13 +00:00
{{ if .customErrors }}
content_by_lua_block {
openURL(503)
}
2016-05-23 23:15:13 +00:00
{{ else }}
return 503;
{{ end }}
}
}
2016-02-22 00:13:08 +00:00
}
stream {
# TCP services
2016-03-19 23:29:29 +00:00
{{ range $i, $tcpServer := .tcpUpstreams }}
upstream tcp-{{ $tcpServer.Upstream.Name }} {
{{ range $server := $tcpServer.Upstream.Backends }}server {{ $server.Address }}:{{ $server.Port }};
{{ end }}
}
2016-02-22 00:13:08 +00:00
server {
2016-03-19 23:29:29 +00:00
listen {{ $tcpServer.Path }};
proxy_connect_timeout {{ $cfg.proxyConnectTimeout }};
proxy_timeout {{ $cfg.proxyReadTimeout }};
2016-03-19 23:29:29 +00:00
proxy_pass tcp-{{ $tcpServer.Upstream.Name }};
2016-02-22 00:13:08 +00:00
}
{{ end }}
# UDP services
{{ range $i, $udpServer := .udpUpstreams }}
upstream udp-{{ $udpServer.Upstream.Name }} {
{{ range $server := $udpServer.Upstream.Backends }}server {{ $server.Address }}:{{ $server.Port }};
{{ end }}
}
server {
listen {{ $udpServer.Path }} udp;
proxy_timeout 10s;
proxy_responses 1;
proxy_pass udp-{{ $udpServer.Upstream.Name }};
}
{{ end }}
2016-02-22 00:13:08 +00:00
}
{{/* definition of templates to avoid repetitions */}}
{{ define "CUSTOM_ERRORS" }}
2016-05-23 23:15:13 +00:00
{{ range $errCode := .customHttpErrors }}
location @custom_{{ $errCode }} {
internal;
2016-02-22 00:13:08 +00:00
content_by_lua_block {
2016-05-23 23:15:13 +00:00
openURL({{ $errCode }})
2016-02-22 00:13:08 +00:00
}
2016-05-23 23:15:13 +00:00
}
{{ end }}
2016-02-22 00:13:08 +00:00
{{ end }}