【问题标题】:cannot set proxy on node.js无法在 node.js 上设置代理
【发布时间】:2015-10-05 15:38:46
【问题描述】:

我在端口 5550 上有 node.js 4.1.1 和 express.js 4.8.5。我还在端口 8080 上有 Geoserver 2.8.0。两台服务器都在同一台笔记本电脑上。

节点上的应用想要从 Geoserver 访问一些地图数据,这些是 openlayers 上的详细信息

 source: new ol.source.TileWMS({
      url: 'http://localhost:8080/geoserver/mymap/wms?',
      crossOrigin: 'anonymous',
// I also tried  crossOrigin: 'localhost:8080/' and crossOrigin: 'localhost:5550/' but nothing

       params: {'LAYERS': 'mymap:layer, mymap:anotherLayer, FORMAT': 'image/png' ,'CRS': 'EPSG:3857'},
       serverType: 'geoserver'

在 Geoserver 上设置 CORS 或代理不可能导致技术问题(旧的 Jetty 核心、野外的 hack-ish 解决方案、旧 Jetty 版本不可用的jars)。为了避免 CORS 和 Access Control Allow Origins 错误,我想在 Node.js 上设置一个代理。我只想使用 Node,因为它更容易设置。

根据this和之前的问题here我要设置反向代理,所以

  • 我不在客户端做代理配置
  • Geoserver 通过 Node 反向代理提供服务,所以看起来他们 具有相同的起源(=不再有 CORS 问题)
  • 客户端想要访问 Geoserver,但是通过 Node 没有 知道它

我想我的概念是正确的,但我不知道如何实现它。我选择了http-proxy-middleware 来执行此操作。我添加了我的app.js

var proxyMiddleware = require('http-proxy-middleware'); 

    var proxy = proxyMiddleware('http://localhost:8080/geoserver', {
                    target: 'http://localhost:5550',
                    changeOrigin: true   
                });

var app = express();

app.use('/' , function (req, res) {
       res.render('index', { title: 'testing', head: 'Welcome Testing Area'});
    });

app.use(proxy); 
app.listen(5550);

在控制台我看到[HPM] Proxy created: /geoserver -> http://localhost:5550

但我仍然收到错误Image from origin 'http://localhost:8080' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5550' is therefore not allowed access. The response had HTTP status code 404.

我不明白如何实现这一点。请指出我的错误,或者如果我没有正确理解这个概念。请帮我理解。

谢谢

更新

这些是我打开浏览器控制台时看到的标题

General
Remote Address:[::1]:8080
Request URL:http://localhost:8080/geoserver/mymap/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=mymap%3Aplanet_osm_polygon%2C%20mymap%3Aplanet_osm_line%2C%20mymap%3Aplanet_osm_roads%2C%20mymap%3Aplanet_osm_point&TILED=true&CRS=EPSG%3A3857&WIDTH=256&HEIGHT=256&STYLES=&BBOX=2269873.9919565953%2C4618019.500877209%2C2348145.508920616%2C4696291.017841229
Request Method:GET
Status Code:404 Not Found

Response Headers
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=iso-8859-1
Content-Length: 1408
Server: Jetty(6.1.8)

Request Headers
Accept:image/webp,image/*,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:el-GR,el;q=0.8,en;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:5550
Referer:http://localhost:5550/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

【问题讨论】:

标签: node.js express proxy reverse-proxy


【解决方案1】:

您似乎错误地配置了代理。

  • 混合使用普通代理配置和速记配置
  • 目标应该是 Geoserver,而不是您的 express 服务器。

以 Geoserver 为目标的普通语法:

var proxy = proxyMiddleware('/geoserver', {
                 target: 'http://localhost:8080',
                 changeOrigin: true   
            });

或者使用简写语法:

此配置的行为与前一个完全相同。

var proxy = proxyMiddleware('http://localhost:8080/geoserver', {
                 changeOrigin: true   
            });

【讨论】:

  • 我照你说的做了,但还是没有。
  • 您能否提供对服务器的请求及其响应代码?
  • 您的意思是打开浏览器的控制台并复制/粘贴Response HeadersRequest Headers?这将提供您想要的信息?
  • @chimurai 地理服务器在 8080 端口上,webapp 在 5550 上,他想代理 5550 子文件夹下的地理服务器。
  • 我认为我看到了另一个配置错误。 target 应该是 geoserver 而不是 express 服务器。 ` var proxy = proxyMiddleware('/geoserver', { target: 'localhost:8080', changeOrigin: true }); `
【解决方案2】:

chimurai 是对的。最终对我有用的是设置http-proxy-middleware

在我的app.js 我现在有

var proxyMiddleware = require('http-proxy-middleware'); 

var proxy = proxyMiddleware('http://localhost:5550', {
                 target: 'http://localhost:8080',
                 changeOrigin: true,
                 xfwd: true
            });

/*
the above can be replaced by chimurai's version : 
var proxy = proxyMiddleware('/geoserver', {
                 target: 'http://localhost:8080',
                 changeOrigin: true   
            });
and will still work
*/

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));

app.use('/', function(req, res, next) {
    httpProxy.createProxyServer({target:'http://localhost:8080'});
    next();
});

app.use(proxy);

app.listen(5550);

我在我的 Openlayers 代码中删除了这个 crossOrigin: 'anonymous',,并修正了 Openlayers 中链接上的错字,现在可以正常工作了。

我也尝试通过在 Geoserver 上设置代理来解决此问题,但这是不可能的,因为 Geoserver 运行的是现在 EOL 的旧 Jetty 版本,因此没有代理 Geoserver 或升级它的官方解决方案。

通过 Node 解决这个问题是最好的解决方案

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2013-09-06
    • 1970-01-01
    • 2018-03-22
    • 2013-03-16
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多