Support X-Forward-For / X-Real-IP


(Nathan Rijksen) #1

I have Discourse running through an nginx proxy and unfortunately the IP’s for all my users is 127.0.0.1, even though the X-Real-IP header is set. It would be nice if Discourse could pick up on this and properly set the users IP.


(David Celis) #2

I’m also using Nginx to proxy for SSL purposes (to a Docker container) but my users get their IP addresses set correctly… What’s your nginx configuration look like?

EDIT: Not sure how helpful it’ll be, but here’s mine if you wanna look:

server {
  listen 80 default deferred;
  server_name forums.example.com;
  rewrite ^/(.*) https://forums.example.com/$1 permanent;

  location / {
    access_log off;
    proxy_pass http://forums.example.com:8080; # Docker exposes port 8080
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

server {
  listen 443 ssl spdy;
  server_name forums.example.com;

  ssl_certificate /etc/ssl/forums-unified.crt;
  ssl_certificate_key /etc/ssl/forums.key;

  location / {
    access_log off;
    proxy_pass http://forums.example.com:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

Error: listen tcp 0.0.0.0:80: bind: address already in use
(Nathan Rijksen) #3

Very basic (still in the process of setting up).

server {
        listen 80;

        server_name foo.bar;

        location / {
                proxy_pass      http://0.0.0.0:8080;
                proxy_set_header        X-Real-IP       $remote_addr;
        }
}

Another issue I’ve just noticed is that using eg. google to authenticate it tries to redirect to http://0.0.0.0:8080/..., I assume my config needs some work. Would love to see what works for you?


(David Celis) #4

I amended my post up there with my configuration. I think you need both the X-Forwarded-For header and the X-Real-IP header.


(Nathan Rijksen) #5

Yup that seems to have done the trick! And the host header solved the other issue I was having. Thanks for sharing!


(Jacob) #6

Not sure what I’m doing wrong, but my file looks the same as this and I am still seeing my DigitalOcean IP instead of user IPs. Any way to fix this?


(Lenin Raj Rajasekaran) #7

It worked for me when I removed that above rewrite line alone.


(Jacob) #8

I’m still having this problem. Is there a definitive nginx config file for outside of the docker container?