Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ COPY src /
ENV KEEPALIVE_TIMEOUT=65
ENV PROXY_UWSGI=0
ENV LISTEN_PORT=80
ENV HEALTHCHECK_PATH="/lb-status/"
ENV STATIC_LOCATIONS=
EXPOSE 80
STOPSIGNAL SIGQUIT
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Pair nginx-proxy with your favorite upstream server (wsgi, uwsgi, asgi, et al.)
| `STATIC_LOCATIONS` | Static asset mappings | No | | |
| `PROXY_UWSGI` | Whether to use native uwsgi support | No | 0 | 1 |
| `KEEPALIVE_TIMEOUT` | What value to set HTTP keepalive (This should be higher than your ELB's timeout) | Yes | 65 | |
| `HEALTHCHECK_PATH` | nginx-proxy disables healthcheck path access logs, you can configure the path here | Yes | /lb-status/ | |

### Hosting Static Assets

Expand Down
24 changes: 14 additions & 10 deletions src/docker-entrypoint.d/00-render-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ set -eo pipefail

source /docker-entrypoint.d/functions

# Render main nginx.conf
cat "/etc/nginx/nginx.conf.template" | gomplate > "/etc/nginx/nginx.conf"
function render_templates {
local src="$1"
local dst="$2"
for f in $src; do
final=$(basename "$f")
final=${final%.template}
final="$dst/$final"
cat "$f" | gomplate > "$final"
log "$0: Rendered $f and moved it to $final"
done
}

for f in /etc/nginx/templates/*.template
do
final=$(basename "$f")
final=${final%.template}
final="/etc/nginx/conf.d/$final"
cat "$f" | gomplate > "$final"
log "$0: Rendered $f and moved it to $final"
done
render_templates "/etc/nginx/*.template" "/etc/nginx"
render_templates "/etc/nginx/conf.d/*.template" "/etc/nginx/conf.d"
render_templates "/etc/nginx/includes/*.template" "/etc/nginx/includes"
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ server {
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

{{ if (eq .Env.PROXY_UWSGI "1") }}
location / {
uwsgi_pass app;
uwsgi_param HTTP_X_REQUEST_ID $request_id;
uwsgi_param HTTP_HOST $host;
include uwsgi_params;
uwsgi_read_timeout {{ .Env.KEEPALIVE_TIMEOUT }};
uwsgi_send_timeout {{ .Env.KEEPALIVE_TIMEOUT }};
{{ if (eq .Env.PROXY_UWSGI "1") }}
include /etc/nginx/includes/uwsgi.conf;
{{ else }}
include /etc/nginx/includes/proxy.conf;
{{ end }}
}
{{ else }}
location / {
proxy_set_header X-Request-ID $request_id;
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://app;

location {{ .Env.HEALTHCHECK_PATH }} {
access_log off;
{{ if (eq .Env.PROXY_UWSGI "1") }}
include /etc/nginx/includes/uwsgi.conf;
{{ else }}
include /etc/nginx/includes/proxy.conf;
{{ end }}
}
{{ end }}

{{ if .Env.STATIC_LOCATIONS }}
{{ range (.Env.STATIC_LOCATIONS | strings.Split "," )}}
Expand Down
2 changes: 1 addition & 1 deletion src/etc/nginx/conf.d/status.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# NGINX status/stats used by datadog, et al.
server {
listen 8091;
server_name localhost;
server_name _;

access_log off;
allow 127.0.0.1;
Expand Down
4 changes: 4 additions & 0 deletions src/etc/nginx/includes/proxy.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proxy_set_header X-Request-ID $request_id;
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://app;
6 changes: 6 additions & 0 deletions src/etc/nginx/includes/uwsgi.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
uwsgi_pass app;
uwsgi_param HTTP_X_REQUEST_ID $request_id;
uwsgi_param HTTP_HOST $host;
include uwsgi_params;
uwsgi_read_timeout {{ .Env.KEEPALIVE_TIMEOUT }};
uwsgi_send_timeout {{ .Env.KEEPALIVE_TIMEOUT }};
4 changes: 3 additions & 1 deletion src/etc/nginx/nginx.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ http {
more_clear_headers "Server";
more_clear_headers "server";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/includes/log-format.conf;

# For docker logs to work, we need to output to stdout/stderr
access_log /dev/stdout json_analytics;

include /etc/nginx/conf.d/*.conf;
}
29 changes: 29 additions & 0 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ func getHTTPClient() *http.Client {
}
}

func TestHealth(t *testing.T) {
u, err := getTestURL()
if err != nil {
t.Fatal(err)
}

parsed, err := url.Parse(u)
if err != nil {
t.Fatal(err)
}

req, err := http.NewRequest("GET", parsed.JoinPath("/health").String(), nil)

if err != nil {
t.Fatal(err)
}

client := getHTTPClient()
res, err := client.Do(req)

if err != nil {
t.Fatal(err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("Expected HTTP %d but got %d", http.StatusOK, res.StatusCode)
}
}

func TestStatic(t *testing.T) {
u, err := getTestURL()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LISTEN_PORT="8080" \
KEEPALIVE_TIMEOUT="65" \
PROXY_REVERSE_URL="http://localhost:8081" \
SERVER_NAME="localhost" \
HEALTHCHECK_PATH="/health" \
STATIC_LOCATIONS="/static/:/test/static/" \
/docker-entrypoint.sh

Expand Down