varnish+nginx as backend and real IP

by

in

With default config nginx all IP addresses shows as localhost.
Here is configuration to show real client IP.

Snippet from varnish config:

    sub vcl_pipe {
        set bereq.http.connection = "close";
        if (req.http.X-Forwarded-For) {
                set bereq.http.X-Forwarded-For = req.http.X-Forwarded-For;
        } else {
                set bereq.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
          }
    }
    sub vcl_pass {
        set bereq.http.connection = "close";
        if (req.http.X-Forwarded-For) {
                set bereq.http.X-Forwarded-For = req.http.X-Forwarded-For;
        } else {
          set bereq.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
          }
    }

Snippet from nginx config:

set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;

Comments

7 responses to “varnish+nginx as backend and real IP”

  1. great !

    it ‘s work perfectly

    thks a lot

  2. Thanks. Your method worked even when the other methods have failed to get the real ip.

  3. works 100% thx!!1!

  4. Where could I put these?

    set_real_ip_from 127.0.0.1;
    real_ip_header X-Real-ip;

    1. it may be entered in three places according to Your configuration:
      http, server or location

      more info:
      http://wiki.nginx.org/HttpRealipModule

  5. For getting client ip in application like PHP, ASP visit getting-real-client-ip-through-varnish/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.