You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
make RealIP middleware compatible with Cloudflare/CDN setups
change header priority to: X-Real-IP → CF-Connecting-IP → X-Forwarded-For (leftmost) → RemoteAddr. only accept public IPs from headers, skip private/loopback/link-local. this fixes issue where Cloudflare edge server IPs were returned instead of actual client IPs.
Related to #40
Copy file name to clipboardExpand all lines: README.md
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -157,7 +157,14 @@ Compresses response with gzip.
157
157
158
158
### RealIP middleware
159
159
160
-
RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Forwarded-For or X-Real-IP headers.
160
+
RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing various headers that contain the client's real IP address. It checks headers in the following priority order:
161
+
162
+
1.`X-Real-IP` - trusted proxy (nginx/reproxy) sets this to actual client
163
+
2.`CF-Connecting-IP` - Cloudflare's header for original client
164
+
3.`X-Forwarded-For` - leftmost public IP (original client in CDN/proxy chain)
165
+
4.`RemoteAddr` - fallback for direct connections
166
+
167
+
Only public IPs are accepted from headers; private/loopback/link-local IPs are skipped. This makes the middleware compatible with CDN setups like Cloudflare where the leftmost IP in `X-Forwarded-For` is the actual client.
0 commit comments