代理Socket.io与NGINX

时间:2018-03-01 13:57:18

标签: node.js nginx socket.io

有没有人知道如何使用nginx代理socket.io?

Nginx v1.13 +

Socket.io v2.0.4

Nodejs v8.9.4

我当前的nginx服务器块。

server {
    listen 80;
    listen 443 ssl http2;
    server_name .domain-name.com;
    root "/home/vagrant/code/domain-name";

    location / {
        proxy_pass "http://localhost:6001";
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    ssl_certificate     /etc/nginx/ssl/domain-name.com.crt;
    ssl_certificate_key /etc/nginx/ssl/domain-name.com.key;
}

我在客户端遇到502 Bad Gateway错误。

1 个答案:

答案 0 :(得分:1)

试试这个

# Sockets redirect
upstream sockets {
    server 127.0.0.1:6001;
}

server {
    listen 80;
    listen 443 ssl http2;
    server_name domain-name.com;
    root /home/vagrant/code/domain-name;

    location / {
        proxy_pass $scheme://sockets;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        proxy_buffers 8 32k;
        proxy_buffer_size 64k;
    }

    ssl_certificate     /etc/nginx/ssl/domain-name.com.crt;
    ssl_certificate_key /etc/nginx/ssl/domain-name.com.key;
}