WebSocket 应用程序的 Nginx 代理和负载平衡
最编程
2024-04-18 20:46:41
...
WebSocket 介绍
WebSocket是目前比较成熟的技术了,大部分现在的浏览器都支持WebSocket,比如Firefox,IE,Chrome,Safari,Opera,并且越来越多的服务器框架现在也同样支持WebSocket。WebSocket 和HTTP虽然是不同协议,但是两者“握手”方式兼容。通过HTTP升级机制,使用HTTP的Upgrade和Connection协议头的方式可以将连接从HTTP升级为WebSocket。NGINX从1.3版本开始支持WebSocket,其可以作为一个反向代理和为WebSocket程序做负载均衡。即将Upgrade: websocket和Connection: Upgrade必须设置一致
GET /app/websocket/ HTTP/1.1
Host: localhost
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dddskkkdsjss
Origin: http://localhost
Sec-WebSocket-Version: 13
一个典型的Websocket握手请求如下:
客户端请求:
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: example.com
Origin: http://example.com
Sec-WebSocket-Key: sN9cRrP/n9NdMgdcy2VJFQ==
Sec-WebSocket-Version: 13
服务器回应:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: fFBooB7FAkLlXgRSz0BT3v4hq5s=
Sec-WebSocket-Location: ws://example.com/
二.Nginx开启websocket代理功能的配置如下
编辑vhosts下虚拟主机的配置文件,在location匹配配置中添加如下内容:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
具体配置实例如下
server {
listen 80;
server_name 域名;
proxy_http_version 1.1;
#启用支持websocket连接的配置
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
upstream myweb_backend {
hash $remote_addr consistent;
server 10.10.12.108:9001;
server 10.10.12.109:9001;
}
location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-scheme $scheme;
proxy_pass http://myweb_backend;
proxy_connect_timeout 60; #配置规避webSocket连接中断
proxy_read_timeout 600; #配置规避webSocket连接中断
proxy_send_timeout 600; #配置规避webSocket连接中断
}
}
proxy_set_header Upgrade $http_upgrade和proxy_set_header Connection "upgrade"也可以配置到location里面 ,配置如下
server {
listen 80;
server_name 域名;
upstream myweb_backend {
hash $remote_addr consistent;
server 10.10.12.108:9001;
server 10.10.12.109:9001;
}
location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-scheme $scheme;
proxy_pass http://myweb_backend;
proxy_connect_timeout 60; #配置规避webSocket连接中断
proxy_read_timeout 600; #配置规避webSocket连接中断
proxy_send_timeout 600;#配置规避webSocket连接中断
proxy_http_version 1.1;
#启用支持websocket连接的配置
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
上一篇: Lrc 歌词格式简介
下一篇: LRC (文件格式)
推荐阅读
-
Linux 下的 Nginx+Tomcat 负载平衡和运动分离配置点 | Nginx+Tomcat 负载平衡和运动分离配置点
-
WebSocket 应用程序的 Nginx 代理和负载平衡
-
Nginx 配置]使用上游和代理通行证实现反向代理和负载平衡
-
在nginx-2中使用重写(rewrite)和代理通行证(proxy_pass)的区别
-
nginx反向代理位置和proxy_pass斜线踩坑-3:位置末尾无斜线 proxy_pass无斜线的情况下
-
Nginx 反向代理和负载平衡 (1)
-
Java 类加载器的作用 - 简介:类加载器是 Java™ 中一个非常重要的概念。类加载器负责将 Java 类的字节码加载到 Java 虚拟机中。本文首先详细介绍了 Java 类加载器的基本概念,包括代理模型、加载类的具体过程和线程上下文类加载器等。然后介绍了如何开发自己的类加载器,最后介绍了类加载器在 Web 容器和 OSGi™ 中的应用。 类加载器是 Java 语言的一项创新,也是 Java 语言广受欢迎的重要原因之一。它允许将 Java 类动态加载到 Java 虚拟机中并执行。类加载器从 JDK 1.0 开始出现,最初是为了满足 Java Applets 的需求而开发的,Java Applets 需要从远程位置下载 Java 类文件并在浏览器中执行。现在,类加载器已广泛应用于网络容器和 OSGi。一般来说,Java 应用程序的开发人员不需要直接与类加载器交互;Java 虚拟机的默认行为足以应对大多数情况。但是,如果遇到需要与类加载器交互的情况,而您又不太了解类加载器的机制,就很容易花费大量时间调试异常,如 ClassNotFoundException 和 NoClassDefFoundError。本文将详细介绍 Java 的类加载器,帮助读者深入理解 Java 语言中的这一重要概念。下面先介绍一些基本概念。 类加载器的基本概念 顾名思义,类加载器用于将 Java 类加载到 Java 虚拟机中。一般来说,Java 虚拟机以如下方式使用 Java 类:Java 源程序(.java 文件)经 Java 编译器编译后转换为 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码并将其转换为 java.lang 实例。每个实例都用来表示一个 Java 类。通过该实例的 newInstance 方法创建该类的对象。实际情况可能更加复杂,例如,Java 字节代码可能是由工具动态生成或通过网络下载的。 基本上,所有类加载器都是 java.lang.ClassLoader 类的实例。下面将详细介绍这个 Java 类。 java.lang.ClassLoader 类简介 java.lang.ClassLoader 类的基本职责是根据给定类的名称为其查找或生成相应的字节码,然后根据这些字节码定义一个 Java 类,即 java.lang.Class 类的实例。除此之外,ClassLoader 还负责加载 Java 应用程序所需的资源,如图像文件和配置文件。不过,本文只讨论它加载类的功能。为了履行加载类的职责,ClassLoader 提供了许多方法,其中比较重要的方法如表 1 所示。下文将详细介绍这些方法。 表 1.与加载类相关的 ClassLoader 方法
-
使用 Nginx 和 uWSGI 运行 Python 应用程序的详细讲解
-
Nginx 反向代理和负载平衡
-
如何使用 Docker 配置禅道和 Nginx 的反向代理部署教程