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

发布时间: 更新时间: 总字数:659 阅读时间:2m 作者: IP属地: 分享 复制网址

本文介绍如何使用 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

配置stream模块

HTTP 代理配置

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

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

UDP 代理配置

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

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

$ mv nginx.conf{,.bak}
  • /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;
    }
}

Nginx 代理 WebSocket

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

什么是 WebSocket

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

代理结构

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

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

WebSocket 代理配置

PS: 该方法也可用于对 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;
        }
    }
}

说明:

关键部分是在 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. 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
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数