Nginx配置WebSocket代理的方法

目录
文章目录隐藏
  1. Linux 查看安装文件命令手册
  2. Nginx 日志配置方案
  3. 成功解决问题–使用 Nginx 代理 WebSocket
  4. 可能出现的问题

本文关于 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 日志配置方案

一般使用 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 是 `连接的对象信息
  • 能指定发消息的需求

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » Nginx配置WebSocket代理的方法

发表回复