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.
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
DocumentRootmust point directly topublic/assets, not the entirepublicdirectory—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/assetsfolder to ensure you see hashed files likeapplication-xyz123.cssorlogo-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.comordig assets.example.comlocally to verify it returns your server's IP address. - On your server, test a direct asset request with curl to see the exact error:
A 404 here confirms the file doesn't exist at the expected path; other codes (like 500) point to Apache config issues.curl -I https://assets.example.com/your-actual-asset-file.png
内容的提问来源于stack exchange,提问作者Santosh Aryal

