How to avoid serving static files in the public folder?

32 views
Skip to first unread message

Alejandro Riera

unread,
Apr 24, 2017, 7:48:36 AM4/24/17
to Phusion Passenger Discussions
Hello everyone,

I have a sinatra application (geminabox) that stores files in the public folder, the one specified by the root directive in the nginx configuration, which looks like this:

server {
    listen 80;
    server_name myapp.com;
    passenger_enabled on;
    root /var/www/myapp.com/public;
}

Inside of the public directory there is another directory (called "gems") that contains different files. The problem that I have is that whenever I hit a url like "myapp.com/gems/my_gem.gem" nginx/passenger attends the request and serves the file, but what I would like the request to be handled by sinatra itself.

Is there a way to tell passenger not to serve a specific static file (or directory) that is contained in the public folder?

Thank you very much,

Alex 

Camden Narzt

unread,
Apr 24, 2017, 11:15:18 AM4/24/17
to Phusion Passenger Discussions
The easiest solution to your problem is to make the path from the site root to the files not match the path your app should receive exactly. Both because having files you don't want served by nginx in with your static files is a bad idea and because Passenger intentionally defers to nginx when static files can be served for performance reasons.

Alejandro Riera

unread,
May 4, 2017, 2:45:31 AM5/4/17
to Phusion Passenger Discussions
Thanks Camden for your reply.

What you said made me think about the way I was structuring things. 
Essentially I was setting the directive root to point to /var/www/myapp.com/public because one level above (i.e. /var/www/myapp.com/) is where my config.ru file is sitting. So I was pointing to public so passenger could find config.ru in its default location.
It turns out that you can override this default with a directive called "passenger_app_root". So the final server block that make it work for me looks something like this:

server {
    listen 80;
    server_name myapp.com;
    passenger_enabled on; 
    root  /var/www/myapp.com;
    passenger_app_root /var/www/myapp.com; # override passenger's default to look for `config.ru` one level above the `root` dir, ie: /var/www
}

Thanks again for the help :)

Hongli Lai

unread,
May 4, 2017, 5:24:21 AM5/4/17
to phusion-passenger
You should not point your nginx root to your app root. That way visitors can download your app's source code or even config files. Instead, point to an empty folder.

Sent from my Android phone.

Op 4 mei 2017 8:45 a.m. schreef "Alejandro Riera" <ari...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Phusion Passenger Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phusion-passenger+unsubscribe@googlegroups.com.
To post to this group, send email to phusion-passenger@googlegroups.com.
Visit this group at https://groups.google.com/group/phusion-passenger.
To view this discussion on the web visit https://groups.google.com/d/msgid/phusion-passenger/125d2d53-5541-4319-a78c-6d54060c2c4e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alejandro Riera

unread,
May 4, 2017, 6:01:42 AM5/4/17
to Phusion Passenger Discussions, hon...@phusion.nl
Oh! Thank you Hongli Lai!
updated :)
To unsubscribe from this group and stop receiving emails from it, send an email to phusion-passen...@googlegroups.com.
To post to this group, send email to phusion-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages