我有一个带有React前端和nodeJS / PHP后端(在不同容器上工作)的Docker容器化应用程序。我已经为前端构建和PHP后端成功安装了带有中间容器(let'sencrypt certbot)的https,但是套接字池到nodejs后端存在一些问题。当socket.io池启动时,我在POST请求中收到错误:
[POST] https://my.domain/socket.io/?EIO=3&transport=polling&t=MlE0IBv
405不允许
当我试图通过下一个Nginx构造防止这种情况发生时:
error_page 405 @nodejs;
我在下一条消息中得到了相同的错误代码:
代码:2条消息:“错误的握手方法”
我的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;
}
我的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;
还有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);
答案 0 :(得分:0)
问题已解决。正确的配置是:
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;