Nginx配置WebSocket代理的示例代碼
Nginx 官方文檔網(wǎng)址 nginx documentation
...
http:{
...
server{
...
# WebSocket代理
location /wsUrl/ {
rewrite ^/wsUrl/(.*)$ /$1 break; #攔截標(biāo)識去除
proxy_pass http://192.168.100.20:8080; #這里是http不是ws,不用懷疑,代理的ip和port寫ws訪問的實際地址
proxy_http_version 1.1; #這里必須使用http 1.1
#下面兩個必須設(shè)置,請求頭設(shè)置為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 跳出來了很多路徑,但是我不太明白每個路徑是什么意思,就仔細(xì)去看了看,然后發(fā)現(xiàn)了一個路徑 /usr/share/man/man8/ 這個目錄,下面一般都是手冊路徑,在這里面可以看很多軟件的基本指令操作 可使用指令 man nginx 來查看 nginx.8.gz 手冊。
Nginx 日志配置方案
可以參考 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基本身份驗證的情況下,遠(yuǎn)程用戶的用戶名。$time_local: 本地時間的訪問時間。$request: 客戶端請求的內(nèi)容。$status: 服務(wù)器響應(yīng)的HTTP狀態(tài)碼。$body_bytes_sent: 發(fā)送給客戶端的字節(jié)數(shù),不包括響應(yīng)頭的大小。$http_referer: 引用頁面的URL。$http_user_agent: 客戶端的User-Agent字符串,標(biāo)識客戶端的瀏覽器和操作系統(tǒng)等信息。$http_x_forwarded_for: X-Forwarded-For 頭,用于標(biāo)識原始客戶端的IP地址,當(dāng)請求通過代理服務(wù)器時使用。$upstream_addr: 后端(上游)服務(wù)器的IP地址。$upstream_response_time: 從后端服務(wù)器接收響應(yīng)的時間。$request_time: 客戶端發(fā)起請求到收到響應(yīng)的總時間。
[!錯誤]
配置 nginx 日志的時候,由于不知道要將 log_format main 配置放在哪里,就放在了最外層,導(dǎo)致錯誤提示 nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/nginx.conf:14后序解決是 將 log_format main 放在 http {} 里面就解決問題了
成功解決問題–使用 Nginx 代理 WebSocket
nginx.conf具體配置如下, 實現(xiàn)的功能是將所有發(fā)往 10.6.30.185:9001 的請求去匹配一下 url里面有沒有 /websocket 這一級,如果有就使用 WebSocket 請求發(fā)往 10.6.3.46:8001 ,后序使用了6臺服務(wù)器進(jìn)行了一個 nginx 代理 WebSocket 操作,都能夠在后臺讀取到信息,同時,后臺也能夠推送信息過去。
user nobody;
worker_processes 6;
#nginx 開啟多核設(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基本身份驗證的情況下,遠(yuǎn)程用戶的用戶名。
#$time_local: 本地時間的訪問時間。
#$request: 客戶端請求的內(nèi)容。
#$status: 服務(wù)器響應(yīng)的HTTP狀態(tài)碼。
#$body_bytes_sent: 發(fā)送給客戶端的字節(jié)數(shù),不包括響應(yīng)頭的大小。
#$http_referer: 引用頁面的URL。
#$http_user_agent: 客戶端的User-Agent字符串,標(biāo)識客戶端的瀏覽器和操作系統(tǒng)等信息。
#$http_x_forwarded_for: X-Forwarded-For 頭,用于標(biāo)識原始客戶端的IP地址,當(dāng)請求通過代理服務(wù)器時使用。
#$upstream_addr: 后端(上游)服務(wù)器的IP地址。
#$upstream_response_time: 從后端服務(wù)器接收響應(yīng)的時間。
#$request_time: 客戶端發(fā)起請求到收到響應(yīng)的總時間。
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ǎng)關(guān)出來的 IP 可能會重復(fù),所以如果我想要做一個具體的指定連接的
WebSocket IP集合中,key必須是mac地址value是 `連接的對象信息 - 能指定發(fā)消息的需求
到此這篇關(guān)于Nginx配置WebSocket代理的示例代碼的文章就介紹到這了,更多相關(guān)Nginx WebSocket代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx(自定義/預(yù)定義)變量,alias虛擬目錄解讀
這篇文章主要介紹了Nginx(自定義/預(yù)定義)變量,alias虛擬目錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-07-07
Nginx反向代理在Web應(yīng)用中的實戰(zhàn)分享
本文將介紹Nginx反向代理的基本原理和配置,以及如何利用Nginx實現(xiàn)高可用性和故障轉(zhuǎn)移,最后,我們將探討如何監(jiān)控Nginx反向代理的性能并進(jìn)行日志分析,需要的朋友可以參考下2024-08-08

