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-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-07-05 16:37:54 +00:00
{{ if $ cfg . useProxyProtocol -}}
set_real_ip_from {{ $ cfg . proxyRealIpCidr }};
real_ip_header proxy_protocol;
{{ else }}
2016-06-17 22:26:08 +00:00
real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;
2016-07-05 16:37:54 +00:00
{{ end -}}
2016-06-17 22:26:08 +00:00
real_ip_recursive on;
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
{{- 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
2016-06-17 22:26:08 +00:00
# lua section 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;
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-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 ';
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 * / } }
map $ request $ loggable {
{{- range $ reqUri := $ cfg . skipAccessLogUrls }}
{{ $ reqUri }} 0;{{ end }}
default 1;
}
access_log /var/log/nginx/access.log upstreaminfo if= $ loggable ;
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 ;
}
# 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-06-18 21:03:27 +00:00
{{- if not $ cfg . enableDynamicTlsRecords }}
ssl_dyn_rec_size_lo 0;
{{ end }}
2016-05-23 23:15:13 +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-05-23 23:15:13 +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
# In case of errors try the next upstream server before returning an error
2016-06-01 14:39:12 +00:00
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
2016-03-15 15:31:39 +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 -}}
2016-03-15 15:31:39 +00:00
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-03-15 15:31:39 +00:00
}
2016-05-16 20:29:33 +00:00
{{ end }}
2016-03-15 15:31:39 +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 */}}
2016-06-01 14:39:12 +00:00
{{- range $ zone := (buildRateLimitZones .servers) }}
2016-05-27 21:03:54 +00:00
{{ $ zone }}
{{ end }}
2016-03-15 02:29:13 +00:00
{{ 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-06-18 21:03:27 +00:00
{{ if $ server . SSL }}listen 443 {{ if $ cfg . useProxyProtocol }}proxy_protocol{{ end }} ssl {{ if $ cfg . enableSpdy }}spdy{{ end }} {{ if $ cfg . useHttp2 }}http2{{ end }};
2016-05-24 14:02:29 +00:00
{{/* comment PEM sha is required to detect changes in the generated configuration and force a reload */}}
# PEM sha: {{ $ server . SSLPemChecksum }}
2016-03-15 15:31:39 +00:00
ssl_certificate {{ $ server . SSLCertificate }};
2016-05-16 20:29:33 +00:00
ssl_certificate_key {{ $ server . SSLCertificateKey }};
2016-06-01 14:39:12 +00:00
{{- end }}
2016-03-15 15:31:39 +00:00
2016-05-16 20:29:33 +00:00
{{ if (and $ server . SSL $ cfg . hsts ) -}}
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-06-01 14:39:12 +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 }}
2016-05-27 14:58:13 +00:00
{{- range $ location := $ server . Locations }}
2016-06-01 14:39:12 +00:00
{{ $ path := buildLocation $ location }}
2016-05-27 14:58:13 +00:00
location {{ $ path }} {
2016-06-06 18:31:40 +00:00
{{ if gt (len $ location . Whitelist . CIDR ) 0 }}
{{- range $ ip := $ location . Whitelist . CIDR }}
allow {{ $ ip }};{{ end }}
deny all;
{{ end -}}
2016-06-05 14:19:55 +00:00
{{ if (and $ server . SSL $ location . Redirect . SSLRedirect ) -}}
# enforce ssl on server side
if ( $ scheme = http) {
return 301 https:// $ host $ request_uri ;
}
{{- end }}
2016-05-30 17:39:10 +00:00
{{/* if the location contains a rate limit annotation, create one */}}
{{ $ limits := buildRateLimit $ location }}
{{- range $ limit := $ limits }}
{{ $ limit }}{{ end }}
2016-05-31 16:22:04 +00:00
2016-06-01 14:39:12 +00:00
{{ if $ location . Auth . Secured }}
2016-05-31 16:22:04 +00:00
{{ if eq $ location . Auth . Type "basic" }}
auth_basic "{{ $ location . Auth . Realm }}";
auth_basic_user_file {{ $ location . Auth . File }};
{{ else }}
#TODO : add nginx - http - auth - digest module
auth_digest "{{ $ location . Auth . Realm }}";
auth_digest_user_file {{ $ location . Auth . File }};
{{ end }}
{{- end }}
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-29 23:30:44 +00:00
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for ;
2016-03-22 18:01:04 +00:00
proxy_set_header X-Forwarded-Host $ host ;
2016-04-16 22:36:45 +00:00
proxy_set_header X-Forwarded-Port $ server_port ;
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-05-27 14:58:13 +00:00
{{/* rewrite only works if the content is not compressed */}}
{{ if $ location . Redirect . AddBaseURL -}}
proxy_set_header Accept-Encoding "";
{{- end }}
{{- buildProxyPass $ location }}
2016-03-15 15:31:39 +00:00
}
{{ end }}
2016-04-05 18:15:59 +00:00
{{ 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 }}
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-07-12 03:04:21 +00:00
listen 8080 default_server reuseport backlog={{ .backlogSize }};
2016-02-22 00:13:08 +00:00
location /healthz {
access_log off;
return 200;
}
2016-03-15 15:31:39 +00:00
2016-03-26 21:25:51 +00:00
location /nginx_status {
2016-05-16 20:29:33 +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-05-16 20:29:33 +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-05-23 23:15:13 +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 / {
2016-05-23 23:15:13 +00:00
{{ if .customErrors }}
2016-03-16 14:12:45 +00:00
content_by_lua_block {
2016-03-19 20:17:58 +00:00
openURL(503)
2016-03-16 14:12:45 +00:00
}
2016-05-23 23:15:13 +00:00
{{ else }}
return 503;
{{ end }}
2016-03-16 14:12:45 +00:00
}
}
2016-02-22 00:13:08 +00:00
}
stream {
2016-03-29 23:30:44 +00:00
# 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 }};
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 }}
2016-03-29 23:30:44 +00:00
# UDP services
{{ range $i, $ udpServer := .udpUpstreams }}
upstream udp-{{ $ udpServer . Upstream . Name }} {
2016-03-30 03:47:20 +00:00
{{ range $ server := $ udpServer . Upstream . Backends }}server {{ $ server . Address }}:{{ $ server . Port }};
2016-03-29 23:30:44 +00:00
{{ end }}
}
server {
2016-03-30 03:47:20 +00:00
listen {{ $ udpServer . Path }} udp;
proxy_timeout 10s;
2016-03-29 23:30:44 +00:00
proxy_responses 1;
2016-03-30 03:47:20 +00:00
proxy_pass udp-{{ $ udpServer . Upstream . Name }};
2016-03-29 23:30:44 +00:00
}
{{ 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 }} {
2016-03-22 18:01:04 +00:00
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 }}