Ubuntu 16.04 Rails one click images 404 not found.

May 25, 2017 273 views
Ruby on Rails One-Click Install Apps Ubuntu 16.04

Images that exist in my repo and display fine in development are 404 not found in production.

I have confirmed the images are present on the production server in root/assets/images/<subdir>/. The images are not accessible in any subpath I have tried typing into the broswer, unlike locally, where I can get a lone image to display in the browser by adding /<subdir>/<imagename> to the url.

I have added the following lines to my /etc/nginx/nginx.conf:
server {
root /home/rails/<myproj>;

        location /images/ {
        root /home/rails/<myproj>/app/assets;
    }
    }

No change. I manually precompiled assets on the server with no errors.

My /config/environments/production.rb has the line:
config.servestaticfiles = ENV['RAILSSERVESTATIC_FILES'].present?

and the rails environment RAILSSERVESTATIC_FILES is set to true.

How can I make my images locatable in my production site?

2 Answers

@jcspampinato

If you're server block only consists of:

server {
    root /home/rails/<myproj>;

    location /images/ {
        root /home/rails/<myproj>/app/assets;
    }
}

The server block really isn't complete. You can take a look at the error logs for NGINX to see what is or may be causing the issue by running:

tail -20 /var/log/nginx/error.log

If you'll paste the output of that command to a code block using the </> icon, we can see if there's a bit of information there that we can use.

Generally though, when it comes to server blocks, you'd have something that looks more like this:

server {
    listen 80;
    server_name domain.com www.domain.com;

    root /home/rails/<myproj>;

    location /images {
        root /home/rails/<myproj>/app/assets;
    }
}

That's very basic and NGINX would only handle /images for you in this case.

Hi!

If you are referring to these images from your app, then you need to use image_path in your code, instead of direct link to the image

Docs http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html

Regards,
Sergey

  • Thanks Sergey. However, as I said in my post, these images and references to them load fine in my development environment. I don't think the problem is an incorrect path or helper.

    • All the images will be available by their real name only in case they are located in app/public folder. It's related to development and production environments.
      But in case of production and assets pipeline, you need to use image_path. Your images has a different names if they are in assets pipeline.

Have another answer? Share your knowledge.