In Rails, should I enable serve_static_assets?

♀尐吖头ヾ 提交于 2019-12-23 18:53:17

问题


I am currently using Apache to proxy to Thin (using this article)

None of my static assets work (e.g. stylesheets, javascripts). Is Apache supposed to be serving them or do I have to enable config.serve_static_assets in config/environments/production.rb? If Apache is supposed to serve them, then what am I probably doing wrong?

Here is my Apache config:

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  DocumentRoot /home/r/public_html/example/public

  RewriteEngine On

  <Proxy balancer://thinservers>
    BalancerMember http://127.0.0.1:5000
    BalancerMember http://127.0.0.1:5001
    BalancerMember http://127.0.0.1:5002
  </Proxy>

  # Redirect all non-static requests to thin
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://thinservers%{REQUEST_URI} [P,QSA,L]

  ProxyPass / balancer://thinservers/
  ProxyPassReverse / balancer://thinservers/
  ProxyPreserveHost on

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  # Custom log file locations
  ErrorLog  /home/r/public_html/example/log/error.log
  CustomLog /home/r/public_html/example/log/access.log combined

</VirtualHost>

回答1:


Delete the following two proxy directive lines and it should work:

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

The first rewrite line (RewriteCond) is a test to see if the file exists on the filesystem in the public directory. If it fails, it continues to the next rewrite line (RewriteRule), which passes the request to the balanced proxy. This line actually does much the same thing as the two proxy directive lines.

If the test succeeds (i.e the static file exists), it'll skip this line. If you've removed the two lines above, apache would then serve the file from the document root. However, with the lines above in, it'll just end up passing it over to the proxy anyway. Then as you pointed out, rails won't be configured to serve this file by default and will return a 404.



来源:https://stackoverflow.com/questions/7870408/in-rails-should-i-enable-serve-static-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!