Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx - how to configure IP address? (returns only 127.0.0.1)

I have a Rails app on Amazon EC2, with using nginx and unicorn.

When I access request.remote_ip or request.env['REMOTE_ADDR'], the output is always 127.0.0.1.

Here's my nginx.conf

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    upstream example.com {
      server 127.0.0.1:8080;
    }

    server{
      listen 80;

      server_name example.com _;
      root /home/deployer/example/public;

      location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X_FORWARDED_PROTO $scheme;
        proxy_set_header Host $http_host;

        if (!-f $request_filename) {
          proxy_pass http://example.com;
          break;
        }
        if (-f $document_root/system/maintenance.html) {
          return 503;
        }
      }
   }
}

I've tried to add these lines:

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

But unfortunately, the result was still the same - 127.0.0.1

What am I doing wrong? Is there any other bug?

like image 516
user984621 Avatar asked Oct 20 '22 23:10

user984621


1 Answers

Try this :

proxy_set_header CLIENT_IP $remote_addr;
like image 160
Jeremy Green Avatar answered Oct 24 '22 10:10

Jeremy Green