使用docker部署MERN应用时的socket.io错误
我与Docker一起在Digitalocean上部署了一个MERN应用程序。后端和前端看起来不错,但是由于某种原因,已部署的应用程序对Websocket,socket.io连接失败。
我使用 http-proxy-middleware ,我的 setupproxy.js file:(main-be是容器的名称)< /em>
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://main-be:5001',
changeOrigin: true,
})
);
};
frontend/src/utils/axios.js :
import axios from 'axios';
export const baseURL = 'https://example.com';
export default axios.create({ baseURL });
frontend/src/utils/startants.js :
const API_BASE_ORIGIN = 'wss://46.111.119.161';
export { API_BASE_ORIGIN };
:
const API_BASE_ORIGIN = 'https://example.com';
const API_BASE_ORIGIN = 'ws://46.111.119.161:5001';
const API_BASE_ORIGIN = 'ws://46.111.119.161';
const API_BASE_ORIGIN = 'wss://46.111.119.161';
const API_BASE_ORIGIN = 'wss://46.111.119.161:5001';
... ...我在这里尝试了这些,但没有任何效法 socketContext.js :
//* socket connection
useEffect(() => {
const newSocket = socketIo.connect(API_BASE_ORIGIN, {
transports: ['websocket'],
});
setSocket(newSocket);
if (!newSocket) return;
newSocket.on('connect', () => {
console.log(`Hurrah Socket ${newSocket.id} Connected`);
});
}, []);
nginx default.conf 配置文件:
upstream api {
server main-be:5001;
}
upstream client {
server main-fe:3000;
}
server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://$host$request_uri;
}
# main server block
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
# enable subfolder method reverse proxy confs
include /config/nginx/proxy-confs/*.subfolder.conf;
# all ssl related config moved to ssl.conf
include /config/nginx/ssl.conf;
client_max_body_size 0;
location / {
proxy_pass http://client;
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;
}
location /api {
proxy_pass http://api;
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;
}
location /ws/ {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
I have a MERN app deployed on digitalocean with docker. Backend and frontend seem good but for some reason the Websocket, socket.io connection fails on the deployed application.
I use http-proxy-middleware, my setupProxy.js file: (main-be is the name of container)
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://main-be:5001',
changeOrigin: true,
})
);
};
frontend/src/utils/axios.js:
import axios from 'axios';
export const baseURL = 'https://example.com';
export default axios.create({ baseURL });
frontend/src/utils/constants.js:
const API_BASE_ORIGIN = 'wss://46.111.119.161';
export { API_BASE_ORIGIN };
...here i tried these but none worked:
const API_BASE_ORIGIN = 'https://example.com';
const API_BASE_ORIGIN = 'ws://46.111.119.161:5001';
const API_BASE_ORIGIN = 'ws://46.111.119.161';
const API_BASE_ORIGIN = 'wss://46.111.119.161';
const API_BASE_ORIGIN = 'wss://46.111.119.161:5001';
part of socketContext.js:
//* socket connection
useEffect(() => {
const newSocket = socketIo.connect(API_BASE_ORIGIN, {
transports: ['websocket'],
});
setSocket(newSocket);
if (!newSocket) return;
newSocket.on('connect', () => {
console.log(`Hurrah Socket ${newSocket.id} Connected`);
});
}, []);
NGINX default.conf config file:
upstream api {
server main-be:5001;
}
upstream client {
server main-fe:3000;
}
server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://$host$request_uri;
}
# main server block
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
# enable subfolder method reverse proxy confs
include /config/nginx/proxy-confs/*.subfolder.conf;
# all ssl related config moved to ssl.conf
include /config/nginx/ssl.conf;
client_max_body_size 0;
location / {
proxy_pass http://client;
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;
}
location /api {
proxy_pass http://api;
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;
}
location /ws/ {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案:
添加
wds_socket_port = 0
to react frontend .env 文件。 (,它不会添加不必要的附加端口)编辑nginx default.conf配置文件(不是整个文件):
setUpproxy.js 文件:
frontend/src/utlc/utils/axios.js:
frontend/src/utils/startants.js:
Solution:
add
WDS_SOCKET_PORT=0
to the React frontend .env file. (so it will not add an unnecessary additional port)edit the nginx default.conf config file to this (it's not the whole file):
setupProxy.js file:
frontend/src/utils/axios.js:
frontend/src/utils/constants.js:
我还使用nginx的React和Nestjs部署Fullstack应用程序遇到了一些困难。这是我的方法:
与套接字IO相关的任何请求将被路由到后端中的/socket.io/前缀,与API交互的请求将路由到/api/v1/。
这是Fe中的Env文件:
我使用Docker容器来部署整个应用程序,包括FE容器,是容器,Nginx容器。
I also faced some difficulties in deploying fullstack application using react and nestjs with nginx. Here is my approach:
Any requests related to socket io will be routed to the /socket.io/ prefix in the backend, requests that interact with the api will be routed to /api/v1/.
This is the env file in fe:
I used docker container to deploy the entire application, including fe container, be container, nginx container.