2

How do I forward the real visitors ip adress to Unicorn? The current setup is:

Haproxy => Nginx => Unicorn
  1. How can I forward the real IP address from Haproxy, to Nginx, to Unicorn? Currently it is always only 127.0.0.1
  2. I read that the X headers are going to be depreceated. https://www.rfc-editor.org/rfc/rfc6648 - how will this impact us?

Haproxy Config:

# haproxy config
defaults
    log global
    mode    http
    option  httplog
    option  dontlognull
    option httpclose
    retries 3
    option redispatch
    maxconn 2000
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000

# Rails Backend
backend deployer-production
    reqrep    ^([^\ ]*)\ /api/(.*)  \1\ /\2
    balance     roundrobin
    server      deployer-production localhost:9000 check

Nginx Config:

upstream unicorn-production {
  server unix:/tmp/unicorn.ordify-backend-production.sock fail_timeout=0;
}

server {
  listen 9000 default;
  server_name manager.ordify.localhost;
  root /home/deployer/apps/ordify-backend-production/current/public;
  access_log /var/log/nginx/ordify-backend-production_access.log;
  rewrite_log on;

  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_pass http://unicorn-production;

    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
Hendrik
  • 251
  • 2
  • 4
  • 11
  • 1
    Have you checked X-Real-IP header on Unicorn side? Is it always 127.0.0.1 too? – Alex Jul 11 '12 at 11:30
  • In the case if you use TCP mode, there is a bit complicated configuration. Full example : [How to forward client's IP address to Nginx from Haproxy in tcp mode](https://serverfault.com/questions/867864/how-to-forward-clients-ip-address-to-nginx-from-haproxy-in-tcp-mode/880430#880430) – Tomas Pytel Oct 26 '17 at 13:44

1 Answers1

7

You should add option forwardfor to Haproxy config and configure nginx realip module: http://nginx.org/en/docs/http/ngx_http_realip_module.html

VBart
  • 8,309
  • 3
  • 25
  • 26
  • Thanks. Using the forwardfor option and the realip module solved the problem. IPs are now appearing. – Hendrik Jul 12 '12 at 12:20