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

Rails 6搭配Nginx与Puma部署时出现404页面未找到问题求助

Troubleshooting "404 Page Not Found" with Nginx + Puma + Rails 6

Let's work through the most likely causes of your 404 error and get your app running smoothly:

1. Fix the Truncated Nginx Configuration First

Looking at your Nginx config, there's a clear syntax error in the gzip_types line:

gzip_types text/plain text/xml text/css image/svg+xml application/javas$

The line is cut off at javas$ — it should end with application/javascript (you can add other common types like application/json too if needed). This invalid syntax might prevent Nginx from reloading your config properly, even if you ran sudo systemctl restart nginx.

First, update this line to something like:

gzip_types text/plain text/xml text/css image/svg+xml application/javascript application/json;

Then verify the config is valid with:

sudo nginx -t

If you see "test is successful", restart Nginx again to apply the fix:

sudo systemctl restart nginx

2. Verify Puma is Running and Accessible

Nginx proxies dynamic requests to 127.0.0.1:3000, so if Puma isn't listening there, those requests will fail outright.

  • Check if Puma is active:

    ps aux | grep puma
    

    You should see Puma processes listed. If not, start Puma (for production, use a process manager like systemd or Foreman for reliability):

    cd /home/deploy/my-website
    RAILS_ENV=production bundle exec puma -C config/puma.rb
    
  • Test if you can reach Puma directly:

    curl http://127.0.0.1:3000
    

    If this returns a 404 too, the issue lies with your Rails app (e.g., missing root route, incorrect environment setup) rather than Nginx. Check your Rails routes with RAILS_ENV=production bundle exec rails routes to confirm a root path is defined.

3. Confirm Static File Path and Permissions

Nginx serves static files from /home/deploy/my-website/public — if this path is wrong or Nginx can't read it, you'll get 404s for assets (and maybe even the root page if no index.html exists).

  • Check if the directory exists and contains expected files:

    sudo ls -l /home/deploy/my-website/public
    

    You should see files like favicon.ico, robots.txt, and an assets folder (if you've precompiled assets).

  • Ensure Nginx has read access to the directory. Nginx typically runs as the www-data user, so adjust permissions if needed:

    sudo chown -R www-data:www-data /home/deploy/my-website/public
    sudo chmod -R 755 /home/deploy/my-website/public
    

4. Precompile Rails Assets (for Production Environment)

In production mode, Rails doesn't serve static files directly — you need to precompile them so Nginx can handle them efficiently.

Run the precompile command from your app directory:

cd /home/deploy/my-website
RAILS_ENV=production bundle exec rails assets:precompile

This will generate compiled assets in public/assets that Nginx can serve directly.

5. Check Logs for Detailed Clues

If the above steps don't resolve the issue, dig into the logs to see exactly what's failing:

  • View Nginx error logs in real-time:

    sudo tail -f /var/log/nginx/my-website/error.log
    

    Look for lines like "file not found" (pointing to a missing static file) or "connection refused" (meaning Nginx can't reach Puma).

  • Check Rails production logs:

    tail -f /home/deploy/my-website/log/production.log
    

    If requests are reaching Rails, you'll see entries here — look for routing errors or other exceptions that might trigger a 404.


内容的提问来源于stack exchange,提问作者Promise Preston

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 19:22:51