Wrong IP-Address with nginx + Unicorn + rails

nginx, ruby-on-rails-3, unicorn

Solution

I had trouble with this too; I found this question, but the other answer didn't help me.

I looked at Rails 3.2.8's implementation of Rack::Request#ip to see how it decided what to say; to get it to use an address passed via the environment without filtering out addresses from my local network (it's trying to filter out intermediate proxies, but that's not what I wanted), I had to set the HTTP_CLIENT_IP from my nginx proxy configuration block in addition to what you've got above (X-Forwarded-For has to be there too for this to work!):

proxy_set_header CLIENT_IP $remote_addr;

Problem

I check the ip-address in the controller with ``` request.env['REMOTE_ADDR'] ``` this works fine in my test environment. But on the production server with nginx + unicorn I always get `127.0.0.1`. This is my nginx config for the site: ``` upstream unicorn { server unix:/tmp/unicorn.urlshorter.sock fail_timeout=0; } server { listen 80 default deferred; # server_name example.com; root /home/deployer/apps/urlshorter/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } try_files $uri/index.html $uri @unicorn; location @unicorn { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; } ```

Original source

Related problems