title: Drupal 7 and Varnish image cache setting src to 127.0.0.1
body: <p>I have a Drupal 7 installation that is sometimes setting the src path of images to 127.0.0.1.
  Here is an example.</p>
  
  <pre><code>&lt;img height="291" width="233" style="width: 233px; height: 291px; float: left;" class="media-image media-element file-default" typeof="foaf:Image" src="http://127.0.0.1/sites/default/files/media/news/images/jerzy_sawicki.jpg" alt="" title=""&gt;
  </code></pre>
  
  <p>After clearing the cache the image src is correct for awhile.</p>
  
  <pre><code>&lt;img height="291" width="233" style="width: 233px; height: 291px; float: left;" class="media-image media-element file-default" typeof="foaf:Image" src="http://www.example.com/sites/default/files/media/news/images/jerzy_sawicki.jpg" alt="" title=""&gt;
  </code></pre>
  
  <p>I have many contributed modules enabled, but I imagine that this is most likely a Varnish or Cache Expiration issue.</p>
  
  <p>Here is the Varnish default.vcl config.  I had changed 127.0.0.1 to the server name thinking that may effect the src, but it did not.</p>
  
  <pre><code>backend default {
    .host = "www.example.com";
    .port = "8888";
    .connect_timeout = 10s;
    .first_byte_timeout = 10s;
    .between_bytes_timeout = 10s;
    // Check Drupal every 5 minutes to keep cache warm.
    .probe = {
      .url = "/news";
      .interval = 300s;
      .timeout = 10s;
      .window = 5;
      .threshold = 2;
    }
  }
  
  sub vcl_recv {
    // Remove has_js and Google Analytics __* cookies.
    set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
    // Remove a ";" prefix, if present.
    set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
    // Remove empty cookies.
    if (req.http.Cookie ~ "^\s*$") {
      unset req.http.Cookie;
    }
  
    // Catch Drupal theme files – THIS BREAKS UPDATE.PHP
    if (req.url ~ "^/sites/") {
      unset req.http.Cookie;
    }
    // Catch Drupal misc files (like drupal.js and jquery.js)
    if (req.url ~ "^/misc/") {
      unset req.http.Cookie;
    }
  
    // Drupal js/css doesn’t need cookies, cache them
    if (req.url ~ "^/modules/.*\.(js|css)\?") {
      unset req.http.Cookie;
    }
  
    // Pass cron jobs
    if (req.url ~ "cron.php" ||
      req.url ~ "^/admin/structure/features$" ||
      req.url ~ "^/admin/config/system/backup_migrate$") {
      return (pass);
    }
    // Currently we have server-status monitoring going directly against 8888 port
    // Commenting out this pass-through
    //if (req.url ~ ".*/server-status$") {
      //return (pass);
    //}
  
    # Add a unique header containing the client address
    remove req.http.X-Forwarded-For;
    set    req.http.X-Forwarded-For = client.ip;
  }
  
  sub vcl_hash {
    if (req.http.Cookie) {
      set req.hash += req.http.Cookie;
    }
  }
  
  sub vcl_deliver {
    if (obj.hits &gt; 0) {
      set resp.http.X-Cache = "HIT";
    } else {
      set resp.http.X-Cache = "MISS";
    }
  }
  
  sub vcl_fetch {
  
      # Varnish determined the object was not cacheable
      if (!beresp.cacheable) {
          set beresp.http.X-Cacheable = "NO:Not Cacheable";
  
      # You don't wish to cache content for logged in users
      } elsif (req.http.Cookie ~ "(UserID|_session)") {
          set beresp.http.X-Cacheable = "NO:Got Session";
          return(pass);
  
      # You are respecting the Cache-Control=private header from the backend
      } elsif (beresp.http.Cache-Control ~ "private") {
          set beresp.http.X-Cacheable = "NO:Cache-Control=private";
          return(pass);
  
      # You are extending the lifetime of the object artificially
      } elsif (beresp.ttl &lt; 1s) {
          set beresp.ttl   = 5s;
          set beresp.grace = 5s;
          set beresp.http.X-Cacheable = "YES:FORCED";
  
      # Varnish determined the object was cacheable
      } else {
          set beresp.http.X-Cacheable = "YES";
      }
  
      # ....
  
      return(deliver);
  sub vcl_error {
      # If 503 error and we've tried less than 3 times, try again
      if (obj.status == 503 &amp;&amp; req.restarts &lt; 3) {
          restart;
      }
  }
  </code></pre>