2016-03-19 20:17:58 +00:00
|
|
|
{{ $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;
|
|
|
|
|
2016-03-22 18:01:04 +00:00
|
|
|
pcre_jit on;
|
|
|
|
|
2016-02-22 00:13:08 +00:00
|
|
|
events {
|
2016-03-22 18:01:04 +00:00
|
|
|
multi_accept on;
|
|
|
|
worker_connections {{ $cfg.maxWorkerConnections }};
|
|
|
|
use epoll;
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http {
|
2016-03-26 21:25:51 +00:00
|
|
|
{{ if $cfg.enableVtsStatus}}vhost_traffic_status_zone shared:vhost_traffic_status:{{ $cfg.vtsStatusZoneSize }};{{ end }}
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2016-03-19 20:17:58 +00:00
|
|
|
# lus sectrion to return proper error codes when custom pages are used
|
2016-03-15 02:29:13 +00:00
|
|
|
lua_package_path '.?.lua;./etc/nginx/lua/?.lua;/etc/nginx/lua/vendor/lua-resty-http/lib/?.lua;';
|
2016-03-19 20:17:58 +00:00
|
|
|
init_by_lua_block {
|
2016-02-22 00:13:08 +00:00
|
|
|
require("error_page")
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:01:04 +00:00
|
|
|
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;
|
|
|
|
default_type application/octet-stream;
|
2016-03-19 23:29:29 +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;
|
|
|
|
gzip_vary on;
|
|
|
|
{{ end }}
|
|
|
|
|
2016-03-19 23:29:29 +00:00
|
|
|
client_max_body_size "{{ $cfg.bodySize }}";
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2016-03-19 23:29:29 +00:00
|
|
|
{{ if $cfg.useProxyProtocol }}
|
2016-03-22 18:01:04 +00:00
|
|
|
set_real_ip_from {{ $cfg.proxyRealIpCidr }};
|
|
|
|
real_ip_header proxy_protocol;
|
2016-02-22 00:13:08 +00:00
|
|
|
{{ end }}
|
|
|
|
|
2016-03-19 23:29:29 +00:00
|
|
|
log_format upstreaminfo '{{ if $cfg.useProxyProtocol }}$proxy_protocol_addr{{ else }}$remote_addr{{ end }} - '
|
2016-03-19 20:17:58 +00:00
|
|
|
'[$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
|
2016-03-26 21:25:51 +00:00
|
|
|
map $http_x_forwarded_proto $pass_access_scheme {
|
2016-02-22 00:13:08 +00:00
|
|
|
default $http_x_forwarded_proto;
|
|
|
|
'' $scheme;
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:25:51 +00:00
|
|
|
map $http_x_forwarded_proto $pass_forwarded_for {
|
|
|
|
default $http_x_forwarded_for;
|
|
|
|
'' $proxy_add_x_forwarded_for;
|
|
|
|
}
|
|
|
|
|
|
|
|
map $pass_access_scheme $sts {
|
2016-03-19 23:29:29 +00:00
|
|
|
'https' 'max-age={{ $cfg.htsMaxAge }}{{ if $cfg.htsIncludeSubdomains }}; includeSubDomains{{ end }}; preload';
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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-03-15 15:31:39 +00:00
|
|
|
# Custom error pages
|
2016-02-22 00:13:08 +00:00
|
|
|
proxy_intercept_errors on;
|
|
|
|
|
2016-03-22 18:01:04 +00:00
|
|
|
error_page 403 = @custom_403;
|
|
|
|
error_page 404 = @custom_404;
|
|
|
|
error_page 405 = @custom_405;
|
|
|
|
error_page 408 = @custom_408;
|
|
|
|
error_page 413 = @custom_413;
|
|
|
|
error_page 501 = @custom_501;
|
|
|
|
error_page 502 = @custom_502;
|
|
|
|
error_page 503 = @custom_503;
|
|
|
|
error_page 504 = @custom_504;
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
# In case of errors try the next upstream server before returning an error
|
2016-03-22 18:01:04 +00:00
|
|
|
proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
server {
|
2016-03-19 23:29:29 +00:00
|
|
|
listen 80 default_server{{ if $cfg.useProxyProtocol }} proxy_protocol{{ end }};
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
location / {
|
2016-03-15 15:31:39 +00:00
|
|
|
return 200;
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 21:25:51 +00:00
|
|
|
location /nginx_status {
|
|
|
|
allow 127.0.0.1;
|
|
|
|
deny all;
|
|
|
|
|
|
|
|
access_log off;
|
|
|
|
stub_status on;
|
|
|
|
}
|
|
|
|
|
2016-03-19 20:17:58 +00:00
|
|
|
{{ template "CUSTOM_ERRORS" $cfg }}
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
{{range $name, $upstream := .upstreams}}
|
|
|
|
upstream {{$upstream.Name}} {
|
|
|
|
least_conn;
|
|
|
|
{{range $server := $upstream.Backends}}server {{$server.Address}}:{{$server.Port}};
|
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
|
2016-03-15 02:29:13 +00:00
|
|
|
{{ range $server := .servers }}
|
|
|
|
server {
|
2016-03-15 15:31:39 +00:00
|
|
|
listen 80;
|
|
|
|
{{ if $server.SSL }}listen 443 ssl http2;
|
|
|
|
ssl_certificate {{ $server.SSLCertificate }};
|
|
|
|
ssl_certificate_key {{ $server.SSLCertificateKey }};{{ end }}
|
2016-03-26 21:25:51 +00:00
|
|
|
{{ if $cfg.enableVtsStatus }}
|
|
|
|
vhost_traffic_status_filter_by_set_key {{ $server.Name }} application::*;
|
|
|
|
{{ end }}
|
2016-03-15 15:31:39 +00:00
|
|
|
|
|
|
|
server_name {{ $server.Name }};
|
2016-03-22 18:01:04 +00:00
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
{{ if $server.SSL }}
|
|
|
|
if ($scheme = http) {
|
|
|
|
return 301 https://$host$request_uri;
|
|
|
|
}
|
|
|
|
{{ end }}
|
|
|
|
{{ range $location := $server.Locations }}
|
|
|
|
location {{ $location.Path }} {
|
2016-03-22 18:01:04 +00:00
|
|
|
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;
|
|
|
|
|
2016-03-26 21:25:51 +00:00
|
|
|
proxy_set_header X-Forwarded-For $pass_forwarded_for;
|
2016-03-22 18:01:04 +00:00
|
|
|
proxy_set_header X-Forwarded-Host $host;
|
|
|
|
proxy_set_header X-Forwarded-Server $host;
|
2016-03-26 21:25:51 +00:00
|
|
|
proxy_set_header X-Forwarded-Proto $pass_access_scheme;
|
2016-03-22 18:01:04 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
proxy_pass http://{{ $location.Upstream.Name }};
|
|
|
|
}
|
|
|
|
{{ end }}
|
2016-03-19 20:17:58 +00:00
|
|
|
{{ template "CUSTOM_ERRORS" $cfg }}
|
2016-03-15 15:31:39 +00:00
|
|
|
}
|
|
|
|
{{ end }}
|
2016-03-15 02:29:13 +00:00
|
|
|
|
2016-02-22 00:13:08 +00:00
|
|
|
# default server, including healthcheck
|
|
|
|
server {
|
2016-03-19 23:29:29 +00:00
|
|
|
listen 8080 default_server{{ if $cfg.useProxyProtocol }} proxy_protocol{{ end }} reuseport;
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
location /healthz {
|
|
|
|
access_log off;
|
|
|
|
return 200;
|
|
|
|
}
|
2016-03-15 15:31:39 +00:00
|
|
|
|
2016-02-22 00:13:08 +00:00
|
|
|
location /health-check {
|
|
|
|
access_log off;
|
|
|
|
proxy_pass http://127.0.0.1:10249/healthz;
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:25:51 +00:00
|
|
|
location /nginx_status {
|
|
|
|
{{ if $cfg.enableVtsStatus }}
|
|
|
|
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-03-26 21:25:51 +00:00
|
|
|
{{ end }}
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location / {
|
2016-03-19 20:17:58 +00:00
|
|
|
proxy_pass http://upstream-default-backend;
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-19 20:17:58 +00:00
|
|
|
{{ template "CUSTOM_ERRORS" $cfg }}
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-16 14:12:45 +00:00
|
|
|
|
|
|
|
# default server for services without endpoints
|
|
|
|
server {
|
2016-03-19 23:29:29 +00:00
|
|
|
listen 8181;
|
2016-03-16 14:12:45 +00:00
|
|
|
|
|
|
|
location / {
|
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(503)
|
2016-03-16 14:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# TCP services
|
|
|
|
stream {
|
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 }};
|
2016-03-19 20:17:58 +00:00
|
|
|
{{ end }}
|
|
|
|
}
|
|
|
|
|
2016-02-22 00:13:08 +00:00
|
|
|
server {
|
2016-03-19 23:29:29 +00:00
|
|
|
listen {{ $tcpServer.Path }};
|
2016-03-22 18:01:04 +00:00
|
|
|
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 }}
|
|
|
|
}
|
|
|
|
|
|
|
|
{{/* definition of templates to avoid repetitions */}}
|
|
|
|
{{ define "CUSTOM_ERRORS" }}
|
|
|
|
location @custom_403 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(403)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location @custom_404 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(404)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location @custom_405 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(405)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location @custom_408 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(408)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-15 02:29:13 +00:00
|
|
|
}
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
location @custom_413 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(413)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location @custom_502 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(502)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-15 02:29:13 +00:00
|
|
|
}
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
location @custom_503 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(503)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location @custom_504 {
|
2016-03-22 18:01:04 +00:00
|
|
|
internal;
|
2016-02-22 00:13:08 +00:00
|
|
|
content_by_lua_block {
|
2016-03-19 20:17:58 +00:00
|
|
|
openURL(504)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{{ end }}
|