javascript - Node + socket.io 与 nginx 正确设置

标签 javascript node.js nginx socket.io virtualhost

我有一个从 node.js 提供的简单站点,它将消息发送到 socket.io(它在同一个 node.js 实例上工作)。在 localhost 上一切正常,但是当我尝试在 vps 上设置 nginx 时问题开始了,客户端找不到我的 socket.io 端点。

https://example.com - socket.io 客户端

https://example.com/sender - socket.io 服务器

请记住,来自其他域的客户端也必须能够使用 socket.io 端点。

Node 应用:

const express = require('express');
const path = require('path');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const port = 8080;

app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept-Type');
    res.header('Access-Control-Allow-Credentials', 'true');
    next();
});

app.listen(port, () => {
    console.log("Server is running on port: " + port);
});

server.listen(4200);

app.get('/', (req, res) => {
    res.render('index');
});

io.on('connection', (client) => {
    console.log('Client connected...');

    client.on('messages', (data) => {
        client.emit('reply', data);
    });
});

nginx 配置:

 server {
    listen 80;
    listen [::]:80;

    root /home/project_path/;
    server_name example.com;

    if ($scheme != "https") {
      return 302 https://$host$request_uri;
    } # managed by Certbot

    location /sender {
      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://localhost:4200;
      proxy_redirect off;

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

    location / {
      proxy_pass http://localhost:8080;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com-
    0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com-
    0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  }

最佳答案

Node + socket.io with nginx correct setup :) 👍

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    root         /usr/share/nginx/html;

    location /socket.io/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_pass http://localhost:2156/socket.io/;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

Client socket url : http://localhost/socket.io/

关于javascript - Node + socket.io 与 nginx 正确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643240/

相关文章:

javascript - 在 javascript 中使用回调后解释输出

ruby-on-rails-3 - Heroku 和 Rails 3 的零星 400 错误请求错误 nginx/0.7.67

php - jquery masonry 图像重叠,直到完成页面大小调整

php - 1 个同时下载 + 强制等待时间 + Amazon S3

javascript - 单击(右键单击)使用传单 map 库获取图像叠加层的像素坐标

javascript - 使用Javascript实时读取txt文件

node.js - npm 更新后 angular-cli 构建错误

node.js - 如何在 Express.js 中执行同源策略?

nginx - Curl,证书的所有者与主机名不匹配

nginx - 无法通过高级网络在 AKS 中获取真正的远程 IP