1

特定のサブドメインを ubuntu AWS EC2 仮想サーバーの特定のポートにリダイレクトしようとしています。すでにDNSで試しましたが、次のトピックに基づいてうまくいきません。ノードhttpプロキシを使用するデフォルトルート? コンピューターで HTTP トラフィックをログに記録するために node.js http-proxy を使用するにはどうすればよいですか? 、ロギング付きのNode.JSプロキシサーバーを作成しようとしていました。そうは言っても、少し混ぜ合わせて(Node.JSは初めてで、まだ学習中です)、次のスクリプトを作成しました。

var httpProxy = require('http-proxy');

var PORT = 80;

logger = function() {
   return function (request, response, next) {
    // This will run on each request.
    console.log(JSON.stringify(request.headers, true, 2));
    next();
  }
}

var options = {
  // this list is processed from top to bottom, so '.*' will go to
  // 'http://localhost:3000' if the Host header hasn't previously matched
  router : {
    'dev.domain.com': 'http://localhost:8080',
    'beta.domain.com': 'http://localhost:8080',
    'status.domain.com': 'http://localhost:9000',
    'health.domain.com': 'http://localhost:9000',
    'log.domain.com': 'http://localhost:9615',
    '^.*\.domain\.com': 'http://localhost:8080',
    '.*': 'http://localhost:3000'
  }
};

// Listen to port 80
httpProxy.createServer(logger(), options).listen(PORT);
console.log("Proxy server started, listening to port" + PORT);

さて、次のエラーが発生し続け、これを機能させる方法がわかりません。

$node proxyServer.js
Proxy server started, listening to port80

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at ProxyServer.listen (/home/ubuntu/QuantBull-Project/node_modules/http-proxy/lib/http-proxy/index.js:130:16)
    at Object.<anonymous> (/home/ubuntu/QuantBull-Project/proxyServer.js:28:43)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

つまり、ポート 80 で http 要求を受信しようとしています。それが sub1.domain.com から来た場合は portA にリダイレクトされ、sub2.domain.com から来た場合は同じ IP から portB にリダイレクトされます。アドレスと両方のポートが公開されています。

誰かがこれを修正する方法を説明し、なぜそれが起こるのか説明できますか?

4

2 に答える 2

3

ポート アクセス:

前の回答とコメントで述べたように、通常のユーザーは 1024 未満のポートを開くことができません。これは、次の指示に従うことで克服できます。

  1. cat /proc/sys/net/ipv4/ip_forward0 が返された場合はnet.ipv4.ip_forward、ファイルのコメントを解除し、/etc/sysctl.confこれらの変更を有効sudo sysctl -p /etc/sysctl.confにします。1 が返された場合は、この手順をスキップします。

  2. ポート 80 から 1024 より上の任意のポート (ポート 8080) への転送を設定します。sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080;

  3. Linux ファイアウォールを開き、ポート 80 での接続を許可しますsudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPTsudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

注:サーバーを再起動してもこれらの変更を保持するには、これをチェックしてください。

http-proxyroutefeature が削除されました:

ポート アクセスを処理した後、プロキシ サーバーは動作せずに続行したため、問題を開いた後、ルーティング機能が削除されたように見えました。Nodejitsu Inc. によると:

この機能は単純化のために削除されました。プロキシビットを担当するだけなので、http-proxyそれ自体ではなく、別のモジュールに属します。http-proxy

そのため、使用することをお勧めしますhttp-master

使用http-master:

http-masterREADME セクションで説明されているように、node.js が必要であり、実行する必要がありますnpm install -g http-master(セットアップによっては、root として実行する必要がある場合があります)。次に、構成ファイル、つまり http-master.conf を作成し、ルーティングの詳細を追加します。この特定の質問の構成ファイルは次のとおりです。

{
# To detect changes made to the config file:
watchConfig: true,
# Enable logging to stdout:
logging: true,
# Here is where the magic happens, definition of our proxies:
ports: {
    # because we defined that Port 80 would be redirected to port 8080 before,
    # we listen here to that port, could be added more, i.e. for the case of a
    # secure connections trough port 443:
    8080 : {
      proxy: {
        # Proxy all traffic for monitor subdomains to port 9000
        'status.domain.com' : 9000,
        'health.domain.com' : 9000,
        # Proxy all traffic for logger subdomains to port 9615
        'log.domain.com' : 9615,
        # Proxy all traffic from remaining subdomains to port 8000
        '*.domain.com' : 8000
      },
      redirect: {
        # redirect .net and .org requests to .com
        'domain.net': 'http://domain.com/[path]',
        'domain.org': 'http://domain.com/[path]'
      }
    }
  }
}

これでほぼ完了です。次のように実行するhttp-master --config http-master.confだけです。サブドメインのルーティングは正常に機能するはずです。

注:プロキシ サーバーをバックグラウンドで実行する場合は、 foreverやpm2 などのツールを使用することをお勧めします。pm2を使用する場合は、この問題を読むことをお勧めします。

于 2014-06-08T16:54:41.163 に答える
1

通常のユーザー (root ではない) としてプロキシを実行している場合、1024 未満のポートを開くことはできません。通常のユーザーとしてこれを行う方法があるかもしれませんが、通常は root などを実行するだけです。

于 2014-06-07T16:41:47.700 に答える