无法通过Nginx连接到Web套接字

时间:2020-05-24 03:42:43

标签: node.js sockets nginx websocket architecture

我有一个在端口3000上运行的节点上构建的REST API,现在我向其中添加了一个套接字服务器,代码如下:

    var net = require('net'),
        JsonSocket = require('json-socket');
    var port = 9838;
    var server = net.createServer();
    server.listen(port);
    server.on('connection', function(socket) { //This is a standard net.Socket
        socket = new JsonSocket(socket); //Now we've decorated the net.Socket to be a JsonSocket
        // Handle socket logic
    });

现有的REST API通过nginx托管在ec2上,它将所有HTTP流量重定向到https并将应用程序公开到端口8080,我的问题是现在如何连接到代理后面的这些Web套接字?

这是nginx conf:

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
}

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com
    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:8080/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

我也需要打开端口9838以便客户端连接吗?如果是这样,他们会在https://example.com:9838/上进行连接吗?

0 个答案:

没有答案
相关问题