Websocket链接如何配置nginx转发规则?

前端页面部署在localhost:8088端口上,后端websocket服务部署在localhost:8082端口上,下面介绍两种环境的转发规则。

前端接口:ws://localhost:8088/api/ws/runs/16?token=null

1、开发环境(vue3框架)

复制代码
const { defineConfig } = require('@vue/cli-service');

module.exports = defineConfig({
  publicPath: process.env.VUE_APP_BASE_URL,
  outputDir: `./dist/${process.env.VUE_APP_BASE_URL}`,
  transpileDependencies: true,
  productionSourceMap: false,
  css: {
    sourceMap: true,
  },
  chainWebpack: config => {
    config.devtool('eval-source-map')
  },
  chainWebpack: (config) => {
    config.plugin('define').tap((definitions) => {
      Object.assign(definitions[0], {
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(false),
        __VUE_OPTIONS_API__: JSON.stringify(true),
        __VUE_PROD_DEVTOOLS__: JSON.stringify(false),
      });
      return definitions;
    });
  },
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8081',
        ws: true,
        changeOrigin: true
      },
    }
  }
});

2、生产环境

复制代码
        location ^~ /api/ws/ {
                proxy_pass http://127.0.0.1:8082;
                
                 proxy_http_version 1.1;
                 proxy_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection "upgrade";
                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 X-Forwarded-Proto $scheme;
                 
                proxy_read_timeout 3600s;
                proxy_send_timeout 3600s;
        }
相关推荐
默默前行的虫虫3 分钟前
解决EMQX WebSocket连接不稳定及优化WS配置提升稳定性?
websocket
那就回到过去1 小时前
MPLS多协议标签交换
网络·网络协议·hcip·mpls·ensp
胖咕噜的稞达鸭6 小时前
网络基础:初识TCP/IP协议
网络·网络协议·tcp/ip
全栈工程师修炼指南6 小时前
Nginx | stream content 阶段:UDP 协议四层反向代理浅析与实践
运维·网络·网络协议·nginx·udp
cur1es7 小时前
【UDP的报文结构】
网络·网络协议·udp·md5
闲人编程7 小时前
使用FastAPI和WebSocket构建高性能实时聊天系统
websocket·网络协议·网络编程·fastapi·持久化·实时聊天·codecapsule
惊讶的猫7 小时前
OpenFeign(声明式HTTP客户端)
网络·网络协议·http·微服务·openfeign
鹏北海7 小时前
micro-app 微前端项目部署指南
前端·nginx·微服务
心.c8 小时前
TCP协议深入解析
网络·网络协议·tcp/ip
摇滚侠8 小时前
HTTP 404 - No response body available
网络·网络协议·http