简体   繁体   中英

Docker + Node.JS + Socket.IO + Nginx (405 & Bad handshake method)

I have a docker containerized application with react front-end and nodeJS/PHP backend (working on different containers). I've successfully installed https with an intermediate container (let'sencrypt certbot) for my front-end build and PHP backend, but have some problem with socket pooling to nodejs backend. When socket.io pooling starts I got the error on POST request:

[POST] https://my.domain/socket.io/?EIO=3&transport=polling&t=MlE0IBv

405 Not Allowed

When I tried to prevent this by next Nginx construction:

error_page 405 @nodejs;

I got the same error code with the next message:

code: 2 message: "Bad handshake method"

There is part of my Nginx configuration (nginx is separate docker container):

upstream node {
    ip_hash;
    server node:4000; //nodejs container
}

server {
    listen 80;
    // ...redirect to https
}

server {
    listen 443 ssl;

    // .... cert's and other settings


    // front-end static react build
    location / {
        try_files $uri /index.html =404;
    }
    location /static {
        try_files $uri @nodejs;
    }

    location @nodejs {
        proxy_pass http://node;
        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;
    }

    // without this string i just got "405 Not Allowed" nginx error page
    // with this string i got probably nodejs "Bad handshake method" error
    error_page 405 @nodejs; 
}

My app.js server code:

const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const socketRouter = require('./sockets/index');
const app = express();

const server = http.Server(app);
const io = socketIO(server, {origins: '*:*'});

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

io.on('connection', socketRouter.bind({}, io));

module.exports = server;

And index.js:

require('dotenv').config();
const app = require('./src/app');

const PORT = process.env.APP_PORT || 4000;
app.listen(PORT);

console.log('Application started on Port ' + PORT);
console.log('APP_ENV ' + process.env.APP_ENV);

Problem was solved. The correct config is:

location /static {
    try_files $uri =404;
}

location /socket.io {
        proxy_pass http://node;
        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;
        add_header  Front-End-Https   on;
}

error_page 405 @nodejs;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM