Young's blog Young's blog
首页
Spring
  • 前端文章1

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Young

首页
Spring
  • 前端文章1

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Nginx基础
  • Nginx配置
  • Nginx proxy_pass 配置转发 / 路径
  • Nginx 配置密码认证
  • Nginx 配置 CPU 亲和性
  • Nginx 配置 UDP/TCP/WebSocket 反向代理
    • 安装编译
    • 配置 stream 模块
      • HTTP 代理配置
      • UDP 代理配置
    • Nginx 代理 WebSocket
      • 什么是 WebSocket
      • 代理结构
      • WebSocket 代理配置
    • 参考
  • Nginx 配置目录文件列表显示功能
  • Nginx 配置 gzip 压缩、缓存功能
  • Nginx 配置端口转发
  • Nginx底层进程机制剖析
  • nginx配置location匹配顺序总结
  • nginx
andanyang
2023-03-29
目录

Nginx 配置 UDP/TCP/WebSocket 反向代理

本文介绍如何使用 Nginx 配置 UDP/TCP/WebSocket 反向代理。怎样实现 UDP 的反向代理,Nginx 从 1.9.13 起开始发布 ngx_stream_core_module 模块不仅能支持 TCP 代理及负载均衡,其实也是支持 UDP 协议的。

# 安装编译

ngx_stream_core_module 这个模块并不会默认启用,需要在编译时通过指定 --with-stream 参数来激活这个模块。

$ yum -y install proc* openssl* pcre*
$ wget http://nginx.org/download/nginx-1.10.3.tar.gz
$ tar zxvf nginx-1.10.3.tar.gz
$ cd nginx-1.10.3
$ ./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
$ make
$ make install
1
2
3
4
5
6
7

# 配置 stream 模块

# HTTP 代理配置

stream {
    server {
        listen 9958 udp;
        proxy_pass vpn.ykw.xyz:9958;
    }

    server {
        listen 9958;
        proxy_pass vpn.ykw.xyz:9958;
    }
}
1
2
3
4
5
6
7
8
9
10
11

# UDP 代理配置

UDP 负载均衡解决了两个关键点:高可用性和横向扩展。UDP 设计是不保证端至端传送数据的,因此需要在客户端软件来处理网络级错误和重传机制。

stream 模块必需在 nginx.conf 中配置,下面配置为 DNS 代理配置:

$ mv nginx.conf{,.bak}
$ vim  /etc/nginx/nginx.conf
worker_processes auto;
error_log /var/log/nginx_error.log info;
events {
    worker_connections  1024;
}
stream {
    upstream dns {
        server 192.168.0.1:53;
        server dns.example.com:53;
    }
    server {
        listen 127.0.0.1:53 udp;
        proxy_responses 1;
        proxy_timeout 20s;
        proxy_pass dns;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Nginx 代理 WebSocket

下面介绍如何在 Nginx 中配置 WebSocket 代理。

# 什么是 WebSocket

WebSocket 是一种网络传输协议,可在单个 TCP 连接上进行全双工通信,位于 OSI 模型的应用层。WebSocket 中的握手和 HTTP 中的握手过程兼容,且可以使用 HTTP 中的 Upgrade 协议头将连接从 HTTP 升级到 WebSocket。

# 代理结构

WebSocket 可以工作在 80/443 端口,并且使用 ws:// 或 wss:// 标记协议类型。可以 nginx 代理 WebSocket,实现从 HTTP/1.1 到 Websocket 转化,结构图如下:

Client HTTPS <--wss--> Nginx Websocket Proxy <-- ws --> App Server
1

# WebSocket 代理配置

该方法也可用于对 Kubernetes API 的代理。

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        ...

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $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;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

说明:

关键部分是在 HTTP 的请求中添加了如下 header,将协议从 HTTP1.1 升级到 WebSocket:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

# 响应
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
1
2
3
4
5
6
7

# 参考

  1. http://nginx.org/en/docs/stream/ngx_stream_core_module.html
  2. https://www.hi-linux.com/posts/14615.html
  3. http://nginx.org/en/docs/http/websocket.html
编辑 (opens new window)
上次更新: 2024/04/19, 08:52:45
Nginx 配置 CPU 亲和性
Nginx 配置目录文件列表显示功能

← Nginx 配置 CPU 亲和性 Nginx 配置目录文件列表显示功能→

最近更新
01
idea 热部署插件 JRebel 安装及破解,不生效问题解决
04-10
02
spark中代码的执行位置(Driver or Executer)
12-12
03
大数据技术之 SparkStreaming
12-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Young | MIT License
浙ICP备20002744号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式