Skip to main content

Hosting static images in subdirectories of Public on nginx

Servers • Asked by Brian Schwartz

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.


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


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;

      }

}


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


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;
}

Not seeing anything in your comment? Markdown fail?


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


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?


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


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?


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.


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.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.