[英]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). 我有一个带有react前端和nodeJS / PHP后端的docker容器化应用程序(在不同容器上工作)。 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.
我已经为前端构建和PHP后端成功安装了带有中间容器(让我们使用sencrypt certbot)的https,但是套接字池到nodejs后端存在一些问题。 When socket.io pooling starts I got the error on POST request:
当socket.io池启动时,我在POST请求中收到错误:
[POST] https://my.domain/socket.io/?EIO=3&transport=polling&t=MlE0IBv
[POST] https://my.domain/socket.io/?EIO = 3&transport = polling&t = MlE0IBv
405 Not Allowed
405不允许
When I tried to prevent this by next Nginx construction: 当我试图通过下一个Nginx构造防止这种情况发生时:
error_page 405 @nodejs;
I got the same error code with the next message: 我在下一条消息中得到了相同的错误代码:
code: 2 message: "Bad handshake method"
代码:2消息:“错误的握手方法”
There is part of my Nginx configuration (nginx is separate docker container): 我的Nginx配置有一部分(nginx是单独的docker容器):
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: 我的app.js服务器代码:
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: 和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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.