Rails 3.2 Asset Pipeline with Thin and Apache, not finding assets
Asked Answered
A

5

9

My question is similar to this one Rails 3.2 Asset Pipeline with Passenger Endless Errors except that when I try to actually go to

<link href="/assets/application-eed7996ee9017637f923133371ab3e92.css" media="all" rel="stylesheet" type="text/css" />

I get a 404. Here's the thing I don't understand. It is looking in /assets/, but when I look at the code that was deployed, the assets are only in /public/assets, which is actually a symlink to /var/www/myapp/shared/assets. So what in the world is responsible for telling the app that looking in /assets will produce correct results??

I am using Rails 3.2.0, ruby-1.9.3-p125, deploying to Ubuntu, Apache, and Thin.

I should clarify: My assets are indeed deployed to the server. Everything works perfectly fine until they need to be served, in which case production.log tells me it's looking for them in /assets/application-eed7996ee9017637f923133371ab3e92.css, which 404's.

For every request my thin.log says

cache: [GET /] miss

and production.log says

ActionController::RoutingError (No route matches [GET] "/assets/application-abecf2e096af9ee80697fd49e79a55e7.js"):

UPDATE @Brandan thanks for the help. My assets are indeed in RAILS_ROOT/public/assets. I put this in my Apache vhost file:

DocumentRoot /var/rails/myappname/current/public

RewriteEngine On
XSendFile On
XSendFilePath /var/rails/myappname #not even sure if this line is needed

<LocationMatch "^/assets/.*$">
    Header unset ETag
    FileETag None
    ExpiresActive On
    ExpiresDefault "access plus 1 year"
</LocationMatch>

My RAILS_ROOT/config/environments/production.rb settings:

config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
Athamas answered 24/2, 2012 at 20:19 Comment(5)
how do you create your links?Gonroff
in my layout I say <%= stylesheet_link_tag "application", :media => "all" %> and <%= javascript_include_tag "application" %>Athamas
Did you precompile your assets?Tantalite
yes, I have the latest capistrano and am using the load 'deploy/assets' in my Capfile to do that. But even when I do precompile the assets manually, still no go.Athamas
Also having this problem.. Am keen to find out why.Whimsicality
W
2

I've been having this problem for days now. Thought it was an issue with capistrano or the ruby version however I'm pretty sure it's permissions related too.

My configuration was pretty much the same as yours although I'm also using Unicorn.

Here's what I did to sort:

  1. Temporarily remove the following section because I think that was causing problems with the troubleshooting:

     <LocationMatch "^/assets/.*$">
      Header unset ETag
        FileETag None
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
      </LocationMatch>
    

Perhaps get it all working and then add it back in. I don't think it's the cause of the problems however, when diagnosing things like this, it's best to remove as much as you can to find the culprit.

  1. Run a chown -R xxx.xxx (replace xxx with your application user or web user) on the public directory. As soon as I did so, the css appeared again.

  2. (What I did but maybe not essential) You might also want to install locally without cap. just in case there's an issue with it. That also worked for me.

  3. Completely wipeout tmp/cache and public/* just in case.

  4. Restart your apache server a couple of times.

You can see a gist of my conf. here

Whimsicality answered 26/2, 2012 at 22:19 Comment(3)
could you clarify what you mean by "Temporarily remove the section"? What section? I'm at my wit's end with this problem, I'll try anything at this point!Athamas
Sorry, for some reason there was a bit missing from my answer. I'll update my answer in a few. I know your pain...Whimsicality
@Athamas You should really accept an answer if your problem has been resolved.Whimsicality
P
4

Delete the following lines from your Apache configuration.

ProxyPass / balancer://thinservers/
ProxyPassReverse / balancer://thinservers/

The answer came from In Rails, should I enable serve_static_assets?.

Pitman answered 15/5, 2012 at 19:58 Comment(0)
S
3

Typically, your assets should only exist in /public/assets for a deployed application.

Apache should be configured so that its DocumentRoot is your RAILS_ROOT/public. Then it will serve http://example.com/assets/whatever.css from RAILS_ROOT/public/assets/whatever.css, and it never goes through Rails for static assets.

Have you restarted your application since you precompiled your assets? Sometimes Rails is expecting an older/newer compiled version of your assets than is currently deployed.

Swordsman answered 25/2, 2012 at 1:14 Comment(5)
yes, restarted Apache, Thin, rebooted the server... same result.Athamas
Can you access other files that live in RAILS_ROOT/public/assets from the browser via http://example.com/assets/filename.ext? Is it just Rails's compiled assets that are failing? Also, can you post your config/environments/production.rb? I'm not sure why these requests are hitting Rails at all.Swordsman
Nope, cannot access any file that lives in RAILS_ROOT/public/assets. They all 404. I'll update my post with my production.rb.Athamas
The line I was looking for was config.serve_static_assets = false, which you already have. This sounds like a problem with Apache. What are the ownership and permissions on RAILS_ROOT/public/assets?Swordsman
The permissions look right, lrwxrwxrwx myuser myuser where myuser is the same one I use in deploy.rb. All the assets inside public/assets have these same permissions.Athamas
W
2

I've been having this problem for days now. Thought it was an issue with capistrano or the ruby version however I'm pretty sure it's permissions related too.

My configuration was pretty much the same as yours although I'm also using Unicorn.

Here's what I did to sort:

  1. Temporarily remove the following section because I think that was causing problems with the troubleshooting:

     <LocationMatch "^/assets/.*$">
      Header unset ETag
        FileETag None
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
      </LocationMatch>
    

Perhaps get it all working and then add it back in. I don't think it's the cause of the problems however, when diagnosing things like this, it's best to remove as much as you can to find the culprit.

  1. Run a chown -R xxx.xxx (replace xxx with your application user or web user) on the public directory. As soon as I did so, the css appeared again.

  2. (What I did but maybe not essential) You might also want to install locally without cap. just in case there's an issue with it. That also worked for me.

  3. Completely wipeout tmp/cache and public/* just in case.

  4. Restart your apache server a couple of times.

You can see a gist of my conf. here

Whimsicality answered 26/2, 2012 at 22:19 Comment(3)
could you clarify what you mean by "Temporarily remove the section"? What section? I'm at my wit's end with this problem, I'll try anything at this point!Athamas
Sorry, for some reason there was a bit missing from my answer. I'll update my answer in a few. I know your pain...Whimsicality
@Athamas You should really accept an answer if your problem has been resolved.Whimsicality
G
1

Try removing the ProxyPass and ProxyPassReverse directives from your apache/thin configuration. The P flag in your rewrite rule is already performing the proxy pass that you desire.

See http://httpd.apache.org/docs/2.0/mod/mod_proxy.html for more information.

Guillory answered 27/3, 2012 at 13:15 Comment(0)
A
0

Passanger knows its a RoR application as there is a config.ru file.

The same error you are reporting happened to me due to wrong permissions. Apache was not able to serve the files inside assets, but was able to send the files on public/

In my case I use capistrano so assets was a symlink to shared/public/assets.

what I did was:

chmod -R o+x shared/ 

x permissions are required to list and access directories. After that it worked. You have to make sure that assets has +x for others

Avaavadavat answered 15/7, 2016 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.