Rails应用+ngrok:已定义POST路由/api/callbacks/payfast_notifications仍无法访问
Rails路由无法识别PayFast回调请求问题排查求助
环境配置
本地开发环境使用puma-dev运行,应用同时启用HTTP(80端口)和HTTPS(443端口),通过ngrok将本地环境暴露至公网,用于接收PayFast支付网关的回调。启动ngrok的命令如下:
ngrok http --host-header=api.site_name.test 80
问题现象
当PayFast向/api/callbacks/payfast_notifications发送POST回调请求时,ngrok返回404错误:
NGROK: HTTP Requests POST /api/callbacks/payfast_notifications 404 Not Found
Rails日志显示路由匹配失败:
ActionController::RoutingError (No route matches [POST] "/api/callbacks/payfast_notifications"):
路由定义
回调路由在CallbackRoutes模块中定义:
module CallbackRoutes def self.extended(router) router.instance_exec do scope '/api/callbacks' do scope :module => 'callbacks_api', :as => 'callbacks_api' do resources :payfast_notifications, only: [:create] # ... [其他路由] end end end end end
并在routes.rb中通过域名约束引入:
Rails.application.routes.draw do constraints DomainConstraint.new([REDACTED]) do extend CallbackRoutes # ... [其他路由] end end
执行rails routes可确认路由已存在:
callbacks_api_payfast_notifications POST /api/callbacks/payfast_notifications(.:format) callbacks_api/payfast_notifications#create
已尝试操作
- 反复检查路由定义
- 重启Rails服务器与ngrok
- 排查开发日志中的其他异常
- 确认无中间件干扰请求
目前问题仍未解决,寻求技术帮助。
内容的提问来源于stack exchange,提问作者Michael T
相关产品推荐
相关产品推荐

