更新:原来唯一的问题是我在阻止某些端口的防火墙后面,但不是8000.
编辑:TL; DR:无法远程连接到端口9000,但端口8000是可以的,我不知道为什么:(
我有这个在端口8000上运行的node.js应用程序和在端口9000上运行的另一个(http-proxy).
在我的机器上运行它们很好,但是当我把它们放在服务器上时我遇到了一些问题(EC2实例 - 我确实打开了Web控制台安全组[1]中的端口).该应用程序工作正常,但我无法从外部连接到代理.我尝试$ telnet localhost 9000在服务器上连接,所以我猜这是一个好兆头.
我注意到的另一件事是,如果我尝试单独运行应用程序,我会得到相同的结果,即:8000 - OK,9000 - NOTOK:<.但是,如果我将代理使用的端口从9000更改为8000,则可以正常工作.如果我切换端口,即应用程序:9000和代理:8000,我可以连接到代理,但不能连接到应用程序.我也尝试了其他数字,但这也无法解决.
我想有一些非常愚蠢的东西与应用程序本身无关,而且我很遗憾,但我不能指责它,所以有人知道为什么这个设置不起作用?
server.js
var express = require('express.io');
var app = module.exports = express();
require('./proxy');
app.http().io();
app.listen(8000);
// ...
Run Code Online (Sandbox Code Playgroud)
的proxy.js
var httpProxy = require('http-proxy');
var url = require('url');
httpProxy.createServer(function(req, res, proxy) {
// ...
proxy.proxyRequest(req, res, {
host: destination.host,
port: 80
});
}).listen(9000);
Run Code Online (Sandbox Code Playgroud)
$ netstat -pln | grep node 产量
tcp 0 …Run Code Online (Sandbox Code Playgroud) 我使用node-http-proxy模块在node.js中创建了一个代理服务器.
看起来像这样:
var http = require('http'),
httpProxy = require('http-proxy'),
io = require("socket.io").listen(5555);
var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 1338
});
}).listen(9000);
Run Code Online (Sandbox Code Playgroud)
因此,我需要在将响应发送回客户端之前从我代理并分析它的服务器获取正文.
但我无法找到事件或属性,我可以在哪里获得这些数据.我试过了:
proxy.on('end', function (data) {
console.log('end');
});
Run Code Online (Sandbox Code Playgroud)
但我无法想象如何从中获取哑剧身体.
我想编程Node.js http代理,它将能够修改响应体.到目前为止我做到了这一点:
http = require('http'),
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
http.createServer( function (req, res){
//here I want to change the body I guess
proxy.web(req, res, {
target: req.url
});
}).listen(8013);
Run Code Online (Sandbox Code Playgroud)
我试图使用res.write(),但它给了我一个错误"无法在发送后设置标题".好吧,我不想改变标题,我想改变身体.
我该如何改变身体?任何建议将不胜感激.
尝试使用http-proxy 1.4.3安装带有自定义路由逻辑的http代理,在此tuto之后执行以下代码:
var httpProxy = require("http-proxy");
var url = require("url");
httpProxy.createServer(function(req, res, proxy) {
var hostname = req.headers.host.split(":")[0];
var pathname = url.parse(req.url).pathname;
// Options for the outgoing proxy request.
var options = { host: hostname };
// Routing logic
if(hostname == "127.0.0.1") {
options.port = 8083;
} else if(pathname == "/upload") {
options.port = 8082;
options.path = "/";
} else {
options.port = 8081;
}
// (add more conditional blocks here)
proxy.proxyRequest(req, res, options); …Run Code Online (Sandbox Code Playgroud) 我做了使用基本的聊天应用程序node.js,express和socket.io。与socket.io 的教程聊天应用程序没有太大区别,它只是在连接的客户端之间发出事件。当我在服务器上的端口3001上运行它时,它工作正常。
然后,我制作了一个代理服务器应用程序,使用node-http-proxy该服务器侦听端口80并将基于请求的url的流量重定向到我在不同端口上运行的各种独立节点应用程序。非常简单。但是有些东西坏了。每当有人断开连接时,每个单个插座都会断开并重新连接。这对具有基于连接事件的聊天应用程序不利。客户端控制台均显示:
WebSocket与“ ws:// [某些套接字信息] ”的连接失败:在收到握手响应之前,连接已关闭
我认为这是代码的重要部分。
var http = require('http');
var httpProxy = require('http-proxy');
//create proxy template object with websockets enabled
var proxy = httpProxy.createProxyServer({ws: true});
//check the header on request and return the appropriate port to proxy to
function sites (req) {
//webapps get their own dedicated port
if (req == 'mychatwebsite.com') {return 'http://localhost:3001';}
else if (req == 'someothersite.com') {return 'http://localhost:3002';}
//static sites are …Run Code Online (Sandbox Code Playgroud) 我有以下代码在我的本地环境中运行良好.但是,当我尝试从Docker容器(通过Boot2Docker)运行相同的代码时,我根本无法访问https:// [boot2docker_ip]:4000
我尝试使用所有这些选项更新下面代码中的目标值,但它们似乎都没有做到这一点:
target: 'http://localhost:3000',
target: 'http://0.0.0.0:3000',
target: 'http://127.0.0.1:3000',
target: 'http://<boot2docker_ip>:3000',
var fs = require('fs');
require('http-proxy').createProxyServer({
ssl: {
key: fs.readFileSync(__dirname + '/certs/ssl.key', 'utf8'),
cert: fs.readFileSync(__dirname + '/certs/ssl.crt', 'utf8')
},
target: 'http://localhost:3000',
ws: true,
xfwd: true
}).listen(4000);
Run Code Online (Sandbox Code Playgroud)
我正在使用https://github.com/nodejitsu/node-http-proxy中的node-http-proxy软件包
这是一个Git repo来尝试这种行为; 为简单起见,我已经检查了假SSL.
Dockerfile:
FROM readytalk/nodejs
ADD ./src /app
ADD ./ssl-proxy /proxy
COPY ./run.sh /run.sh
RUN chmod +x /run.sh
EXPOSE 3000
EXPOSE 4000
ENTRYPOINT ["/run.sh"]
Run Code Online (Sandbox Code Playgroud)
run.sh:
#!/bin/sh
/nodejs/bin/node /app/main.js; /nodejs/bin/node /proxy/main.js
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用http-proxy代理对REST API的调用,但它会不断返回404代码.
这是对我的API的示例调用:http://petrpavlik.apiary-mock.com/notes它返回一些JSON数据.
这就是我的服务器代码:
var httpProxy = require('http-proxy');
var proxy = httpProxy.createServer({
target:'http://petrpavlik.apiary-mock.com'
});
proxy.listen(8005);
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
proxy.on('proxyRes', function (res) {
console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
});
Run Code Online (Sandbox Code Playgroud)
当我尝试使用我的代理调用相同的请求时,这就是我得到的.
Petrs-MacBook-Air-2:~ petr$ curl -v 127.0.0.1:8005/notes
* About to connect() to 127.0.0.1 port 8005 (#0)
* Trying 127.0.0.1...
* Adding handle: conn: 0x7f977280fe00
* Adding …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Express应用程序的代理设置为应用程序中特定路径的根路径:
http://my-domain.com/some/route --> http://another-domain:8000/
Run Code Online (Sandbox Code Playgroud)
我已经根据http-proxy文档尝试了多种方法,但是我一直在用路径/路由碰壁。我正在尝试在已登录的Express应用程序中执行此操作,以便我也可以在尝试代理的应用程序后面使用身份验证。我不断收到与代理的应用程序有关的错误,说未定义路径“ / some / route” ...等等。
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
proxy.proxyRequest(req, res, {
host:'localhost',
port:8000
});
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
var url = 'http://localhost:8000/';
var httpProxy = require('http-proxy'),
proxy = httpProxy.createProxyServer({});
proxy.web(req,res, { target: url }, function(e) {
console.log('proxy.web callback');
console.log(e);
});
Run Code Online (Sandbox Code Playgroud)
该函数调用,但我最终遇到一个明确的404错误...
如果可能的话,我也想传递一些变量,例如:
http://my-domain.com/some/route?var1=something&var2=something --> http://another-domain:8000/?var1=something&var2=something
Run Code Online (Sandbox Code Playgroud)
但是无法弄清楚这是否可行,我尝试在请求上进行设置,因为该请求已发送到proxyRequest中,但无法在第二个应用程序中找到它们。
如何使用nodejs中的查询字符串参数代理请求,我目前正在使用express和http-proxy?
我有一个使用express和http-proxy模块的nodejs应用程序将HTTP GET请求从我的端点上的某些路径代理到在同一服务器上运行的第三方API但是有一个不同的端口(因此遇到了相同的原始问题,需要代理).这工作正常,直到我想在后端API上使用查询字符串参数调用REST函数,即"?name = value".然后我得到一个404.
var express = require('express');
var app = express();
var proxy = require('http-proxy');
var apiProxy = proxy.createProxyServer();
app.use("/backend", function(req,res){
apiProxy.web(req,res, {target: 'http://'+ myip + ':' + backendPort + '/RestApi?' + name + '=' + value});
});
Run Code Online (Sandbox Code Playgroud)
Chrome的控制台显示:
"GET http://localhost:8080/backend 404 (Not Found)"
Run Code Online (Sandbox Code Playgroud)
注意:我稍后在快递中使用其他东西,但不是在代理行之前使用,而是在路由路径时从更具体到更通用.可以使用相同的协议在浏览器中直接访问后端:// url:port/path?name = value没有问题.
我试图让一个非常简单的代理使用node-http-proxy,我希望只返回谷歌的内容:
const http = require('http');
const httpProxy = require('http-proxy');
const targetUrl = 'http://www.google.co.uk';
const proxy = httpProxy.createProxyServer({
target: targetUrl
});
http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(6622);
Run Code Online (Sandbox Code Playgroud)
例如,我希望http:// localhost:6622/images/nav_logo242.png代理到http://www.google.co.uk/images/nav_logo242.png而不是返回404找不到.
谢谢.
node-http-proxy ×10
node.js ×10
proxy ×5
http-proxy ×2
javascript ×2
amazon-ec2 ×1
boot2docker ×1
docker ×1
express ×1
http ×1
socket.io ×1
sockets ×1