当您事先不知道 URL 时如何设置套接字连接?
How to setup a socket connection when you don't know the URL in advance?
我正在尝试使用 NGINX 和 NuxtJS 设置一个 Docker 容器,该容器将托管在 AWS EBS 上。我可能稍后会为它设置 DNS,但现在我不会提前知道 URL。无论如何告诉 NGINX/socket.io 使用容器所在机器的地址 运行?
我一直在使用 'localhost' 作为占位符值,它在我的本地计算机上的开发中运行良好。但是当托管在 EBS 上时,该应用程序仍在尝试通过套接字端口连接到我的机器。
当前设置
nginx 配置:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://nuxt:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
socket.io(这是来自模块,但您可以看到 url 设置为本地主机)
io: {
sockets: [{
default: true,
name: 'mainSocket',
url: 'http://localhost:3000'
}],
},
Docker 撰写文件
version: "3.8"
services:
nuxt:
build: ./app
restart: always
ports:
- "3000:3000"
command:
"npm run start"
nginx:
image: nginx:1.19
ports:
- "80:80"
volumes:
- ./nginx:/etc/nginx/conf.d
depends_on:
- nuxt
有什么建议吗?
根据 user253751 的评论,window.location 包含有关应用程序 URL 的数据。在我使用的模块中,这个值很容易访问。情况可能并非总是如此。
我正在尝试使用 NGINX 和 NuxtJS 设置一个 Docker 容器,该容器将托管在 AWS EBS 上。我可能稍后会为它设置 DNS,但现在我不会提前知道 URL。无论如何告诉 NGINX/socket.io 使用容器所在机器的地址 运行?
我一直在使用 'localhost' 作为占位符值,它在我的本地计算机上的开发中运行良好。但是当托管在 EBS 上时,该应用程序仍在尝试通过套接字端口连接到我的机器。
当前设置
nginx 配置:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://nuxt:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
socket.io(这是来自模块,但您可以看到 url 设置为本地主机)
io: {
sockets: [{
default: true,
name: 'mainSocket',
url: 'http://localhost:3000'
}],
},
Docker 撰写文件
version: "3.8"
services:
nuxt:
build: ./app
restart: always
ports:
- "3000:3000"
command:
"npm run start"
nginx:
image: nginx:1.19
ports:
- "80:80"
volumes:
- ./nginx:/etc/nginx/conf.d
depends_on:
- nuxt
有什么建议吗?
根据 user253751 的评论,window.location 包含有关应用程序 URL 的数据。在我使用的模块中,这个值很容易访问。情况可能并非总是如此。