服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx 配置 WebSocket 代理的操作过程

Nginx 配置 WebSocket 代理的操作过程

2024-04-01 16:01枫飘长安 Nginx

这篇文章主要介绍了Nginx 配置 WebSocket 代理的操作过程,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

Nginx 配置 WebSocket 代理

Nginx 官方文档网址 nginx documentation

...
http:{
  ...
  server{
    ...
    # WebSocket代理
    location /wsUrl/ {
      rewrite ^/wsUrl/(.*)$ /$1 break; #拦截标识去除
      proxy_pass http://192.168.100.20:8080; #这里是http不是ws,不用怀疑,代理的ip和port写ws访问的实际地址
      proxy_http_version 1.1; #这里必须使用http 1.1
      #下面两个必须设置,请求头设置为ws请求方式
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
    ...
  }
  ...
}

官方文档代理样例

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    map $http_upgrade $connection_upgrade {
		default upgrade;
		''      close;
	}
    server {
        listen       9001;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location ^~ /websocket {
            proxy_pass http://localhost:8090/;
            proxy_http_version 1.1;
            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_read_timeout 120s;
            proxy_set_header Upgrade websocket;
            proxy_set_header Connection Upgrade;
        }
    }
}

Linux 查看安装文件命令手册

[!起因]
我使用指令 whereis nginx 跳出来了很多路径,但是我不太明白每个路径是什么意思,就仔细去看了看,然后发现了一个路径 /usr/share/man/man8/ 这个目录,下面一般都是手册路径,在这里面可以看很多软件的基本指令操作 可使用指令 man nginx 来查看 nginx.8.gz 手册。

Nginx 日志配置方案

可以参考 Nginx访问日志(access_log)配置及信息详解_nginx access.log配置

一般使用 main 格式

如下

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                       '$upstream_addr $upstream_response_time $request_time ';
access_log  logs/access.log  main;
  • $remote_addr: 客户端的IP地址。
  • $remote_user: 使用HTTP基本身份验证的情况下,远程用户的用户名。
  • $time_local: 本地时间的访问时间。
  • $request: 客户端请求的内容。
  • $status: 服务器响应的HTTP状态码。
  • $body_bytes_sent: 发送给客户端的字节数,不包括响应头的大小。
  • $http_referer: 引用页面的URL。
  • $http_user_agent: 客户端的User-Agent字符串,标识客户端的浏览器和操作系统等信息。
  • $http_x_forwarded_for: X-Forwarded-For 头,用于标识原始客户端的IP地址,当请求通过代理服务器时使用。
  • $upstream_addr: 后端(上游)服务器的IP地址。
  • $upstream_response_time: 从后端服务器接收响应的时间。
  • $request_time: 客户端发起请求到收到响应的总时间。

[!错误]
配置 nginx 日志的时候,由于不知道要将 log_format main 配置放在哪里,就放在了最外层,导致错误提示 nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/nginx.conf:14
后序解决是 将 log_format main 放在 http {} 里面就解决问题了

成功解决问题–使用 Nginx 代理 WebSocket

nginx.conf具体配置如下, 实现的功能是将所有发往 10.6.30.185:9001 的请求去匹配一下 url
里面有没有 /websocket 这一级,如果有就使用 WebSocket 请求发往 10.6.3.46:8001 ,后序使用了6台服务器进行了一个 nginx 代理 WebSocket 操作,都能够在后台读取到信息,同时,后台也能够推送信息过去。

user nobody;  
worker_processes  6;  
#nginx 开启多核设置,目前185的机子,都是6核  
worker_cpu_affinity 000001 000010 000100 001000 010000 100000;  
#error_log  logs/error.log;  
#error_log  logs/error.log  notice;  
#error_log  logs/error.log  info;  
error_log  /var/log/nginx/error.log info;  
#进程文件  
pid        /var/run/nginx.pid;  
worker_rlimit_nofile 1024;  
events {  
    use epoll; # 修改这里  
    worker_connections  1024;  
}
# 设置http 服务器  
http {  
    include       mime.types; #文件扩展名与文件类型映射表  
    default_type  application/octet-stream; #默认文件类型  
    charset utf-8; #默认编码  
    fastcgi_connect_timeout 2000;  
    fastcgi_send_timeout 2000;  
    fastcgi_read_timeout 2000;  
    client_max_body_size 1024m;  
    sendfile on;  
    tcp_nopush on;  
    tcp_nodelay on;  
    keepalive_timeout 120;  
    gzip  on;  
    limit_req_zone $binary_remote_addr zone=test:10m rate=10r/s;  
    #日志配置  
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
                          '$status $body_bytes_sent "$http_referer" '                          '"$http_user_agent" "$http_x_forwarded_for"'                           '$upstream_addr $upstream_response_time $request_time ';  
            #$remote_addr: 客户端的IP地址。  
            #$remote_user: 使用HTTP基本身份验证的情况下,远程用户的用户名。  
            #$time_local: 本地时间的访问时间。  
            #$request: 客户端请求的内容。  
            #$status: 服务器响应的HTTP状态码。  
            #$body_bytes_sent: 发送给客户端的字节数,不包括响应头的大小。  
            #$http_referer: 引用页面的URL。  
            #$http_user_agent: 客户端的User-Agent字符串,标识客户端的浏览器和操作系统等信息。  
            #$http_x_forwarded_for: X-Forwarded-For 头,用于标识原始客户端的IP地址,当请求通过代理服务器时使用。  
            #$upstream_addr: 后端(上游)服务器的IP地址。  
            #$upstream_response_time: 从后端服务器接收响应的时间。  
            #$request_time: 客户端发起请求到收到响应的总时间。  
    access_log /var/log/nginx/nginx-access.log main;
	map $http_upgrade $connection_upgrade {  
	    default upgrade;  
	    ''      close;  
	}
    server {  
        listen 9001;  
        server_name  10.6.30.185;  
        location ^~ /websocket {  
            proxy_pass http://10.6.3.46:8001;  
            proxy_http_version 1.1;  
            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_read_timeout 120s;  
            proxy_set_header Upgrade $http_upgrade;  
            proxy_set_header Connection $connection_upgrade;  
        }  
    }  
}

可能出现的问题

  • 同一个网关出来的 IP 可能会重复,所以如果我想要做一个具体的指定连接的WebSocket IP集合中,key 必须是 mac 地址 value 是 `连接的对象信息
  • 能指定发消息的需求 

到此这篇关于Nginx 配置 WebSocket 代理的文章就介绍到这了,更多相关Nginx WebSocket 代理内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_62636014/article/details/137211771

延伸 · 阅读

精彩推荐
  • NginxNginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小内存VPS服务器上的配置优化

    Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小内存VPS服务器上的配

    VPS(全称Virtual Private Server)是利用最新虚拟化技术在一台物理服务器上创建多个相互隔离的虚拟私有主机。它们以最大化的效率共享硬件、软件许可证以及...

    nginx教程网5122020-11-19
  • Nginx基于Xen的VPS ubuntu+nginx+php安装教程

    基于Xen的VPS ubuntu+nginx+php安装教程

    跟踪vps已经很久了,但是因为需要特殊端口开服务,所以符合条件的多为Xen平台的vps。众多比较之后选择了vpslink在西雅图机房,速度还不错。 ...

    服务器之家3382019-10-08
  • Nginx阿里云Linux系统Nginx配置多个域名的方法详解

    阿里云Linux系统Nginx配置多个域名的方法详解

    本篇文章主要介绍了阿里云Linux系统Nginx配置多个域名的方法详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    @巫师Denni3852019-11-23
  • NginxNginx配置之实现多台服务器负载均衡

    Nginx配置之实现多台服务器负载均衡

    这篇文章主要介绍了Nginx配置之实现多台服务器负载均衡,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    java构架师4612021-08-19
  • Nginx详解Nginx之Location配置(Location匹配顺序)

    详解Nginx之Location配置(Location匹配顺序)

    这篇文章主要介绍了详解Nginx之Location配置(Location匹配顺序),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    RobertoHuang6712020-01-20
  • Nginxnginx编译安装及常用参数详解

    nginx编译安装及常用参数详解

    这篇文章主要介绍了nginx编译安装及常用参数详解,一种是基于ansible role实现编译安装nginx以及编译安装参数详解,本文给大家介绍的非常详细,对大家的学...

    焱黎9622023-01-13
  • NginxNginx的完整配置详解及实例代码

    Nginx的完整配置详解及实例代码

    这篇文章主要介绍了Nginx的完整配置详解及实例代码的相关资料,需要的朋友可以参考下 ...

    lqh8302019-11-25
  • NginxMac M1 Nginx 配置多站点的实现

    Mac M1 Nginx 配置多站点的实现

    这篇文章主要介绍了Mac M1 Nginx 配置多站点的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    老掌8812021-05-06