Skip to content

fix: correct adjacent server URL rewriting when behind a reverse proxy/load balancer#989

Open
sujen1412 wants to merge 2 commits into
NASA-AMMOS:developmentfrom
sujen1412:fix/adjacent-server-proxy-url-rewrite
Open

fix: correct adjacent server URL rewriting when behind a reverse proxy/load balancer#989
sujen1412 wants to merge 2 commits into
NASA-AMMOS:developmentfrom
sujen1412:fix/adjacent-server-proxy-url-rewrite

Conversation

@sujen1412
Copy link
Copy Markdown

Summary

Fixes #753

When MMGIS is deployed behind a load balancer that terminates SSL (e.g. AWS ALB), adjacent services like stac-fastapi receive proxied requests where changeOrigin: true rewrites the Host header to the internal Docker service name (e.g. stac-fastapi). The upstream service then builds self-referencing URLs using that internal hostname.

The existing replaceAll in createSwaggerInterceptor only matched the explicit http://service:port target string — but when the upstream service also picks up X-Forwarded-Proto: https from the ALB (passed through unchanged by the proxy), it generates URLs like https://stac-fastapi/collections instead of http://stac-fastapi:8881/collections. This variant was never matched, so internal hostnames leaked through to clients.

Changes

  • proxyReq handler on the STAC proxy: forwards X-Forwarded-Host (the real public hostname) and X-Forwarded-Proto to the upstream service, so it can natively build correct public URLs when configured with --proxy-headers
  • Broadened URL replacement in createSwaggerInterceptor: extracts the service hostname from the target URL and replaces all variants (http://, https://, with and without port) as a reliable fallback for any upstream service

Testing

Verified on a deployment behind an AWS ALB with stac-fastapi-pgstac. Before the fix, STAC catalog responses contained https://stac-fastapi/collections. After the fix, responses correctly contain https://<public-domain>/stac/collections.

For stac-fastapi, also add the following flags to the uvicorn command so it trusts the forwarded headers:

--proxy-headers --forwarded-allow-ips='*' --root-path /stac

…y/load balancer

When MMGIS is deployed behind a load balancer (e.g. AWS ALB) that terminates
SSL, adjacent services like stac-fastapi receive requests with changeOrigin
rewriting the Host header to the internal Docker service name (e.g.
stac-fastapi). The service then builds self-referencing URLs using that internal
hostname, and the existing replaceAll in createSwaggerInterceptor only matched
the explicit http://service:port target string — missing variants like
https://stac-fastapi/ produced when the service picks up X-Forwarded-Proto from
the ALB.

- Add proxyReq handler to the STAC proxy that forwards X-Forwarded-Host and
  X-Forwarded-Proto to the upstream service, so it can build correct public URLs
- Broaden createSwaggerInterceptor URL replacement to catch all hostname variants
  (http/https, with or without port) using the service hostname extracted from
  the target URL

Fixes NASA-AMMOS#753
Comment thread adjacent-servers/adjacent-servers-proxy.js Outdated
Comment thread adjacent-servers/adjacent-servers-proxy.js
- Fix greedy hostname replacement bug: add port-qualified patterns before
  bare hostname patterns so https://service:port/path is fully replaced
  before the bare https://service pattern can partially match it
- Refactor replacement block to use a publicBase variable to reduce repetition
- Extend proxyReq X-Forwarded-Host/Proto forwarding to tipg, titiler, and
  titiler-pgstac proxies (not just stac)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Adjacent servers using internal Docker container name instead of server name in URLs

2 participants