From 5b7fa72d18d0bcd465f95b714c55f458018ddbe3 Mon Sep 17 00:00:00 2001 From: geospatial-jeff Date: Sun, 27 Nov 2022 19:43:23 -0700 Subject: [PATCH 1/4] add nginx service to docker-compose, proxy pgstac and sqlalchemy apps --- docker-compose.yml | 13 +++++++++ nginx.conf | 29 +++++++++++++++++++ .../pgstac/stac_fastapi/pgstac/app.py | 1 + .../sqlalchemy/stac_fastapi/sqlalchemy/app.py | 3 ++ 4 files changed, 46 insertions(+) create mode 100644 nginx.conf diff --git a/docker-compose.yml b/docker-compose.yml index 506a4a37b..91476859e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,16 @@ version: '3' services: + nginx: + image: nginx + ports: + - 80:80 + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf + depends_on: + - app-pgstac + - app-sqlalchemy + command: ["nginx-debug", "-g", "daemon off;"] + app-sqlalchemy: container_name: stac-fastapi-sqlalchemy image: stac-utils/stac-fastapi @@ -19,6 +30,7 @@ services: - POSTGRES_HOST_WRITER=database - POSTGRES_PORT=5432 - WEB_CONCURRENCY=10 + - UVICORN_ROOT_PATH=/api/v1/sqlalchemy ports: - "8081:8081" volumes: @@ -50,6 +62,7 @@ services: - DB_MIN_CONN_SIZE=1 - DB_MAX_CONN_SIZE=1 - USE_API_HYDRATE=${USE_API_HYDRATE:-false} + - UVICORN_ROOT_PATH=/api/v1/pgstac ports: - "8082:8082" volumes: 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") From 88449299ffe19b9ad495a62da5d84915276f0fda Mon Sep 17 00:00:00 2001 From: Jeff Albrecht Date: Mon, 28 Nov 2022 10:56:12 -0700 Subject: [PATCH 2/4] Update docker-compose.yml Co-authored-by: Christian Wygoda <103508637+c-wygoda@users.noreply.github.com> --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 91476859e..be6beb37a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ services: nginx: image: nginx ports: - - 80:80 + - ${STAC_FASTAPI_NGINX_PORT:-80}:80 volumes: - ./nginx.conf:/etc/nginx/nginx.conf depends_on: From 72cab052b1dfaa6bd0258a47bd9dcf42778533d3 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Wed, 18 Jan 2023 11:55:34 -0700 Subject: [PATCH 3/4] 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. --- Makefile | 4 ++++ README.md | 20 +++++++++++++++++++- docker-compose.nginx.yml | 18 ++++++++++++++++++ docker-compose.yml | 13 ------------- 4 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 docker-compose.nginx.yml 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 d26c12a32..85b03d930 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/docker-compose.yml b/docker-compose.yml index be6beb37a..506a4a37b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,16 +1,5 @@ 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-sqlalchemy: container_name: stac-fastapi-sqlalchemy image: stac-utils/stac-fastapi @@ -30,7 +19,6 @@ services: - POSTGRES_HOST_WRITER=database - POSTGRES_PORT=5432 - WEB_CONCURRENCY=10 - - UVICORN_ROOT_PATH=/api/v1/sqlalchemy ports: - "8081:8081" volumes: @@ -62,7 +50,6 @@ services: - DB_MIN_CONN_SIZE=1 - DB_MAX_CONN_SIZE=1 - USE_API_HYDRATE=${USE_API_HYDRATE:-false} - - UVICORN_ROOT_PATH=/api/v1/pgstac ports: - "8082:8082" volumes: From 775a23d3fdb3ccbd91c6e42cb89670435c03c37a Mon Sep 17 00:00:00 2001 From: Nathan Zimmerman Date: Mon, 27 Feb 2023 15:53:22 -0600 Subject: [PATCH 4/4] Update changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) 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))