Question

Ubuntu 16.04 Rails one click images 404 not found.

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.serve_static_files = ENV[‘RAILS_SERVE_STATIC_FILES’].present?

and the rails environment RAILS_SERVE_STATIC_FILES is set to true.

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

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

@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.