Nginx?配置?WebSocket?代理的操作過(guò)程
Nginx 配置 WebSocket 代理
Nginx
官方文檔網(wǎng)址 nginx documentation
... http:{ ... server{ ... # WebSocket代理 location /wsUrl/ { rewrite ^/wsUrl/(.*)$ /$1 break; #攔截標(biāo)識(shí)去除 proxy_pass http://192.168.100.20:8080; #這里是http不是ws,不用懷疑,代理的ip和port寫ws訪問(wèn)的實(shí)際地址 proxy_http_version 1.1; #這里必須使用http 1.1 #下面兩個(gè)必須設(shè)置,請(qǐng)求頭設(shè)置為ws請(qǐng)求方式 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 查看安裝文件命令手冊(cè)
[!起因]
我使用指令whereis nginx
跳出來(lái)了很多路徑,但是我不太明白每個(gè)路徑是什么意思,就仔細(xì)去看了看,然后發(fā)現(xiàn)了一個(gè)路徑/usr/share/man/man8/
這個(gè)目錄,下面一般都是手冊(cè)路徑,在這里面可以看很多軟件的基本指令操作 可使用指令man nginx
來(lái)查看nginx.8.gz
手冊(cè)。
Nginx 日志配置方案
可以參考 Nginx訪問(wèn)日志(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基本身份驗(yàn)證的情況下,遠(yuǎn)程用戶的用戶名。
- $time_local: 本地時(shí)間的訪問(wèn)時(shí)間。
- $request: 客戶端請(qǐng)求的內(nèi)容。
- $status: 服務(wù)器響應(yīng)的HTTP狀態(tài)碼。
- $body_bytes_sent: 發(fā)送給客戶端的字節(jié)數(shù),不包括響應(yīng)頭的大小。
- $http_referer: 引用頁(yè)面的URL。
- $http_user_agent: 客戶端的User-Agent字符串,標(biāo)識(shí)客戶端的瀏覽器和操作系統(tǒng)等信息。
- $http_x_forwarded_for: X-Forwarded-For 頭,用于標(biāo)識(shí)原始客戶端的IP地址,當(dāng)請(qǐng)求通過(guò)代理服務(wù)器時(shí)使用。
- $upstream_addr: 后端(上游)服務(wù)器的IP地址。
- $upstream_response_time: 從后端服務(wù)器接收響應(yīng)的時(shí)間。
- $request_time: 客戶端發(fā)起請(qǐng)求到收到響應(yīng)的總時(shí)間。
[!錯(cuò)誤]
配置nginx
日志的時(shí)候,由于不知道要將log_format main
配置放在哪里,就放在了最外層,導(dǎo)致錯(cuò)誤提示nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/nginx.conf:14
后序解決是 將log_format main
放在http {}
里面就解決問(wèn)題了
成功解決問(wèn)題–使用 Nginx 代理 WebSocket
nginx.conf
具體配置如下, 實(shí)現(xiàn)的功能是將所有發(fā)往 10.6.30.185:9001
的請(qǐng)求去匹配一下 url
里面有沒(méi)有 /websocket
這一級(jí),如果有就使用 WebSocket
請(qǐng)求發(fā)往 10.6.3.46:8001
,后序使用了6臺(tái)服務(wù)器進(jìn)行了一個(gè) nginx
代理 WebSocket
操作,都能夠在后臺(tái)讀取到信息,同時(shí),后臺(tái)也能夠推送信息過(guò)去。
user nobody; worker_processes 6; #nginx 開(kāi)啟多核設(shè)置,目前185的機(jī)子,都是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; #進(jìn)程文件 pid /var/run/nginx.pid; worker_rlimit_nofile 1024; events { use epoll; # 修改這里 worker_connections 1024; } # 設(shè)置http 服務(wù)器 http { include mime.types; #文件擴(kuò)展名與文件類型映射表 default_type application/octet-stream; #默認(rèn)文件類型 charset utf-8; #默認(rèn)編碼 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基本身份驗(yàn)證的情況下,遠(yuǎn)程用戶的用戶名。 #$time_local: 本地時(shí)間的訪問(wèn)時(shí)間。 #$request: 客戶端請(qǐng)求的內(nèi)容。 #$status: 服務(wù)器響應(yīng)的HTTP狀態(tài)碼。 #$body_bytes_sent: 發(fā)送給客戶端的字節(jié)數(shù),不包括響應(yīng)頭的大小。 #$http_referer: 引用頁(yè)面的URL。 #$http_user_agent: 客戶端的User-Agent字符串,標(biāo)識(shí)客戶端的瀏覽器和操作系統(tǒng)等信息。 #$http_x_forwarded_for: X-Forwarded-For 頭,用于標(biāo)識(shí)原始客戶端的IP地址,當(dāng)請(qǐng)求通過(guò)代理服務(wù)器時(shí)使用。 #$upstream_addr: 后端(上游)服務(wù)器的IP地址。 #$upstream_response_time: 從后端服務(wù)器接收響應(yīng)的時(shí)間。 #$request_time: 客戶端發(fā)起請(qǐng)求到收到響應(yīng)的總時(shí)間。 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; } } }
可能出現(xiàn)的問(wèn)題
- 同一個(gè)網(wǎng)關(guān)出來(lái)的 IP 可能會(huì)重復(fù),所以如果我想要做一個(gè)具體的指定連接的
WebSocket IP
集合中,key
必須是mac
地址value
是 `連接的對(duì)象信息 - 能指定發(fā)消息的需求
到此這篇關(guān)于Nginx 配置 WebSocket 代理的文章就介紹到這了,更多相關(guān)Nginx WebSocket 代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx配置Https安全認(rèn)證的實(shí)現(xiàn)
為了保障應(yīng)用的安全性,我們?cè)诩軜?gòu)網(wǎng)絡(luò)層的時(shí)候需要采用HTTPS協(xié)議。本文介紹了Nginx配置Https安全認(rèn)證的實(shí)現(xiàn),分享給大家,感興趣的可以了解一下2021-05-05nginx實(shí)現(xiàn)單主機(jī)多域名映射的項(xiàng)目實(shí)踐
本文主要介紹了nginx實(shí)現(xiàn)單主機(jī)多域名映射的項(xiàng)目實(shí)踐,配置不同的子域名映射到不同的內(nèi)部服務(wù)端口,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02簡(jiǎn)單了解Nginx七層負(fù)載均衡的幾種調(diào)度算法
這篇文章主要介紹了簡(jiǎn)單了解Nginx七層負(fù)載均衡的幾種調(diào)度算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Nginx Proxy 代理測(cè)試的實(shí)現(xiàn)
本文主要介紹了Nginx Proxy 代理測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02在Debian11上安裝Openresty服務(wù)(Nginx+Lua)的詳細(xì)教程
OpenResty 是一個(gè)基于 Nginx 與 Lua 的高性能 Web 平臺(tái),其內(nèi)部集成了大量精良的 Lua 庫(kù)、第三方模塊以及大多數(shù)的依賴項(xiàng),這篇文章主要介紹了在Debian11上安裝Openresty服務(wù)(Nginx+Lua)?,需要的朋友可以參考下2022-10-10