Enterprise-Onion-Toolkit/templates.d/nginx.conf.txt

228 wiersze
5.7 KiB
Plaintext
Czysty Zwykły widok Historia

2017-02-01 08:38:03 +00:00
# -*- awk -*-
# eotk (c) 2017 Alec Muffett
# EMACS awk mode works quite well for nginx configs
# logs and pids
pid %PROJECT_DIR%/nginx.pid;
error_log %LOG_DIR%/nginx-error.log %NGINX_SYSLOG%;
2017-02-01 08:38:03 +00:00
# performance
%%IF %IS_SOFTMAP%
worker_processes %SOFTMAP_NGINX_WORKERS%; # softmap
%%ELSE
worker_processes %NGINX_WORKERS%; # hardmap
%%ENDIF
worker_rlimit_nofile %NGINX_RLIM%;
events {
worker_connections %NGINX_RLIM%;
}
http {
# dns for proxy (sigh)
2017-02-07 11:35:33 +00:00
resolver %NGINX_RESOLVER% valid=%NGINX_TIMEOUT%s; # should be able to do `ipv6=off` here, but problems
2017-02-01 08:38:03 +00:00
resolver_timeout %NGINX_TIMEOUT%s;
proxy_buffering on;
proxy_buffers 16 64k;
proxy_buffer_size 64k;
proxy_busy_buffers_size 512k;
2017-02-07 11:35:33 +00:00
proxy_max_temp_file_size 2048k;
proxy_temp_file_write_size 64k;
proxy_temp_path "/tmp";
2017-02-01 09:56:44 +00:00
2017-02-01 08:38:03 +00:00
# logs
access_log %LOG_DIR%/nginx-access.log;
# global settings
server_tokens off;
# allow/deny (first wins)
allow "unix:";
deny all;
# rewrite these content types; text/html is implicit
subs_filter_types
application/javascript
application/json
application/x-javascript
text/css
text/javascript
text/xml
;
2017-02-11 22:14:18 +00:00
# subs filters
# - named capture groups appear not to work
# - anchor slightly more firmly with a single slash
# -- double-slash would break https:\/\/foo.net\/ weirdness
2017-02-11 22:14:18 +00:00
%%BEGIN
subs_filter
/(([-0-9a-z]+\.)+)?%DNS_DOMAIN_RE%\b
/$1%ONION_ADDRESS%
gir;
2017-02-11 22:14:18 +00:00
%%END
# fix the cookies
%%BEGIN
proxy_cookie_domain
%DNS_DOMAIN%
%ONION_ADDRESS%
;
2017-02-11 22:14:18 +00:00
%%END
# fix the header-redirects
%%BEGIN
proxy_redirect
~*^(.*?)\b%DNS_DOMAIN_RE%\b(.*)$
$1%ONION_ADDRESS%$2
;
2017-02-11 22:14:18 +00:00
%%END
2017-02-26 20:44:39 +00:00
# o2d_lookup -> if cannot remap, return input. note: old versions
# of lua-plugin cannot cope with code like o2d_mappings[o[1]]
# because of `long bracket syntax`; the `[o[` freaks it out.
# See: https://github.com/openresty/lua-nginx-module/issues/748
2017-02-07 10:05:53 +00:00
init_by_lua_block {
slog = function (s) -- in case of manual debugging
ngx.log(ngx.ERR, "\n<<", s, ">>\n")
2017-02-07 15:27:41 +00:00
return
end
o2d_mappings = {}
2017-02-07 10:05:53 +00:00
%%BEGIN
o2d_mappings["%ONION_ADDRESS%"] = "%DNS_DOMAIN%"
2017-02-07 10:05:53 +00:00
%%END
o2d_lookup = function (o)
2017-02-26 20:44:39 +00:00
local k = o[1]
return ( o2d_mappings[k] or k )
2017-02-07 10:05:53 +00:00
end
onion2dns = function (i)
2017-02-07 10:05:53 +00:00
if i == nil then
return nil
end
local o, num, errs = ngx.re.gsub(i, "\\b([a-z2-7]{16}\\.onion)\\b", o2d_lookup, "io")
2017-02-07 10:05:53 +00:00
return o
end
dns2onion = function (i) -- inherently a bit flaky because ordering, boundaries; avoid
local num, errs
%%BEGIN
i, num, errs = ngx.re.gsub(i, "\\b(%DNS_DOMAIN_RE2%)\\b", "%ONION_ADDRESS%", "io")
%%END
return i
end
2017-02-07 10:05:53 +00:00
}
2017-02-06 13:42:48 +00:00
%%IF %HEADER_CSP_SUPPRESS%
2017-02-07 11:00:56 +00:00
# csp suppression
2017-02-06 13:42:48 +00:00
proxy_hide_header "Content-Security-Policy";
2017-02-07 11:00:56 +00:00
proxy_hide_header "Content-Security-Policy-Report-Only";
%%ELSE
# csp not suppressed
2017-02-06 13:42:48 +00:00
%%ENDIF
2017-02-07 11:00:56 +00:00
%%IF %HEADER_HSTS_SUPPRESS%
# hsts suppression
proxy_hide_header "Strict-Transport-Security";
%%ELSE
# hsts not suppressed
%%ENDIF
2017-02-01 08:38:03 +00:00
%%IF %HEADER_HPKP_SUPPRESS%
# hpkp suppression
proxy_hide_header "Public-Key-Pins";
proxy_hide_header "Public-Key-Pins-Report-Only";
%%ELSE
# hpkp not suppressed
2017-02-07 11:00:56 +00:00
%%ENDIF
2017-02-01 08:38:03 +00:00
# global proxy settings
proxy_read_timeout %NGINX_TIMEOUT%;
proxy_connect_timeout %NGINX_TIMEOUT%;
# SSL config
ssl_certificate %SSL_DIR%/%CERT_PREFIX%.cert;
ssl_certificate_key %SSL_DIR%/%CERT_PREFIX%.pem;
2017-02-01 13:23:46 +00:00
#ssl_ciphers 'EECDH+CHACHA20:EECDH+AESGCM:EECDH+AES256'; ## LibreSSL, OpenSSL 1.1.0+
ssl_ciphers 'EECDH+AESGCM:EECDH+AES256'; ## OpenSSL 1.0.1% to 1.0.2%
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
2017-02-01 13:23:46 +00:00
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_buffer_size 4k;
ssl_prefer_server_ciphers on;
ssl_ecdh_curve prime256v1;
2017-02-01 13:23:46 +00:00
#ssl_ecdh_curve secp384r1:prime256v1; ## NGINX nginx 1.11.0 and later
2017-02-01 08:38:03 +00:00
# websockets
map $http_upgrade $connection_upgrade {
default "upgrade";
"" "";
}
%%BEGIN
# for %ONION_ADDRESS% -> %DNS_DOMAIN%
server {
%%IF %IS_SOFTMAP%
%%RANGE I 1 %SOFTMAP_TOR_WORKERS%
# softmap onion %I%
listen unix:%PROJECT_DIR%/%TOR_WORKER_PREFIX%-%I%.d/port-80.sock;
listen unix:%PROJECT_DIR%/%TOR_WORKER_PREFIX%-%I%.d/port-443.sock ssl;
2017-02-01 08:38:03 +00:00
%%ENDRANGE
%%ELSE
# hardmap
# unix sockets; use <ONION_ADDRESS>.d as a naming convention
listen unix:%PROJECT_DIR%/%ONION_ADDRESS%.d/port-80.sock;
listen unix:%PROJECT_DIR%/%ONION_ADDRESS%.d/port-443.sock ssl;
2017-02-01 08:38:03 +00:00
%%ENDIF
# subdomain regexp captures trailing dot, use carefully; does not need "~*"
2017-02-01 08:38:03 +00:00
server_name
%ONION_ADDRESS%
~^(?<snsd>([-0-9a-z]+\.)+)%ONION_ADDRESS_RE%$
2017-02-01 08:38:03 +00:00
;
%%IF %NGINX_HELLO_ONION%
# for test & to help SSL certificate acceptance
location ~*^/hello[-_]onion/?$ {
2017-02-01 08:38:03 +00:00
return 200 "Hello, Onion User!";
}
%%ENDIF
# for traffic
location / {
proxy_pass "$scheme://${snsd}%DNS_DOMAIN%"; # note $scheme
2017-02-01 08:38:03 +00:00
proxy_http_version 1.1;
proxy_set_header Host "${snsd}%DNS_DOMAIN%";
2017-02-01 08:38:03 +00:00
proxy_set_header Accept-Encoding ""; # but putting this in `http` fails?
proxy_set_header Connection $connection_upgrade; # SSL
proxy_set_header Upgrade $http_upgrade; # SSL
proxy_ssl_server_name on; # SSL
2017-02-07 10:05:53 +00:00
set_by_lua_block $referer2 {
return onion2dns(ngx.var.http_referer)
2017-02-07 10:05:53 +00:00
}
proxy_set_header Referer $referer2;
set_by_lua_block $origin2 {
return onion2dns(ngx.var.http_origin)
2017-02-07 10:05:53 +00:00
}
proxy_set_header Origin $origin2;
2017-02-01 08:38:03 +00:00
}
}
%%END
# header purge
more_clear_headers "Age";
more_clear_headers "Server";
more_clear_headers "Via";
more_clear_headers "X-From-Nginx";
more_clear_headers "X-NA";
more_clear_headers "X-Powered-By";
more_clear_headers "X-Request-Id";
more_clear_headers "X-Runtime";
more_clear_headers "X-Varnish";
}