Nginx 配置 WebSocket 代理

Nginx 配置 WebSocket 代理

文章目录

  • [Nginx 配置 WebSocket 代理](#Nginx 配置 WebSocket 代理)
    • [Linux 查看安装文件命令手册](#Linux 查看安装文件命令手册)
    • [Nginx 日志配置方案](#Nginx 日志配置方案)
    • [成功解决问题--使用 Nginx 代理 WebSocket](#成功解决问题--使用 Nginx 代理 WebSocket)
    • 可能出现的问题

Nginx 官方文档网址 nginx documentation

nginx 复制代码
...
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";
    }
    ...
  }
  ...
}

官方文档代理样例

nginx 复制代码
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配置-CSDN博客

一般使用 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 操作,都能够在后台读取到信息,同时,后台也能够推送信息过去。

nginx 复制代码
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 是 `连接的对象信息
  • 能指定发消息的需求
相关推荐
Pythonliu72 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我3 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
小O_好好学4 小时前
CentOS 7文件系统
linux·运维·centos
哲伦贼稳妥5 小时前
一天认识一个硬件之机房地板
运维·网络·经验分享·其他
苹果醋35 小时前
快速玩转 Mixtral 8x7B MOE大模型!阿里云机器学习 PAI 推出最佳实践
spring boot·nginx·毕业设计·layui·课程设计
john_hjy5 小时前
11. 异步编程
运维·服务器·javascript
x晕x5 小时前
Linux dlsym符号查找疑惑分析
linux·运维·服务器
活跃的煤矿打工人5 小时前
【星海saul随笔】Ubuntu基础知识
linux·运维·ubuntu
tangdou3690986556 小时前
两种方案手把手教你多种服务器使用tinyproxy搭建http代理
运维·后端·自动化运维
北京智和信通6 小时前
云平台和虚拟化智慧运维监控,全面提升故障感知与处置能力
运维·虚拟化·云平台·虚拟机监控