Skip to content
FastAPI/Uvicorn: trusted proxy configuration for client IP rate limiting
bunyaminsonmezknb-jpg
OP

a month ago

Hello Railway team,

Our FastAPI backend at api.endstp.com uses Uvicorn 0.24.0. Rate limiting reads request.client.host.

For a normal browser request, we observed:

  • ASGI request.client.host: 100.64.0.2
  • Leftmost X-Forwarded-For value: 78.168.163.183

The X-Forwarded-For value has not yet been independently verified as the browser’s public IP. FORWARDED_ALLOW_IPS is not defined in the container shell, and no proxy-related flags were found in the Uvicorn process arguments.

Could you please clarify:

  1. Does Railway overwrite client-supplied X-Forwarded-For and X-Real-IP headers, or preserve them and append the detected client IP? Which value should the application trust?
  2. Are connections reaching the backend guaranteed to come through Railway-controlled proxies, with no external client able to connect directly to the container?
  3. What FORWARDED_ALLOW_IPS configuration do you recommend for Uvicorn? Is there a documented, stable proxy address or range?

We do not want to trust "*" or permanently trust 100.64.0.2 based on one observation. Please share the official documentation or supported configuration.

Thank you.

Solved

7 Replies

Railway
BOT

a month ago

Our HTTP edge overwrites X-Real-IP, X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host on every request it forwards. Any copies the client sends are replaced, not kept or appended to. For traffic arriving through the public edge, X-Real-IP is the value to trust as the client address. The one exception: when the request reaches us from Cloudflare's published IPv4 ranges with CF-Connecting-IP set, X-Real-IP carries that visitor IP instead.

Public traffic always goes through the edge, but that isn't the only way to reach your listener. Other services in the same project and environment can connect to its port directly over private networking, and those requests skip the edge's header sanitization, so a private caller can send its own forwarding headers. A TCP proxy on the service would also bypass the edge, and it passes raw bytes with no forwarding headers added.

We don't publish a stable range for the proxy address your container sees as its socket peer. The 100.64.0.0/10 address you observed is not a supported contract and can change, so don't hard-code it in FORWARDED_ALLOW_IPS. Trusting * assumes every path to the listener is trusted, including other services in that environment. A safer option for rate limiting is to read X-Real-IP directly for edge traffic and not rely on Uvicorn's proxy-header rewriting.

Header meanings are in our public networking specs, and private network isolation is covered here.


Status changed to Awaiting User Response Railway • about 1 month ago


Railway

Our HTTP edge overwrites `X-Real-IP`, `X-Forwarded-For`, `X-Forwarded-Proto` and `X-Forwarded-Host` on every request it forwards. Any copies the client sends are replaced, not kept or appended to. For traffic arriving through the public edge, `X-Real-IP` is the value to trust as the client address. The one exception: when the request reaches us from Cloudflare's published IPv4 ranges with `CF-Connecting-IP` set, `X-Real-IP` carries that visitor IP instead. Public traffic always goes through the edge, but that isn't the only way to reach your listener. Other services in the same project and environment can connect to its port directly over private networking, and those requests skip the edge's header sanitization, so a private caller can send its own forwarding headers. A TCP proxy on the service would also bypass the edge, and it passes raw bytes with no forwarding headers added. We don't publish a stable range for the proxy address your container sees as its socket peer. The `100.64.0.0/10` address you observed is not a supported contract and can change, so don't hard-code it in `FORWARDED_ALLOW_IPS`. Trusting `*` assumes every path to the listener is trusted, including other services in that environment. A safer option for rate limiting is to read `X-Real-IP` directly for edge traffic and not rely on Uvicorn's proxy-header rewriting. Header meanings are in our [public networking specs](https://docs.railway.com/networking/public-networking/specs-and-limits#technical-specifications), and private network isolation is covered [here](https://docs.railway.com/networking/private-networking/how-it-works).

bunyaminsonmezknb-jpg
OP

a month ago

Thank you. Before changing our production rate limiting, could a Railway team member confirm the header-overwrite behavior described in this automated response and provide the exact documentation links?

One implementation question remains: how can our backend reliably distinguish a request forwarded by Railway’s public HTTP edge from a direct private-network request, given that the edge’s socket peer addresses have no supported stable range?

We need to know when X-Real-IP can safely be trusted without allowing a direct caller to supply an arbitrary client IP.


Status changed to Awaiting Railway Response Railway • about 1 month ago


a month ago

The above is correct.


Status changed to Awaiting User Response Railway • about 1 month ago


bunyaminsonmezknb-jpg
OP

a month ago

Thanks for confirming. Could you also clarify the remaining implementation question: how can our backend reliably distinguish public-edge requests from direct private-network requests before trusting X-Real-IP? If there is no supported way to distinguish them, what configuration or network restriction do you recommend for secure client-IP rate limiting?


Status changed to Awaiting Railway Response Railway • about 1 month ago


Railway
BOT

a month ago

There isn't a supported way for your backend to tell an edge-forwarded request apart from a direct private-network request by inspecting the request itself. The socket peer address has no stable range, and a private caller can send any headers it likes, including its own X-Real-IP. We don't offer a signed header, mTLS identity, or edge-only ingress setting that would prove a request came through the edge.

What decides whether X-Real-IP is safe is which paths can reach the listener. Private networks are isolated per project and environment, so only services in the same project and environment as your API can reach its port privately. If no other service in that environment calls the API over private networking, and the API service has no TCP proxy, then every request it receives came through the public HTTP edge, and X-Real-IP on those requests is set by us and cannot be forged by the caller.

For that setup, reading X-Real-IP directly for rate limiting is the safer choice than turning on Uvicorn's proxy-header rewriting with FORWARDED_ALLOW_IPS set to * or to an observed proxy address. If other services in that environment do call the API privately, they sit inside the same trust boundary, so any forwarding header they send is their own assertion rather than ours.

References: public networking specs and private networking isolation.


Status changed to Awaiting User Response Railway • about 1 month ago


Railway

There isn't a supported way for your backend to tell an edge-forwarded request apart from a direct private-network request by inspecting the request itself. The socket peer address has no stable range, and a private caller can send any headers it likes, including its own X-Real-IP. We don't offer a signed header, mTLS identity, or edge-only ingress setting that would prove a request came through the edge. What decides whether X-Real-IP is safe is which paths can reach the listener. Private networks are isolated per project and environment, so only services in the same project and environment as your API can reach its port privately. If no other service in that environment calls the API over private networking, and the API service has no TCP proxy, then every request it receives came through the public HTTP edge, and X-Real-IP on those requests is set by us and cannot be forged by the caller. For that setup, reading X-Real-IP directly for rate limiting is the safer choice than turning on Uvicorn's proxy-header rewriting with FORWARDED_ALLOW_IPS set to * or to an observed proxy address. If other services in that environment do call the API privately, they sit inside the same trust boundary, so any forwarding header they send is their own assertion rather than ours. References: [public networking specs](https://docs.railway.com/networking/public-networking/specs-and-limits#technical-specifications) and [private networking isolation](https://docs.railway.com/networking/private-networking/how-it-works).

bunyaminsonmezknb-jpg
OP

a month ago

Could a Railway team member confirm this follow-up as well?

Our environment contains the API and a Redis service. Before trusting X-Real-IP, we want to clarify whether the requirement is that all services able to reach the API privately must be trusted—not merely that they currently do not call it.

We understand that no request-level mechanism proves edge origin, and that a TCP proxy would create another ingress path. Is this the correct trust model?


Status changed to Awaiting Railway Response Railway • about 1 month ago


a month ago

The above is correct.


Status changed to Awaiting User Response Railway • about 1 month ago


Railway
BOT

a month ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway • about 1 month ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...