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

如何绕过Heroku自带HTTP路由器在Heroku上托管网站并直接获取HTTP流量

Hosting on Heroku Without the Built-in HTTP Router (Direct HTTP Traffic)

Hey there! Bypassing Heroku's HTTP router to get unmodified direct HTTP traffic is totally feasible—you’ve got two solid options depending on your app’s needs: TCP routing for standard apps, or Heroku Private Spaces for fully isolated setups. Let’s break down both approaches step by step.

Option 1: TCP Routing (Standard Heroku Apps)

This is the simplest method for most apps. Since HTTP runs over TCP, setting up a TCP endpoint routes raw traffic straight to your dyno, skipping the Heroku HTTP router entirely.

Step 1: Reserve a TCP Port

Use the Heroku CLI to add a TCP port to your app—pick a port your app will listen on (I’ll use 5000 as an example):

heroku ports:add tcp:5000

After running this, Heroku will generate a public endpoint (like tcp://your-app-12345.herokussl.com:67890) that maps directly to your dyno’s internal port 5000.

Step 2: Configure Your App to Listen on the TCP Port

Forget the usual $PORT environment variable—that’s tied to the HTTP router. Instead, set your app to listen on the reserved port. For a basic Node.js app, that might look like:

const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200);
  res.end('Direct HTTP traffic received—no router interference!\n');
});
server.listen(5000);

Update your Procfile to use a custom dyno type (to avoid accidentally linking to the HTTP router):

tcp-worker: node server.js

Step 3: Scale Your Dyno

Spin up your custom dyno to handle incoming traffic:

heroku ps:scale tcp-worker=1

Step 4: Test Direct Access

Send an HTTP request directly to the public TCP endpoint Heroku provided (e.g., http://your-app-12345.herokussl.com:67890). This traffic hits your dyno directly—no header modifications, no SSL termination (you’ll need to handle that yourself if required), just raw, unaltered HTTP.

Option 2: Heroku Private Spaces (Isolated, Direct IP Access)

If you need full network isolation and direct access to your dyno’s private IP, Private Spaces are the way to go. This is ideal for apps requiring strict control over traffic flow.

Step 1: Create a Private Space

Note: Private Spaces are a paid feature. Set one up with the CLI:

heroku spaces:create my-isolated-space --region us-east-1

Step 2: Deploy Your App to the Space

Link your app to the Private Space and deploy your code:

heroku git:remote -a your-app-name
heroku spaces:join my-isolated-space
git push heroku main

Step 3: Retrieve the Dyno’s Private IP

Each dyno in a Private Space gets a unique private IP. To get it, jump into the dyno’s shell:

heroku ps:exec --app your-app-name
# Inside the shell, run:
hostname -i

From within your private network (or via a VPN connected to the Space), you can send HTTP requests directly to this private IP and the port your app listens on. No Heroku routing infrastructure touches this traffic at all.

Key Considerations

  • SSL Termination: With TCP routing, you’ll need to handle SSL in your app (the HTTP router won’t do it for you). For Private Spaces, you can use a private load balancer or implement SSL directly in your app.
  • Dyno Restarts: If your dyno restarts, its private IP (in Private Spaces) or mapped TCP port (standard apps) may change. Use dynamic DNS or service discovery for persistent access.
  • Pricing: TCP routing is included with standard dyno plans, but Private Spaces have higher tier costs—ensure it fits your budget.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 17:44:05