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;
        }
相关推荐
拾忆,想起1 小时前
Dubbo配置方式大全:七种配置任你选,轻松玩转微服务!
服务器·网络·网络协议·微服务·云原生·架构·dubbo
pcc_is_world2 小时前
Nginx HTTPS服务器搭建与认证配置
服务器·nginx·https
xinxinhenmeihao3 小时前
爬虫如何使用代理IP才能不被封号?有什么解决方案?
爬虫·网络协议·tcp/ip
Moonquake_www5 小时前
WSL2设置桥接网络至主机IP
网络·网络协议·tcp/ip
杜大哥6 小时前
电脑:如何解决电脑网络IP地址冲突?
网络协议·tcp/ip·电脑
cui_win6 小时前
HTTP协议:常见状态码(400/500 系列)
网络·网络协议·http
北京耐用通信8 小时前
工业通信升级利器:耐达讯自动化Ethernet/IP转CC-Link网关让IO模块兼容无忧!
网络·人工智能·科技·物联网·网络协议·自动化·信息与通信
刘孬孬沉迷学习8 小时前
SCTP、GTP-U 和 WebSocket 协议比较
网络·websocket·网络协议
wadesir8 小时前
Nginx反向代理错误处理(从零开始配置自定义错误页面与故障应对)
运维·nginx
zt1985q8 小时前
本地部署开源运维工具 MyIP 并实现外部访问
运维·服务器·网络协议·开源