# Nginx static file serving - Some files are 404, some not

**URL:** <https://www.ruby-forum.com/t/nginx-static-file-serving-some-files-are-404-some-not/244912>\
**Category:** NGINX\
**Created:** [July 6, 2016, 2:39pm UTC](https://www.ruby-forum.com/t/nginx-static-file-serving-some-files-are-404-some-not/244912 "2016-07-06T14:39:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [July 6, 2016, 2:39pm UTC](https://www.ruby-forum.com/t/nginx-static-file-serving-some-files-are-404-some-not/244912/1 "2016-07-06T14:39:22Z")

</div>

I have the following server configuration block:

> 

^/(fonts/|gallery/|images/|javascripts/|stylesheets/|ajax\_info.txt|apple-touch-icon.png|browserconfig.xml|crossdomain.xml|favicon.ico|robots.txt|tile-wide.png|tile.png)

> { root /var/www/html/public; access\_log off; expires max;  
> } error\_page 401 403 404 /404.html; error\_page 500 502 503 504  
> /50x.html;}\*

I want to server my static files with Nginx to my Node/Express app. I  
not  
want to re-factore every single route in my app, that’s why i want to  
server all these static files into / URL path.

The problem is some files cannot be located on the disk, although they  
existing, for example _/images/art/lindon.png_.

This is a docker-compose stack and nginx built from source:

> <https://github.com/DJviolin/lantosistvan/blob/be8e49e2302793d37ed3bfdec865f7086e579197/docker/nginx/Dockerfile>

The error message that I got for a missing file:

\*lantosistvan\_nginx | 2016/07/06 14:24:42 [error] 6#6: \*3 open()

> “/var/www/html/public/images/art/lindon.png” failed (2: No such file or  
> directory), client: 10.0.2.2, server: localhost, request: “GET  
> /images/art/lindon.png HTTP/1.1”, host: “127.0.0.1”, referrer:  
> “[http://127.0.0.1/hu/blog/mariya-balazs](http://127.0.0.1/hu/blog/mariya-balazs)  
> [http://127.0.0.1/hu/blog/mariya-balazs](http://127.0.0.1/hu/blog/mariya-balazs)”\*

Is there any better way to server static files for the / URL without  
blocking\* location / {}\*?

Thank You for your help!

István

---

<div class="post-metadata">

**Author:** ![Lantos\_IstvSSn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@Lantos\_IstvSSn](https://www.ruby-forum.com/u/Lantos_IstvSSn)\
**Post date:** [July 6, 2016, 3:45pm UTC](https://www.ruby-forum.com/t/nginx-static-file-serving-some-files-are-404-some-not/244912/2 "2016-07-06T15:45:23Z")

</div>

Sorry, the parent folder, /images/art was uncommented in .gitignore,  
that’s  
why didn’t uploaded into my repo. Problem solved.

Still, is there any method to share static files? Something like expose  
the  
public folder into / URL, but without blocking the route?

2016-07-06 14:38 GMT+02:00 Lantos István [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Lantos\_IstvSSn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@Lantos\_IstvSSn](https://www.ruby-forum.com/u/Lantos_IstvSSn)\
**Post date:** [July 6, 2016, 6:51pm UTC](https://www.ruby-forum.com/t/nginx-static-file-serving-some-files-are-404-some-not/244912/3 "2016-07-06T18:51:43Z")

</div>

location / only means ‘a location which starts with /’. Basically, this  
catches every single request, and is the least specific way (lowest  
precedence ever) to do so.  
When choosing the most suitable location block, nginx will most of the  
time  
use a more specific one. That is why this is called ‘default location’.

One way I understand your question:  
If you want to have a specific behavior for the ‘/’ path, you could  
use location  
= / which matches only this _exact_ path and has the highest precedence,  
as  
a match with the requested path makes this block immediately selected.

Another way:  
If you want to first browse your filesystem and fall back (in case no  
file  
matches) to proxying the request to backends, that is not what your  
current  
configuraiton file tells nginx to do.  
You would need something like:  
location / {  
try\_files $uri $uri/ @fallback;  
autoindex on;  
}

## location @fallback { }

_B. R._

---

<div class="post-metadata">

**Author:** ![Lantos\_IstvSSn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@Lantos\_IstvSSn](https://www.ruby-forum.com/u/Lantos_IstvSSn)\
**Post date:** [July 6, 2016, 7:02pm UTC](https://www.ruby-forum.com/t/nginx-static-file-serving-some-files-are-404-some-not/244912/4 "2016-07-06T19:02:49Z")

</div>

Check out try\_files.

[http://nginx.org/en/docs/http/ngx\_http\_core\_module.html#try\_files](http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files)
