Code

Opened 8 years ago

Closed 8 years ago

Last modified 3 years ago

#2552 closed enhancement (fixed)

[contrib] get IP from X-Forwarded-For

Reported by: ian@… Owned by: adrian
Component: Metasystem Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

SmileyChris suggest this would be contrib worthy.

from myblog

on some shared hosting environments like webfaction they install a reverse proxy solution where you run a apache2 webserver on a custom port, and the main webserver forwards the request through to you.

This works great, and I’ve been using things like this for years, but the problem you get is that all the requests appear to be from 127.0.0.1 (or the machine which is doing the proxying for you) which isn’t the best if you want to record that stuff.

webfaction fixes this in the log files for you automatically, but that doesn’t help you if you want to do something with the IP# in django.

enter FixIP it takes the X-Forwarded-For header sent by compliant servers which holds the real IP and makes django work properly.

installation is easy.. just add the routine to your middleware settings ala

MIDDLEWARE_CLASSES = (
    "django.middleware.common.CommonMiddleware",
    "zilbo.common.utils.middleware.fixip.FixIP",
   ...

code is available here

Attachments (0)

Change History (4)

comment:1 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [3602]) Fixed #2552 -- Added SetRemoteAddrFromForwardedFor middleware and documentation. Thanks, Ian Holsman

comment:2 Changed 8 years ago by adrian

For the record, I put this in django/middleware/http.py instead of in django/contrib because we don't have the precedent of middleware in django/contrib.

comment:3 Changed 3 years ago by Oliver

  • Easy pickings unset
  • UI/UX unset

just for the one who drop here from a google request, the fix saying it's in middleware/http.py means that you have to use the following syntax to use this middleware (which fix the IP properly using HTTP header X-Forwarded-For if present) :

 MIDDLEWARE_CLASSES = (
  ...
  'django.middleware.http.FixIP',
 )

comment:4 Changed 3 years ago by Oliver

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.