diff --git a/docker_env/nginx/my.conf b/docker_env/nginx/my.conf index 17c7ea1..81933a6 100644 --- a/docker_env/nginx/my.conf +++ b/docker_env/nginx/my.conf @@ -23,4 +23,21 @@ server { rewrite ^/api/(.*)$ /$1 break; #重写 proxy_pass http://177.8.0.12:8000/; # 设置代理服务器的协议和地址 } + location /socket/ { + rewrite ^/socket/(.*)$ /$1 break; + proxy_pass http://177.8.0.12:8000/; + + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + proxy_set_header Upgrade websocket; + proxy_set_header Connection Upgrade; + + proxy_http_version 1.1; + proxy_connect_timeout 4s; #配置点1 + proxy_read_timeout 300s; #配置点2,如果没效,可以考虑这个时间配置长一点 + proxy_send_timeout 12s; #配置点3 + #add_header Content-Security-Policy upgrade-insecure-requests; + } } diff --git a/docker_env/web/Dockerfile b/docker_env/web/Dockerfile index dfa928a..13a6b22 100644 --- a/docker_env/web/Dockerfile +++ b/docker_env/web/Dockerfile @@ -1,7 +1,7 @@ FROM registry.cn-zhangjiakou.aliyuncs.com/dvadmin-pro/node12-base-web:latest WORKDIR /web/ COPY web/. . -RUN npm install --registry=https://registry.npm.taobao.org +RUN npm install --registry=https://registry.npmmirror.com RUN npm run build FROM nginx:alpine diff --git a/docker_env/web/DockerfileBuild b/docker_env/web/DockerfileBuild index d2e687d..7d44396 100644 --- a/docker_env/web/DockerfileBuild +++ b/docker_env/web/DockerfileBuild @@ -1,3 +1,3 @@ FROM node:14-alpine COPY ./web/package.json / -RUN npm install --registry=https://registry.npm.taobao.org +RUN npm install --registry=https://registry.npmmirror.com diff --git a/web/.env b/web/.env index af33066..bb5e8f9 100644 --- a/web/.env +++ b/web/.env @@ -5,7 +5,7 @@ VUE_APP_TITLE=D2Admin # 网络请求公用地址 VUE_APP_API=/api/ - +VUE_APP_SOCKET_API=/socket # 仓库地址 VUE_APP_REPO=https://github.com/d2-projects/d2-admin-start-kit diff --git a/web/.env.development b/web/.env.development index 2b35a9a..2539510 100644 --- a/web/.env.development +++ b/web/.env.development @@ -6,4 +6,4 @@ VUE_APP_TITLE=企业级后台管理系统 VUE_APP_PM_ENABLED = true # 后端接口地址及端口(域名) VUE_APP_API = "http://127.0.0.1:8000" - +VUE_APP_SOCKET_API = ws://127.0.0.1:8000 diff --git a/web/src/api/websocket.js b/web/src/api/websocket.js index a06bdfc..eb033da 100644 --- a/web/src/api/websocket.js +++ b/web/src/api/websocket.js @@ -3,7 +3,8 @@ import util from '@/libs/util' function initWebSocket (e) { const token = util.cookies.get('token') if (token) { - const wsUri = 'ws://127.0.0.1:8000/?auth=' + token + // 根据环境选择路径 + const wsUri = process.env.NODE_ENV === 'development' ? `${process.env.VUE_APP_SOCKET_API}/?auth=` + token : `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}${process.env.VUE_APP_SOCKET_API}/?auth=${token}` this.socket = new WebSocket(wsUri)// 这里面的this都指向vue this.socket.onerror = webSocketOnError this.socket.onmessage = webSocketOnMessage