# Proxy\_cache not serving file from edge server!

**URL:** <https://www.ruby-forum.com/t/proxy-cache-not-serving-file-from-edge-server/230717>\
**Category:** NGINX\
**Created:** [July 1, 2014, 1:58pm UTC](https://www.ruby-forum.com/t/proxy-cache-not-serving-file-from-edge-server/230717 "2014-07-01T13:58:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [July 1, 2014, 1:58pm UTC](https://www.ruby-forum.com/t/proxy-cache-not-serving-file-from-edge-server/230717/1 "2014-07-01T13:58:01Z")

</div>

We’ve an origin and edge server with nginx-1.6 . Origin  
web-server(Located  
in U.S) is configured with nginx\_geo\_module and edge(Local ISP) is  
configured with proxy\_cache in order to cache files from origin server  
and  
serve from their lately. We’re using following method for caching with  
proxy\_cache :-

1. client (1.1.1.1) sends mp4 request to origin webserver and geo\_module  
in  
origin checks, if the ip is 1.1.1.1 then pass that client to the edge  
server using proxy\_pass.

2. Edge, checks if the file is in proxy\_cache than it should serve the  
file  
locally and if file is not in proxy\_cache, it’ll pass back the request  
to  
origin server and client will be served from origin server as well as  
requested file will also be cached in local server, so next time the  
edge  
will not have to pass request again to origin server and serve the same  
file via locally.

But, looks like our caching is not working as expected. Our ISP is  
complaining that, whenever edge server serves the file, instead of  
serving  
that file to local client (1.1.1.1) it serves the file back to origin  
server(U.S) and all outgoing bandwidth is going back to U.S instead of  
local clients (Offcourse bandwidth not being saved).

So i want to ask, if the origin server is passing request to edge  
server,  
the cached file must be served locally but the request going back to the  
origin server even the cache status: HIT. Following are my configs :-

ORIGIN :-

geo $TW {  
default 0;  
1.1.1.1 1;

}

server {  
listen 80;  
server\_name [origin.files.com](http://origin.files.com) [origin.gear.net](http://origin.gear.net)[origin.gear.com](http://origin.gear.com);  
location / {  
root /var/www/html/files;  
index index.html index.htm index.php;

}

location ~ .(mp4|jpg)$ {

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

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;  
if ($TW) {  
proxy\_pass [http://tw002.edge.com:80](http://tw002.edge.com:80);  
}  
mp4;  
root /var/www/html/files;

```
            expires 7d;
    valid_referers none blocked video.pk *.video.pk blog.video.pk 

```

\*.  
[facebook.com](http://facebook.com) \*.twitter.com \*.files.com \*.gear.net [video.tv](http://video.tv) \*.video.tv  
[videomedia.tv](http://videomedia.tv) [www.videomedia.tv](http://www.videomedia.tv) [embed.videomedia.tv](http://embed.videomedia.tv);  
if ($invalid\_referer) {  
return 403;  
}  
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

```
    location ~ \.php$ {
        root /var/www/html/files;
        fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
}

```
    location ~ /\.ht {
        deny all;
    }

```

}

EDGE :-

#proxy\_ignore\_headers “Set-Cookie”;  
proxy\_cache\_path /data/nginx/cache levels=1:2 keys\_zone=static:100m  
loader\_threshold=200 loader\_files=500  
inactive=1d  
max\_size=62g;

server {

```
    listen 80;
    server_name tw002.edge.com;
    root /var/www/html/files;
    location ~ \.(mp4|jpeg|jpg)$ {
           root /var/www/html/files;
            mp4;
            try_files $uri @getfrom_origin;

        }

    location @getfrom_origin {
    proxy_pass http://origin.files.com:80;

```

# proxy\_cache\_valid 200 302 60m;

```
    proxy_cache_valid 15d;
    proxy_cache static;
    proxy_cache_min_uses 1;
    }

```

}

Help will be highly appreciated.

---

<div class="post-metadata">

**Author:** ![shahzaib12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shahzaib12](https://www.ruby-forum.com/u/shahzaib12)\
**Post date:** [July 1, 2014, 2:08pm UTC](https://www.ruby-forum.com/t/proxy-cache-not-serving-file-from-edge-server/230717/2 "2014-07-01T14:08:08Z")

</div>

Our caching method is :-

client ----\> origin —\> edge.

On Tue, Jul 1, 2014 at 4:57 PM, shahzaib shahzaib  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shahzaib12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shahzaib12](https://www.ruby-forum.com/u/shahzaib12)\
**Post date:** [July 1, 2014, 2:49pm UTC](https://www.ruby-forum.com/t/proxy-cache-not-serving-file-from-edge-server/230717/3 "2014-07-01T14:49:17Z")

</div>

## shahzaib1232 Wrote:

> Our caching method is :-
> 
> client ----\> origin —\> edge.

This is not going to work as expected, you need client ----\> edge —\>  
origin

Where edge proxy-passes to origin when file is not in cache.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,251374,251384#msg-251384](http://forum.nginx.org/read.php?2,251374,251384#msg-251384)
