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;
        }
相关推荐
extrao1 天前
🚀 Kea DHCP4 自动分配系统完整搭建
网络协议
CSharp精选营1 天前
WebSocket 快速入门教程(附示例源码)
websocket·教程·csharp·实时通信·asp.net-core
不做菜鸟的网工3 天前
BGP特性
网络协议
明月_清风5 天前
开发者网络概念全扫盲:一篇搞定
后端·网络协议
刘马想放假5 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
王二端茶倒水6 天前
一套可落地的无线运营方案,不能只管 AP,还要管用户、计费和运维
网络协议
162723816086 天前
EtherCAT 分布式时钟(DC)原理与配置实战:把多轴真正"对齐到同一时刻"
网络协议
王二端茶倒水7 天前
宽带无线项目,怎么从一次性交付变成长期运营收入?
网络协议
Avan_菜菜8 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https