Why an App Generates the Wrong URL Behind a Reverse Proxy

Diagnose wrong URLs behind a reverse proxy, from HTTP redirects and callback mismatches to trusted headers, public hosts, and path prefixes.

Published
Reading time
7 min read
Why an App Generates the Wrong URL Behind a Reverse Proxy
On this page

An application can generate an http:// link, an internal hostname, or a callback URL missing its public path when it runs behind a reverse proxy. The browser sees the public URL, but the application may see only the proxy’s connection. This guide shows how to locate the mismatch and decide whether to fix proxy metadata, application configuration, or both.

Key Takeaways

  • HTTPS can terminate at the proxy while the application receives HTTP; without trusted external-scheme information, it may generate HTTP URLs.
  • The public host, port, and path prefix can also differ from what the application sees.
  • Forwarded headers are inputs from the network, not automatically trustworthy facts. Accept them only from explicitly trusted proxies.
  • A configured public base URL is often preferable for links generated outside a request, such as email links and scheduled jobs.
  • Test redirects and authorization callbacks through the public endpoint, not only by connecting directly to the application.

Why the wrong URL appears behind a reverse proxy

Consider this fictional deployment:

Browser:  https://app.example.test/portal/login
                    |
Proxy:    terminates HTTPS and removes /portal
                    |
App:      http://app-internal:8080/login

If the application constructs an absolute URL from its immediate connection, it might produce http://app-internal:8080/login instead of the public URL. Three pieces of information may need correction:

ComponentWhat can differTypical remedy
SchemeThe browser uses HTTPS, but the proxy connects to the app over HTTP.Configure the app to use trusted forwarded scheme information or a public base URL.
Host and portThe app sees an internal host or port rather than the public authority.Preserve or securely convey the public host and port; configure allowed hosts or a canonical origin.
PathThe proxy strips or adds a prefix such as /portal.Configure the app’s supported base path and align it with the proxy rewrite.

A public base URL is the external origin and, where appropriate, the path prefix used for generated links. Exact settings differ among frameworks. In particular, do not assume that a framework understands a path-prefix header: some require an explicit base-path setting or a specific integration.

Advertisement

Diagnose the mismatch

Use a staging deployment or another environment you are authorized to inspect. Replace the example hostname and route below with a public endpoint that actually produces a redirect. Do not include real credentials or authorization codes in shared logs.

1. Inspect the public response

The following request prints response headers without following redirects:

curl -sS -D - -o /dev/null 'https://app.example.test/portal/login'

For a redirecting endpoint, inspect the status and Location header. For example, this would be suspicious if the public site uses HTTPS:

Location: http://app.example.test/portal/dashboard

A Location value can also be relative, such as /portal/dashboard; in that case the browser resolves it against the request URL. Check whether it resolves to the intended public path. A non-redirecting response may not have a Location header, so choose a route that redirects in your deployment.

For an authorization flow, inspect the outgoing authorization request in the browser’s developer tools and check its redirect_uri parameter. Compare it with the callback URL registered with the identity provider. Treat the trace as sensitive: it can contain tokens, codes, state values, or session information.

2. Compare public and application-side views

Record the intended public scheme, host, port, and path prefix. Then inspect sanitized proxy and application diagnostics for the same request:

  • What scheme, host, port, and path does the application believe it received?
  • Which forwarding metadata reached it, and what is the direct network peer?
  • Did the proxy strip or add a path prefix before forwarding?
  • Does the generated URL come from request metadata or from a configured public base URL?

If the public response is wrong but the application’s view already matches the public URL, look at the URL-generation code and explicit base-URL settings. If the application’s view is internal, investigate forwarding and trusted-proxy settings. If only the path is wrong, inspect both the proxy rewrite and the app’s base-path configuration.

Do not indiscriminately log complete headers or query strings: cookies, authorization headers, and callback parameters can contain secrets. Restrict access to diagnostics and redact sensitive values.

Email links, background jobs, and scheduled tasks may have no incoming request from which to infer an origin. Configure the public base URL through the application’s supported settings, then generate a sample link in a non-production environment. If the app serves multiple public domains, use a controlled mapping for the relevant tenant rather than deriving a domain from arbitrary input.

Advertisement

Fix forwarding and proxy trust

A proxy can convey the original request context using Forwarded or deployment-specific headers such as X-Forwarded-Proto and X-Forwarded-Host. These values only help if the application is configured to interpret the headers its proxy actually sends. Port handling and precedence between header formats also vary by framework and configuration.

At the public edge, configure the proxy to remove or replace client-supplied forwarding headers with values it establishes itself. Configure the application to trust forwarded information only when it comes through the specific proxy infrastructure you control. For a multi-hop chain, verify how each hop constructs the chain and how the framework selects trusted values; trusting the wrong number or range of hops can make attacker-supplied values appear authoritative.

Also restrict direct access to the application where practical. Otherwise, a client that bypasses the proxy might send its own forwarding headers. Use host validation or a fixed canonical origin as appropriate: blindly trusting a forwarded host can enable host-header poisoning, including malicious password-reset links or redirects.

For paths, make the proxy’s rewrite and the application’s base-path setting agree. If /portal is stripped before forwarding, the app still needs a supported way to generate public URLs containing /portal. Avoid configuring the prefix in two places that both add it, which can produce /portal/portal/....

Implementation note: Proxy and framework documentation could not be retrieved for this article. Before changing configuration, consult the documentation for your exact versions, particularly its trusted-proxy, forwarded-header, allowed-host, and base-path behavior. The commands above illustrate diagnosis, not framework or proxy configuration, and have not been executed here.

Verify the fix

Repeat the public request and confirm that the Location header uses the intended HTTPS origin and path. Exercise the full login or authorization flow in a safe environment and confirm that the emitted callback exactly matches the registered callback, including scheme, host, port, and path. Check a background-generated link separately.

As an authorized negative test, send a request through the public edge with a deliberately incorrect forwarding value and verify that it cannot change the generated public URL. For example, on a staging endpoint that redirects:

curl -sS -D - -o /dev/null \
  -H 'X-Forwarded-Proto: http' \
  'https://app.example.test/portal/login'

If the intended public scheme is HTTPS, a generated absolute redirect should not become HTTP merely because the client supplied that header. This single test does not prove the whole proxy chain is safe: also review host handling, direct application access, other accepted header formats, and every trusted hop.

Common Mistakes

  • Enabling proxy trust for every client. A forwarded value is not trustworthy just because it has a familiar header name.
  • Fixing only the scheme. The generated host, port, or path may still be internal or incorrect.
  • Testing only the backend port. That bypasses the public proxy behavior responsible for the discrepancy.
  • Assuming relative redirects solve every case. They can avoid an incorrect origin, but path resolution still matters, and email links and many callback registrations require absolute URLs.
  • Using an unvalidated return URL. Correct proxy headers do not prevent an open redirect; validate user-supplied destinations independently.

Best Practices

Keep the public edge responsible for sanitizing forwarding metadata, minimize the set of trusted proxy addresses, and document the expected header chain. Prefer a configured canonical public URL where request-derived origins are unnecessary. Add regression checks for redirects, callbacks, path prefixes, and background-generated links whenever the proxy topology changes.

Conclusion

A wrong URL behind a reverse proxy usually reflects a mismatch between the public request and the context the application uses to generate links. Compare those views, correct the relevant scheme, host, or path configuration, and verify that untrusted clients cannot supply the values on which URL generation depends.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.