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

使用Nginx+Passenger部署Rails API遇404路由错误,请求协助排查

Troubleshooting 404 Routing Error for Rails API on Nginx/Passenger/Capistrano

Let’s walk through the most common fixes for your 404 routing error. I’ll start with the most likely culprits and work our way down:

1. Verify Your Rails Routes Exist in the Staging Environment

First, confirm the endpoints you’re trying to access actually exist in your staging setup. SSH into your server, navigate to your app’s current directory, and run:

cd /var/www/xyz/current
RAILS_ENV=staging bundle exec rails routes

Check if the route you’re hitting is listed here. If it’s missing, you might have forgotten to merge changes into your staging branch before deploying, or there’s an issue with route loading in the staging environment.

Capistrano uses a current symlink to point to the latest release. Make sure this symlink is correctly pointing to a valid release directory:

ls -la /var/www/xyz/current

The output should show current -> /var/www/xyz/releases/[timestamp] where the timestamp matches your latest deployment. If the symlink is broken or pointing to an old release, re-run your Capistrano deploy command to fix it.

3. Ensure Passenger is Running & Using the Correct Ruby Version

Passenger needs to use the same Ruby version your Rails app was built with. First, check if Passenger is active:

passenger-status

If it’s not running, restart Nginx to start Passenger:

sudo systemctl restart nginx

Next, verify Passenger is using the right Ruby. Run this command to see Passenger’s Ruby path:

passenger-config about ruby-command

Compare this to the Ruby version your app uses (check /var/www/xyz/current/.ruby-version if you use rbenv/rvm). If they don’t match, add this line to your Nginx server block to specify your app’s Ruby path:

passenger_ruby /path/to/your/app/ruby; # e.g., /home/deploy/.rbenv/shims/ruby

Then restart Nginx again.

4. Validate Staging Environment Configuration

Your staging environment might be missing critical configs. Check:

  • config/environments/staging.rb: Ensure it’s set up for API mode (e.g., config.api_only = true if you’re using Rails API) and routing is enabled.
  • config/database.yml: Confirm staging database credentials are correct and the database is migrated. Run RAILS_ENV=staging bundle exec rails db:migrate if needed.

5. Check Log Files for Detailed Errors

Logs will tell you exactly what’s going wrong. Look at:

  • Nginx Error Log: /var/log/nginx/error.log – This might show Passenger startup issues or file permission errors.
  • Rails Staging Log: /var/www/xyz/current/log/staging.log – Look for routing errors, missing constants, or database connection issues here.
  • Passenger Logs: Depending on your setup, check /var/log/passenger/ or app-specific Passenger logs for startup failures.

6. Fix File Permissions

Nginx (running as www-data user) needs read access to your app directory. Ensure permissions are correct:

sudo chown -R deploy:www-data /var/www/xyz
sudo chmod -R 755 /var/www/xyz

Adjust the user/group names if your deploy user is different.

7. Test a Specific API Endpoint

Since this is an API, the root route (/) might not be defined (which would cause a 404). Try accessing a specific endpoint you know should exist (e.g., /api/users) instead of the root. If that works, the issue is just that the root route isn’t set up, not a server problem.

Final Nginx Config Tweak (Optional)

Your current Nginx config’s location / block is empty. You can add directives to ensure requests are forwarded to Rails correctly:

location / {
    passenger_enabled on;
    try_files $uri $uri/ @passenger;
}

location @passenger {
    passenger_enabled on;
}

Restart Nginx after making changes: sudo systemctl restart nginx


内容的提问来源于stack exchange,提问作者urjit on rails

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:27:28