WebFaction
Community site: login faq
0
1

We need to log the user IP's on our site, but all IP's are logged as localhost. I've added real_ip_header as stated here: http://community.webfaction.com/questions/15070/all-requests-to-nginx-coming-from-localhost

Solved: Unfortunately after restarting nginx I get this error: nginx: [emerg] unknown directive "real_ip_header"

EDIT: Recompiled nginx with correct module which solves the first issue.

Error: With correct modules installed, the site is still not logging the correct IP address for the user, it's still only logging localhost.

This is how I'm getting the client IP in my Django Middleware:

user_activity.remote_addr = request.META.get('REMOTE_ADDR')

Here is my nginx config (Note this is a template, hence the ${} variables, so just ignore those):

server {
    server_name ${opts:server_name};
    listen ${opts:listen_port};
    access_log  ${logs:access_log};

    # SSL Configuration not required since this is already handled by the
    # host nginx proxy. Keeping these here in case this changes in future.
    #
    # ssl on;
    # ssl_certificate ${ssl_certificate};
    # ssl_certificate_key ${ssl_certificate_key};

    location ^~ /media/ {
        root ${opts:media_dir};
        expires 31d;
    }

    location ^~ /static/ {
        root ${opts:media_dir};
        expires 31d;
    }

    # Enable if you want to use Gunicorn
    location / {
        proxy_pass http://unix:${opts:socketfile}:;
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        send_timeout 600;

        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Protocol ssl;
        real_ip_header X-Forwarded-For;
    }
}

asked 11 Nov '13, 05:39

andre
1129
accept rate: 0%

edited 11 Nov '13, 12:14


Have you compiled nginx with the realip module enabled?

If not, you might need to recompile the server with it enabled, before being able to use it in your config file.

permanent link

answered 11 Nov '13, 06:32

iliasr ♦♦
2.1k14
accept rate: 35%

Recompile and now I'm not getting that error. The IP however that my website is logging is still localhost however.

(11 Nov '13, 07:13) andre

In which log are you seeing localhost? Nginx, or gunicorn?

(11 Nov '13, 12:28) seanf

@seanf it's in django (so I guess gunicorn?). I'm logging the request.META['REMOTE_ADDR'] directly in the database.

(11 Nov '13, 18:22) andre

Hmm, ok. Have you tried setting set_real_ip_from in your nginx config? I think you may need to set it to 127.0.0.1.

(12 Nov '13, 12:05) seanf

@seanf that did indeed solve the problem :) thx!

(12 Nov '13, 16:09) andre
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×186
×25
×11

question asked: 11 Nov '13, 05:39

question was seen: 6,630 times

last updated: 12 Nov '13, 16:09

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM