Laravel provides $request->ip() for a selected client address and $request->ips() for an address list. Behind a load balancer or reverse proxy, the result depends on which peers and forwarding headers the application trusts. Reading a header directly or enabling broad proxy trust without a controlled ingress path can let a visitor influence the value.

Before changing code, identify the installed Laravel version and the exact network route to the application. In a standard Laravel 13 application, trusted-proxy configuration belongs in bootstrap/app.php; older application structures may place it elsewhere. Follow the documentation for the installed release and test the actual deployment. An IP address is useful for diagnosis, but it is not user authentication or a precise location.

Check the Installed Version and Request Path

Run composer show laravel/framework in the application directory to check the installed framework version. Then map every route to the PHP application, including any CDN, edge proxy, cloud load balancer, or web server in the path. Include health checks, private ingress, and any direct origin route. Record the addresses of the peers that can connect to the application and the headers they set.

Identify which proxy is responsible for recording the connecting client. A public client can add a Forwarded or X-Forwarded-For field to its own request, so the first controlled proxy must handle that input according to your forwarding policy. Overwriting an untrusted value avoids passing an invented chain inward. If proxies append addresses instead, distinguish the addresses observed by trusted proxies from earlier claims supplied by an untrusted client.

Configure Trusted Proxies and Headers

In Laravel 13, call trustProxies within the existing withMiddleware callback in bootstrap/app.php. The at argument identifies trusted proxy addresses or ranges; headers selects the forwarding fields to accept. For example, the following code trusts one proxy for X-Forwarded-For:

$middleware->trustProxies(
    at: ['192.0.2.20'],
    headers: \Illuminate\Http\Request::HEADER_X_FORWARDED_FOR,
);

The address 192.0.2.20 is reserved for documentation; replace it with the actual proxy address seen by the application. Add the call to the existing callback rather than replacing the application's bootstrap configuration. This narrow example enables only client-address forwarding; it does not enable forwarded host, scheme, or port information.

Choose additional header constants only when your proxy supplies those values reliably. The AWS load-balancer option is Request::HEADER_X_FORWARDED_AWS_ELB, while the standard Forwarded header uses Request::HEADER_FORWARDED. These shorter names assume that Illuminate\Http\Request is imported as Request. Protect the connection from the trusted proxy to the application so another system cannot impersonate the proxy or alter its headers in transit.

Prefer an explicit, current trusted-peer list when the proxy addresses are known. The wildcard at: '*' accepts proxies without listing their addresses, but requires strict control over which systems can reach the app. If a direct route to the app exists, trusting every apparent proxy can let a client-provided header affect the computed address. Restrict ingress at the network layer when broad trust is operationally necessary, and verify that only the intended balancer can reach the application.

Check the configuration mechanism before applying a Laravel 13 example to an older codebase. A misplaced setting may have no effect, and invalid bootstrap code can prevent requests from being handled at all. An overly broad trust setting can let an untrusted request supply the selected address. Review changes to host, scheme, and port forwarding at the same time because they also affect generated URLs and security decisions.

Read the Selected Address and Address List

In a controller method or route closure, type-hint Illuminate\Http\Request to receive the current request object. Use $clientIp = $request->ip(); for the selected address. For diagnosis, also inspect $request->ips() and $request->server('REMOTE_ADDR') in a restricted test environment.

When the connection peer is not trusted, the address methods use the server's connection address. If accepted forwarding information includes addresses outside the trusted proxy list, ips() removes the trusted proxy addresses and orders the remaining values from the nearest untrusted address toward earlier reported addresses. The ip() method selects the first result; it does not simply choose the leftmost value in the raw header.

For example, suppose the peer is 192.0.2.20 and X-Forwarded-For contains 203.0.113.44, 198.51.100.9. If only 192.0.2.20 is trusted, ips() returns 198.51.100.9 followed by 203.0.113.44, and ip() selects 198.51.100.9. Trusting 198.51.100.9 as an additional controlled proxy would remove that address from the list, leaving 203.0.113.44.

Verify whether the web server has already rewritten REMOTE_ADDR before treating it as the original connection peer. If it has, preserve the actual peer in server logs and coordinate the server and Laravel settings. Do not interpret the last entry in ips() as independently verified just because it appears to be the original client.

Never select a supposed visitor address by blindly taking the first comma-separated header value. The forwarded chain and trusted peer configuration determine which value is credible. The standard Forwarded format does not authenticate the source of a value. Vendor-specific headers and X-Forwarded-For also need an explicitly defined trust policy.

Test Direct and Alternate Routes

Send a request through the normal proxy path from a controlled test client. Check the selected ip() value against the expected source and confirm the connection peer is a trusted proxy. Repeat through every alternate route, because a CDN path, health check, and private path may have different hop counts or headers.

In an authorized test environment, send deliberately false forwarding values both directly and through the proxy. On the proxied route, confirm that the controlled proxy records the actual connecting address instead of accepting the forged value as the visitor. Verify that the application cannot be reached directly when network policy forbids it, or that it ignores the false value when direct access is allowed. A successful ordinary request does not prove spoof resistance. Record the installed framework version, proxy configuration, ingress restriction, test path, and observed results.

If an IP value feeds rate limiting, allowlists, or audit decisions, review those consumers after any trust change. A mistaken trust boundary can let the wrong address through all of them. Keep diagnostic logs limited to what operations require, and remove personal address data from public examples.

Recheck Proxy Settings after Deployment Changes

Update the trusted address list and tests when load balancers, CDN ranges, or application ingress change. Confirm that the proxy still sanitizes forwarding headers after upgrades. Keep the expected test results with the configuration so a later change can be checked against the same request paths.