From 1de7b8b270d6c564718cf41fc3cd68acb5edb16a Mon Sep 17 00:00:00 2001 From: Jeff Albrecht Date: Mon, 27 Feb 2023 15:13:15 -0700 Subject: [PATCH] Add nginx service as second docker-compose stack (#503) * add nginx service to docker-compose, proxy pgstac and sqlalchemy apps * Update docker-compose.yml Co-authored-by: Christian Wygoda <103508637+c-wygoda@users.noreply.github.com> * feat: add nginx proxy in own docker-compose file Includes a Makefile rule to run it, and some documentation in README.md. Also adds a markdownlint silencer and one or two touchups to the whitespace. * Update changelog --------- Co-authored-by: Christian Wygoda <103508637+c-wygoda@users.noreply.github.com> Co-authored-by: Pete Gadomski Co-authored-by: Nathan Zimmerman --- CHANGES.md | 1 + Makefile | 4 +++ README.md | 20 ++++++++++++- docker-compose.nginx.yml | 18 ++++++++++++ nginx.conf | 29 +++++++++++++++++++ .../pgstac/stac_fastapi/pgstac/app.py | 1 + .../sqlalchemy/stac_fastapi/sqlalchemy/app.py | 3 ++ 7 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 docker-compose.nginx.yml create mode 100644 nginx.conf diff --git a/CHANGES.md b/CHANGES.md index 57ddeb2a6..64802cc38 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ ### Added +* Nginx service as second docker-compose stack to demonstrate proxy ([#503](https://github.com/stac-utils/stac-fastapi/pull/503)) * Validation checks in CI using [stac-api-validator](github.com/stac-utils/stac-api-validator) ([#508](https://github.com/stac-utils/stac-fastapi/pull/508)) * Required links to the sqlalchemy ItemCollection endpoint ([#508](https://github.com/stac-utils/stac-fastapi/pull/508)) * Publication of docker images to GHCR ([#525](https://github.com/stac-utils/stac-fastapi/pull/525)) diff --git a/Makefile b/Makefile index 36187c2e8..fbeb7d85f 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,10 @@ docker-run-sqlalchemy: image docker-run-pgstac: image $(run_pgstac) +.PHONY: docker-run-nginx-proxy +docker-run-nginx-proxy: + docker-compose -f docker-compose.yml -f docker-compose.nginx.yml up + .PHONY: docker-shell-sqlalchemy docker-shell-sqlalchemy: $(run_sqlalchemy) /bin/bash diff --git a/README.md b/README.md index d621cc005..1fc149ee7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ + +

FastAPI implemention of the STAC API spec.

@@ -101,7 +103,23 @@ The application will be started on . By default, the apps are run with uvicorn hot-reloading enabled. This can be turned off by changing the value of the `RELOAD` env var in docker-compose.yml to `false`. -#### Note to Docker for Windows users +### nginx proxy + +This repo includes an example nginx proxy service. +To start: + +```shell +make docker-run-nginx-proxy +``` + +The proxy will be started on , with the pgstac app available at and the sqlalchemy app at . +If you need to customize the proxy port, use the `STAC_FASTAPI_NGINX_PORT` environment variable: + +```shell +STAC_FASTAPI_NGINX_PORT=7822 make docker-run-nginx-proxy +``` + +### Note to Docker for Windows users You'll need to enable experimental features on Docker for Windows in order to run the docker-compose, due to the "--platform" flag that is required to allow the project to run on some Apple architectures. diff --git a/docker-compose.nginx.yml b/docker-compose.nginx.yml new file mode 100644 index 000000000..b70bffe50 --- /dev/null +++ b/docker-compose.nginx.yml @@ -0,0 +1,18 @@ +version: '3' +services: + nginx: + image: nginx + ports: + - ${STAC_FASTAPI_NGINX_PORT:-80}:80 + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf + depends_on: + - app-pgstac + - app-sqlalchemy + command: [ "nginx-debug", "-g", "daemon off;" ] + app-pgstac: + environment: + - UVICORN_ROOT_PATH=/api/v1/pgstac + app-sqlalchemy: + environment: + - UVICORN_ROOT_PATH=/api/v1/sqlalchemy diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 000000000..0084e149e --- /dev/null +++ b/nginx.conf @@ -0,0 +1,29 @@ +events {} + +http { + server { + listen 80; + + location /api/v1/pgstac { + rewrite ^/api/v1/pgstac(.*)$ $1 break; + proxy_pass http://app-pgstac:8082; + proxy_set_header HOST $host; + proxy_set_header Referer $http_referer; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /api/v1/sqlalchemy { + rewrite ^/api/v1/sqlalchemy(.*)$ $1 break; + proxy_pass http://app-sqlalchemy:8081; + proxy_set_header HOST $host; + proxy_set_header Referer $http_referer; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location / { + proxy_redirect off; + } + } +} \ No newline at end of file diff --git a/stac_fastapi/pgstac/stac_fastapi/pgstac/app.py b/stac_fastapi/pgstac/stac_fastapi/pgstac/app.py index 904f4d1ee..8a33424bb 100644 --- a/stac_fastapi/pgstac/stac_fastapi/pgstac/app.py +++ b/stac_fastapi/pgstac/stac_fastapi/pgstac/app.py @@ -88,6 +88,7 @@ def run(): port=settings.app_port, log_level="info", reload=settings.reload, + root_path=os.getenv("UVICORN_ROOT_PATH", ""), ) except ImportError: raise RuntimeError("Uvicorn must be installed in order to use command") diff --git a/stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/app.py b/stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/app.py index 29a0894ac..038d92b80 100644 --- a/stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/app.py +++ b/stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/app.py @@ -1,4 +1,6 @@ """FastAPI application.""" +import os + from stac_fastapi.api.app import StacApi from stac_fastapi.api.models import create_get_request_model, create_post_request_model from stac_fastapi.extensions.core import ( @@ -55,6 +57,7 @@ def run(): port=settings.app_port, log_level="info", reload=settings.reload, + root_path=os.getenv("UVICORN_ROOT_PATH", ""), ) except ImportError: raise RuntimeError("Uvicorn must be installed in order to use command")