17

【Nginx】面试官问我Nginx能不能配置WebSocket?我给他现场演示了一番!!

 4 years ago
source link: http://www.cnblogs.com/binghe001/p/13334279.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

写在前面

当今互联网领域,不管是APP还是H5,不管是微信端还是小程序,只要是一款像样点的产品,为了增加用户的交互感和用户粘度,多多少少都会涉及到聊天功能。而对于Web端与H5来说,实现聊天最简单的就是使用WebSocket了。而在实现WebSocket聊天的过程中,后台也往往会部署多个WebSocket服务,多个WebSocket服务之间,可以通过Nginx进行负载均衡。今天,我们就来一起说说Nginx是如何配置WebSocket的。

Nginx配置WebSocket

Nginx配置WebSocket也比较简单,只需要在nginx.conf文件中进行相应的配置。这种方式很简单,但是很有效,能够横向扩展WebSocket服务端的服务能力。

先直接展示配置文件,如下所示(使用的话直接复制,然后改改ip和port即可)

map $http_upgrade $connection_upgrade { 
	default upgrade; 
	'' close; 
} 
upstream wsbackend{ 
	server ip1:port1; 
	server ip2:port2; 
	keepalive 1000; 
} 
 
server { 
	listen 20038; 
	location /{ 
		proxy_http_version 1.1; 
		proxy_pass http://wsbackend; 
		proxy_redirect off; 
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr; 
		proxy_read_timeout 3600s; 
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
		proxy_set_header Upgrade $http_upgrade; 
		proxy_set_header Connection $connection_upgrade; 
	} 
}

接下来,我们就分别分析上述配置的具体含义。

首先:

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

表示的是:

  • 如果 $http_upgrade 不为 '' (空),则 $connection_upgrade 为 upgrade 。
  • 如果 $http_upgrade 为 '' (空),则 $connection_upgrade 为 close。

其次:

upstream wsbackend{ 
	server ip1:port1; 
	server ip2:port2; 
	keepalive 1000; 
}

表示的是 nginx负载均衡:

  • 两台服务器 (ip1:port1)和(ip2:port2) 。
  • keepalive 1000 表示的是每个nginx进程中上游服务器保持的空闲连接,当空闲连接过多时,会关闭最少使用的空闲连接.当然,这不是限制连接总数的,可以想象成空闲连接池的大小,设置的值应该是上游服务器能够承受的。

最后:

server { 
	listen 20038; 
	location /{ 
		proxy_http_version 1.1; 
		proxy_pass http://wsbackend; 
		proxy_redirect off;
		proxy_set_header Host $host; 
		proxy_set_header X-Real-IP $remote_addr; 
		proxy_read_timeout 3600s; 
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
		proxy_set_header Upgrade $http_upgrade; 
		proxy_set_header Connection $connection_upgrade; 
	} 
}

表示的是监听的服务器的配置

  • listen 20038 表示 nginx 监听的端口
  • locations / 表示监听的路径(/表示所有路径,通用匹配,相当于default)
  • proxt_http_version 1.1 表示反向代理发送的HTTP协议的版本是1.1,HTTP1.1支持长连接
  • proxy_pass http://wsbackend ; 表示反向代理的uri,这里可以使用负载均衡变量
  • proxy_redirect off; 表示不要替换路径,其实这里如果是/则有没有都没关系,因为default也是将路径替换到proxy_pass的后边
  • proxy_set_header Host $host; 表示传递时请求头不变, $host是nginx内置变量,表示的是当前的请求头,proxy_set_header表示设置请求头
  • proxy_set_header X-Real-IP $remote_addr; 表示传递时来源的ip还是现在的客户端的ip
  • proxy_read_timeout 3600s; 表的两次请求之间的间隔超过 3600s 后才关闭这个连接,默认的60s,自动关闭的元凶
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 表示X-Forwarded-For头不发生改变
  • proxy_set_header Upgrade $http_upgrade; 表示设置Upgrade不变
  • proxy_set_header Connection $connection_upgrade; 表示如果 $http_upgrade为upgrade,则请求为upgrade(websocket),如果不是,就关闭连接

好了,今天就聊到这儿吧!别忘了点个赞,给个在看和转发,让更多的人看到,一起学习,一起进步!!

写在最后

如果你觉得冰河写的还不错,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术,「 冰河技术 」微信公众号更新了大量技术专题,每一篇技术文章干货满满!不少读者已经通过阅读「 冰河技术 」微信公众号文章,吊打面试官,成功跳槽到大厂;也有不少读者实现了技术上的飞跃,成为公司的技术骨干!如果你也想像他们一样提升自己的能力,实现技术能力的飞跃,进大厂,升职加薪,那就关注「 冰河技术 」微信公众号吧,每天更新超硬核技术干货,让你对如何提升技术能力不再迷茫!

RFBj2aM.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK