# Check for existing remote files

**URL:** <https://www.ruby-forum.com/t/check-for-existing-remote-files/201715>\
**Category:** NGINX\
**Created:** [January 13, 2011, 9:43am UTC](https://www.ruby-forum.com/t/check-for-existing-remote-files/201715 "2011-01-13T09:43:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 13, 2011, 9:43am UTC](https://www.ruby-forum.com/t/check-for-existing-remote-files/201715/1 "2011-01-13T09:43:05Z")

</div>

Hi guys. May be my question will break your nerves but please forgive  
me. At first I love Nginx. Nginx is dream come true for poor student  
like me :)) which doesn’t have money for sloooooowly web servers for my  
projects :)). So, BIG thanks for Igor and all of you guys who contribute  
beautiful code for this project. You are awesome!

However, my question ofcource 🙂 Can you tell me guys how I can check  
for existing remote file.  
What i mean:  
how can i check [http://www.google.com/logo.png](http://www.google.com/logo.png) or  
[http://dl.dropbox.com/u/3720261/testdir/file.zip](http://dl.dropbox.com/u/3720261/testdir/file.zip)

10x in advance.

Posted at Nginx Forum:

> **[Check for existing remote files](https://forum.nginx.org/read.php?2,165799,165799#msg-165799)**
>
> Hi guys. May be my question will break your nerves but please forgive me. At first I love Nginx. Nginx is dream come true for poor student like me :)) which doesn't have money for sloooooowly web servers for my projects :)). So, BIG thanks for Igor...

---

<div class="post-metadata">

**Author:** ![mignev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mignev](https://www.ruby-forum.com/u/mignev)\
**Post date:** [January 13, 2011, 10:16am UTC](https://www.ruby-forum.com/t/check-for-existing-remote-files/201715/2 "2011-01-13T10:16:58Z")

</div>

On 1/13/2011 11:40 AM, mignev wrote:

> However, my question ofcource 🙂 Can you tell me guys how I can check  
> for existing remote file.  
> What i mean:  
> how can i check [http://www.google.com/logo.png](http://www.google.com/logo.png) or  
> [http://dl.dropbox.com/u/3720261/testdir/file.zip](http://dl.dropbox.com/u/3720261/testdir/file.zip)

Technically, of course you can build some solution for that, even do it  
in your application nginx serves. But think about network delays that  
would be raise for each check. It’s really not for fast philosophy.

You have to find workaround. I.e., a script running under the cron every  
minute, which checks that [www.gooogle.com/logo.png](http://www.gooogle.com/logo.png) exists, and creates  
file “exists” under some location, else removes it. And nginx will check  
about this file presence.

---

<div class="post-metadata">

**Author:** ![mignev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mignev](https://www.ruby-forum.com/u/mignev)\
**Post date:** [January 13, 2011, 11:00am UTC](https://www.ruby-forum.com/t/check-for-existing-remote-files/201715/3 "2011-01-13T11:00:50Z")

</div>

Hi.  
First thanks for the critique. Such types of solutions are many, but I  
don’t want such solution. In my case i have over a million images of  
which i don’t know the name or their existence. I want to try for  
existence of requested file and if he is found then return to me. If the  
requested file is not exist go to my proxy\_pass $mybackend; and he will  
generate and upload it. WIth this idea i want to skip database layer. I  
have some ideas how i can do that but firstly i want to hear your  
feedback about that.

10x once again for your advice 🙂

Posted at Nginx Forum:

> **[Re: Check for existing remote files](https://forum.nginx.org/read.php?2,165799,165808#msg-165808)**
>
> Hi guys. May be my question will break your nerves but please forgive me. At first I love Nginx. Nginx is dream come true for poor student like me :)) which doesn't have money for sloooooowly web servers for my projects :)). So, BIG thanks for Igor...

---

<div class="post-metadata">

**Author:** ![mignev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mignev](https://www.ruby-forum.com/u/mignev)\
**Post date:** [January 13, 2011, 12:50pm UTC](https://www.ruby-forum.com/t/check-for-existing-remote-files/201715/4 "2011-01-13T12:50:09Z")

</div>

2011/1/13 mignev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> First thanks for the critique. Such types of solutions are many, but I  
> don’t want such solution. In my case i have over a million images of  
> which i don’t know the name or their existence. I want to try for  
> existence of requested file and if he is found then return to me. If the  
> requested file is not exist go to my proxy\_pass $mybackend; and he will  
> generate and upload it. WIth this idea i want to skip database layer. I  
> have some ideas how i can do that but firstly i want to hear your  
> feedback about that.

server {  
…

location ~ ^/remote/(?\<remote\_url\>.+)$ {  
proxy\_intercept\_errors on;  
error\_page 404 = @mybackend;  
proxy\_pass http://$remote\_url;  
}

location @mybackend {  
proxy\_pass http://mybackend\_upstream;  
}  
}

This shows the general idea about how to do this, but i haven’t tested  
so there may be syntax errors.

---

<div class="post-metadata">

**Author:** ![mignev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mignev](https://www.ruby-forum.com/u/mignev)\
**Post date:** [January 13, 2011, 11:52am UTC](https://www.ruby-forum.com/t/check-for-existing-remote-files/201715/5 "2011-01-13T11:52:22Z")

</div>

> how can i check [http://www.google.com/logo.png](http://www.google.com/logo.png) or  
> [http://dl.dropbox.com/u/3720261/testdir/file.zip](http://dl.dropbox.com/u/3720261/testdir/file.zip)

Well it depends what yout want to do after checking if the file does or  
doesnt exist.

If you just want to serve the remote file and fall back to some backup  
location/host you can technically use proxy\_pass (define 2 or more  
upstream  
( [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpUpstreamModule) ) servers which to check  
first  
(your own you can define as ‘backup’)) with proxy\_next\_upstream  
http\_404;  
option ( [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpProxyModule#proxy_next_upstream) ).

For something more sophiscated scenario you would need to write either  
perl  
module or just rewrite the requests to a php script for example.

rr
