From 53110e9bbdc488c1fadf82e73e2e3faa07a51bdd Mon Sep 17 00:00:00 2001 From: Bennett Goble Date: Tue, 13 Feb 2024 07:56:30 -0800 Subject: [PATCH] Use upstream module with proxy_pass Place servers in an upstream module block to allow greater flexibility in definition. `fail_timeout`, etc. can now be specified, and unix sockets and tcp servers both used. --- README.md | 5 +++-- src/docker-entrypoint.sh | 5 +++++ src/etc/nginx/templates/default.conf.template | 9 +++++++-- test_uwsgi/test.sh | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3da401e..3ecd0c9 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Pair nginx-proxy with your favorite upstream server (wsgi, uwsgi, asgi, et al.) | Environment Variable | Description | Required | Default | Example | |----------------------|-------------|----------|---------|---------| | `LISTEN_PORT` | Server port | Yes | 80 | | -| `PROXY_REVERSE_URL` | Upstream server URL | Yes | | http://myapp:8080 | +| `UPSTREAM_SERVER` | Upstream server | Yes | | myapp:8080 fail_timeout=0, unix://mnt/server.sock | +| `PROXY_REVERSE_URL` | Upstream server URL (Deprecated, please use UPSTREAM_SERVER) | No | | http://myapp:8080 | | `SERVER_NAME` | Allowed server names (hostnames) | Yes | | | | `SILENT` | Silence entrypoint output | No | | | | `STATIC_LOCATIONS` | Static asset mappings | No | | | @@ -46,7 +47,7 @@ The syntax of `STATIC_LOCATIONS` is `HOSTED_PATH1:LOCAL_PATH1,HOSTED_PATH2:LOCAL ## uWSGI If you wish to use this service with uWSGI then define `PROXY_UWSGI=1` and set -`PROXY_REVERSE_URL` to be the uwsgi `--socket` address of your app. (Do not +`UPSTREAM_SERVER` to be the uwsgi `--socket` address of your app. (Do not use `http://`, ex. if your uwsgi server is hosting itself at `--socket :8000` then set `PROXY_REVERSE_URL=localhost:8000`.) diff --git a/src/docker-entrypoint.sh b/src/docker-entrypoint.sh index 7cde702..4740551 100755 --- a/src/docker-entrypoint.sh +++ b/src/docker-entrypoint.sh @@ -2,6 +2,11 @@ set -e +# Transform legacy PROXY_REVERSE_URL to UPSTREAM_SERVER +if [[ -n $PROXY_REVERSE_URL ]]; then + export UPSTREAM_SERVER=${PROXY_REVERSE_URL#http://} +fi + run-parts --exit-on-error /docker-entrypoint.d exec "$@" diff --git a/src/etc/nginx/templates/default.conf.template b/src/etc/nginx/templates/default.conf.template index 18718cd..7223b9e 100644 --- a/src/etc/nginx/templates/default.conf.template +++ b/src/etc/nginx/templates/default.conf.template @@ -8,6 +8,10 @@ server { } {{ end }} +upstream app { + server {{ .Env.UPSTREAM_SERVER }}; +} + server { listen {{ .Env.LISTEN_PORT }}; server_name {{ .Env.SERVER_NAME }}; @@ -20,7 +24,7 @@ server { {{ if (eq .Env.PROXY_UWSGI "1") }} location / { - uwsgi_pass {{ .Env.PROXY_REVERSE_URL }}; + uwsgi_pass app; uwsgi_param HTTP_X_REQUEST_ID $request_id; uwsgi_param HTTP_HOST $host; include uwsgi_params; @@ -29,9 +33,10 @@ server { } {{ else }} location / { - proxy_pass {{ .Env.PROXY_REVERSE_URL }}; proxy_set_header X-Request-ID $request_id; proxy_set_header Host $host; + proxy_redirect off; + proxy_pass http://app; } {{ end }} diff --git a/test_uwsgi/test.sh b/test_uwsgi/test.sh index 7b9a889..4bde8f9 100755 --- a/test_uwsgi/test.sh +++ b/test_uwsgi/test.sh @@ -9,7 +9,7 @@ function fail { LISTEN_PORT="8080" \ KEEPALIVE_TIMEOUT="65" \ -PROXY_REVERSE_URL="localhost:8081" \ +UPSTREAM_SERVER="localhost:8081" \ SERVER_NAME="localhost" \ PROXY_UWSGI="1" \ STATIC_LOCATIONS="/static/:/test/static/" \