Skip to content

fix links when app is behind proxy #221

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

Merged
merged 1 commit into from
Apr 3, 2025
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 1.7.2 (2025-04-03)

* fix Landing page links when app is behind proxy

## 1.7.1 (2025-03-04)

* remove `PostgresSettings` initialization from `main.py`
Expand Down
17 changes: 16 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ services:
- "8083:8083"
command: ["uvicorn", "titiler.pgstac.main:app", "--host", "0.0.0.0", "--port", "8083", "--workers", "1"]

nginx:
image: nginx
ports:
- 8080:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- app-nginx
command: [ "nginx-debug", "-g", "daemon off;" ]

app-nginx:
extends:
service: tiler
command: bash -c "uvicorn titiler.pgstac.main:app --host 0.0.0.0 --port 8082 --proxy-headers --forwarded-allow-ips='*' --root-path=/api/v1/titiler"

database:
container_name: stac-db
image: ghcr.io/stac-utils/pgstac:v${PGSTAC_VERSION-0.9.2}
Expand All @@ -77,4 +92,4 @@ services:

networks:
default:
name: stac-fastapi-network
name: titiler-pgstac-network
20 changes: 20 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
events {}

http {
server {
listen 80;

location /api/v1/titiler {
rewrite ^/api/v1/titiler(.*)$ $1 break;
proxy_pass http://app-nginx:8082;
proxy_set_header HOST $http_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;
}
}
}
48 changes: 26 additions & 22 deletions titiler/pgstac/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""TiTiler+PgSTAC FastAPI application."""

import logging
import re
from contextlib import asynccontextmanager
from typing import Dict

Expand Down Expand Up @@ -305,6 +304,25 @@ def ping(
@app.get("/", response_class=HTMLResponse, tags=["Landing"])
def landing(request: Request):
"""Get landing page."""
urlpath = request.url.path
if root_path := request.scope.get("root_path"):
urlpath = urlpath.removeprefix(root_path)

crumbs = []
baseurl = str(request.base_url).rstrip("/")

crumbpath = str(baseurl)
if urlpath == "/":
urlpath = ""

for crumb in urlpath.split("/"):
crumbpath = crumbpath.rstrip("/")
part = crumb
if part is None or part == "":
part = "Home"
crumbpath += f"/{crumb}"
crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()})

data = {
"title": "TiTiler-PgSTACr",
"links": [
Expand All @@ -328,13 +346,14 @@ def landing(request: Request):
},
{
"title": "PgSTAC Virtual Mosaic list (JSON)",
"href": app.url_path_for("list_searches"),
"href": baseurl + app.url_path_for("list_searches"),
"type": "application/json",
"rel": "data",
},
{
"title": "PgSTAC Virtual Mosaic viewer (template URL)",
"href": app.url_path_for(
"href": baseurl
+ app.url_path_for(
"map_viewer",
search_id="{search_id}",
tileMatrixSetId="{tileMatrixSetId}",
Expand All @@ -345,7 +364,8 @@ def landing(request: Request):
},
{
"title": "PgSTAC Collection viewer (template URL)",
"href": app.url_path_for(
"href": baseurl
+ app.url_path_for(
"map_viewer",
collection_id="{collection_id}",
tileMatrixSetId="{tileMatrixSetId}",
Expand All @@ -356,7 +376,8 @@ def landing(request: Request):
},
{
"title": "PgSTAC Item viewer (template URL)",
"href": app.url_path_for(
"href": baseurl
+ app.url_path_for(
"map_viewer",
collection_id="{collection_id}",
item_id="{item_id}",
Expand All @@ -381,21 +402,6 @@ def landing(request: Request):
],
}

urlpath = request.url.path
if root_path := request.app.root_path:
urlpath = re.sub(r"^" + root_path, "", urlpath)
crumbs = []
baseurl = str(request.base_url).rstrip("/")

crumbpath = str(baseurl)
for crumb in urlpath.split("/"):
crumbpath = crumbpath.rstrip("/")
part = crumb
if part is None or part == "":
part = "Home"
crumbpath += f"/{crumb}"
crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()})

return templates.TemplateResponse(
request,
name="index.html",
Expand All @@ -410,7 +416,5 @@ def landing(request: Request):
"crumbs": crumbs,
"url": str(request.url),
"baseurl": baseurl,
"urlpath": str(request.url.path),
"urlparams": str(request.url.query),
},
)
Loading