Rails 6搭配Nginx与Puma部署时出现404页面未找到问题求助
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 pumaYou 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.rbTest if you can reach Puma directly:
curl http://127.0.0.1:3000If 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 routesto 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/publicYou should see files like
favicon.ico,robots.txt, and anassetsfolder (if you've precompiled assets).Ensure Nginx has read access to the directory. Nginx typically runs as the
www-datauser, 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.logLook 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.logIf 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

