【问题标题】:How to connect to a nodejs express server through proxy如何通过代理连接到 nodejs express 服务器
【发布时间】:2015-06-15 11:57:20
【问题描述】:

我们有一个 nodejs http 服务器在 IP 182.74.215.86 的 8090 端口上运行,我们想通过代理 104.236.147.107:56220 连接到该服务器。

我们的客户端代码是

	this.socket = require('socket.io-client').connect(url, 
	{
		// This line ensures that each client connection will have different client.id
		'force new connection': true,
	});

	this.socket.once('connect', function() 
	{
	})
	.on('disconnect', function() 
	{
	})
	.on('error', function(e) 
	{
		console.error("(storetalk.js) ASL: Unable to connect to garuda gateway:- " + e);
	});

【问题讨论】:

  • 你考虑过使用nginx吗?
  • 我们有一个 nodejs http 服务器在 AWS 的 8090 端口上运行,我们有一个 Windows 桌面客户端通过 socket.io-client 连接到这个服务器。现在的问题是客户端在连接到代理服务器的 LAN 上时无法工作

标签: node.js proxy socket.io


【解决方案1】:

我已经使用 NGinx 解决了这个问题。

在我的红帽服务器上安装了 nginx ip 192.168.10.238 我的节点应用程序服务器在端口 8090 上的同一 IP 上运行 配置 nginx 监听 8020 并将其配置为支持 socket.io 我的 nodejs 客户端通过这个 nginx 代理连接到应用程序服务器

下面是我的 /etc/nginx/nginx.conf 配置文件

worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events 
{
    worker_connections  1024;
}

http 
{
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    map $http_upgrade $connection_upgrade 
    {
        default upgrade;
        '' close;
    }

    server 
    {
        listen 8020;

        location / 
        {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass  http://192.168.10.238:8090;
            proxy_redirect off;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 2014-06-27
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2011-10-22
    相关资源
    最近更新 更多