Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for accepting the Proxy Protocol #1487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 21 additions & 12 deletions nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ map $scheme $proxy_x_forwarded_ssl {

gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

{{ if and $.Env.ACCEPT_PROXY_PROTOCOL $.Env.SET_REAL_IP_FROM }}
{{ range split $.Env.SET_REAL_IP_FROM "," }}
set_real_ip_from {{ . }};
{{ end }}
real_ip_header proxy_protocol;
{{ end }}

log_format vhost '$host $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
Expand Down Expand Up @@ -140,12 +147,14 @@ proxy_set_header Proxy "";

{{ $access_log := (or (and (not $.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}

{{ $proxy_protocol := (and (or ($.Env.ACCEPT_PROXY_PROTOCOL) "") "proxy_protocol") }}

{{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }}
server {
server_name _; # This is just an invalid value which will never trigger on a real hostname.
listen {{ $external_http_port }};
listen {{ $external_http_port }} {{ $proxy_protocol }};
{{ if $enable_ipv6 }}
listen [::]:{{ $external_http_port }};
listen [::]:{{ $external_http_port }} {{ $proxy_protocol }};
{{ end }}
{{ $access_log }}
return 503;
Expand All @@ -154,9 +163,9 @@ server {
{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
server {
server_name _; # This is just an invalid value which will never trigger on a real hostname.
listen {{ $external_https_port }} ssl http2;
listen {{ $external_https_port }} ssl http2 {{ $proxy_protocol }};
{{ if $enable_ipv6 }}
listen [::]:{{ $external_https_port }} ssl http2;
listen [::]:{{ $external_https_port }} ssl http2 {{ $proxy_protocol }};
{{ end }}
{{ $access_log }}
return 503;
Expand Down Expand Up @@ -246,9 +255,9 @@ upstream {{ $upstream_name }} {
{{ if eq $https_method "redirect" }}
server {
server_name {{ $host }};
listen {{ $external_http_port }} {{ $default_server }};
listen {{ $external_http_port }} {{ $default_server }} {{ $proxy_protocol }};
{{ if $enable_ipv6 }}
listen [::]:{{ $external_http_port }} {{ $default_server }};
listen [::]:{{ $external_http_port }} {{ $default_server }} {{ $proxy_protocol }};
{{ end }}
{{ $access_log }}

Expand All @@ -269,9 +278,9 @@ server {

server {
server_name {{ $host }};
listen {{ $external_https_port }} ssl http2 {{ $default_server }};
listen {{ $external_https_port }} ssl http2 {{ $default_server }} {{ $proxy_protocol }};
{{ if $enable_ipv6 }}
listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }};
listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }} {{ $proxy_protocol }};
{{ end }}
{{ $access_log }}

Expand Down Expand Up @@ -341,9 +350,9 @@ server {

server {
server_name {{ $host }};
listen {{ $external_http_port }} {{ $default_server }};
listen {{ $external_http_port }} {{ $default_server }} {{ $proxy_protocol }};
{{ if $enable_ipv6 }}
listen [::]:80 {{ $default_server }};
listen [::]:80 {{ $default_server }} {{ $proxy_protocol }};
{{ end }}
{{ $access_log }}

Expand Down Expand Up @@ -386,9 +395,9 @@ server {
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
server {
server_name {{ $host }};
listen {{ $external_https_port }} ssl http2 {{ $default_server }};
listen {{ $external_https_port }} ssl http2 {{ $default_server }} {{ $proxy_protocol }};
{{ if $enable_ipv6 }}
listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }};
listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }} {{ $proxy_protocol }};
{{ end }}
{{ $access_log }}
return 500;
Expand Down