Ask A Question

Notifications

You’re not receiving notifications from this thread.

Hosting static images in subdirectories of Public on nginx

Brian Schwartz asked in Servers

Hey Chris (and others),

Switched to nginx from Apache and following the deploy guide for Ubuntu 14.04. My app (which is 3.2.X and Ruby 1.9.3) no longer serves the static images I store in public folder. I have several subdirectories of images there (I have a template engine that uses those) and it doesn't make sense to store them in the asset pipeline because they change between deployments.

Any suggestions on nginx conf tweaks to serve up jpg, pngs, gifs and mp4 files from these subdirectories? I get 404's for them now.

Thanks.

Reply

Those should automatically get served up. Can you post your config? Anything in your logs that doesn't look right?

Reply

I've tried a few tweaks of configuration, here's the latest:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

      server_name xxx;
      passenger_enabled on;
      rails_env    production;
      root         /home/deploy/xxx/current/public;

      # redirect server error pages to the static page /50x.html
      #@error_page   500 502 503 504  /50x.html;

      location = /50x.html {
          root   html;
          passenger_document_root /home/deploy/xxx/current/public;

      }

}

Reply

Alright, here's from production.log for one of the files. Getting them consistently

Exception: ActionController::RoutingError: No route matches [GET] "/clients/xxx/xxx.mp4"
Status Code404

Reply

Interesting. Those should not be hitting Rails.

Maybe try this instead of the location block:

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  server_name xxx;
  passenger_enabled on;
  rails_env    production;
  root         /home/deploy/xxx/current/public;

  error_page 500 502 503 504 /500.html;
}
Reply

Not seeing anything in your comment? Markdown fail?

Reply

Yeah whoops. Redcarpet doesn't do well with it apparently. Updated.

Reply

Same problem (after that change and restarting nginx and touching restart file). None of the files in public folder are being served (with or without a subdirectory). Even favicon.ico or 500.html.

But the files trying to load from my theme are giving me errors in production.log (the one I posted before). If I try to directly to navigate to 500.html or favicon I get no errors (my application layout loads instead of serving the static files).

Could this be a permission issue on public directory?

Reply

Nothing in the nginx error.log when I get those 404's either.

Reply

Ok, I figured it out. I had deploy user as the owner of the app directory. Switched to www-data (which I didn't realize was still needed in nginx) and it's rendering correctly. Maybe add something about who should own / execute the app directory in the deploy guide?

I am stuck on capistrano 2 because of the sheer volume of deployments I have in there. So maybe it does this when you run cap deploy:setup? Or maybe it needs to be part of the recipe?

Reply

That could definitely give errors when nginx tries to serve them up. One thing you can do is change the nginx user to deploy. I often do that so I don't have to worry about accidentally messing up www-data ownership when I deploy.

Reply

I had a similar problem in the past with www-data and permissions on static assets out of the app's public folder. I did what Chris did and changed my Nginx user to deploy to alleviate the issue. It also allows the deploy user to HUP Nginx if need be without having to do sudo.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.