You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails应用配置资产服务器后资源404报错求助(Apache2+Passenger)

Hey there, let's troubleshoot this asset host 404 issue together—since you've already set up the DNS A record and updated your Rails production config, let's focus on the Apache and asset deployment pieces that are most likely causing the problem.

Step-by-Step Solutions

1. Verify Apache Virtual Host Configuration for Assets Subdomain

First, make sure your Apache virtual host for assets.example.com is correctly pointing to your Rails app's precompiled assets directory (usually public/assets).

  • Open your virtual host config file (typically located at /etc/apache2/sites-available/assets.example.com.conf) and check for a setup like this:
    <VirtualHost *:443>
      ServerName assets.example.com
      DocumentRoot /path/to/your/rails/app/public/assets
    
      # HTTPS/SSL settings (critical since you're using HTTPS)
      SSLEngine on
      SSLCertificateFile /path/to/your/ssl/cert.crt
      SSLCertificateKeyFile /path/to/your/ssl/private.key
    
      # Ensure proper access to static assets
      <Directory /path/to/your/rails/app/public/assets>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
      </Directory>
    </VirtualHost>
    
  • Key note: The DocumentRoot must point directly to public/assets, not the entire public directory—this lets Apache serve static assets directly without passing requests through Passenger.
  • After updating the config, restart Apache to apply changes:
    sudo systemctl restart apache2
    

2. Confirm Rails Assets Are Precompiled Correctly

If your assets weren't precompiled for production, the public/assets directory won't have the hashed, production-ready files needed to serve requests.

  • Navigate to your Rails app's root directory and run the precompile command for production:
    RAILS_ENV=production bundle exec rake assets:precompile
    
  • Check the public/assets folder to ensure you see hashed files like application-xyz123.css or logo-abc789.png—these are the files your asset host should serve.
  • Fix file permissions so Apache can read the assets:
    sudo chown -R www-data:www-data /path/to/your/rails/app/public/assets
    sudo chmod -R 755 /path/to/your/rails/app/public/assets
    

3. Disable Passenger for the Assets Subdomain

Passenger can interfere with static asset serving if it's enabled for the assets.example.com virtual host. Since assets are static files, you don't need the Ruby runtime here.

  • Add this line to your assets subdomain's Apache config to turn off Passenger:
    PassengerEnabled off
    
  • Restart Apache again to apply this change.

4. Check SSL Certificate Validity

Since you're using HTTPS, ensure your SSL certificate includes assets.example.com as a valid domain (either as the main domain or a Subject Alternative Name/SAN).

  • Test the SSL connection directly with this command:
    openssl s_client -connect assets.example.com:443
    
  • If you see SSL errors in the output or when accessing assets via browser, you'll need to update your certificate to cover the subdomain.

5. Double-Check DNS and Server Connectivity

Even if you added the A record, it's worth confirming it's propagating correctly:

  • Run nslookup assets.example.com or dig assets.example.com locally to verify it returns your server's IP address.
  • On your server, test a direct asset request with curl to see the exact error:
    curl -I https://assets.example.com/your-actual-asset-file.png
    
    A 404 here confirms the file doesn't exist at the expected path; other codes (like 500) point to Apache config issues.

内容的提问来源于stack exchange,提问作者Santosh Aryal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:35:23