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訪問的實(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 跳出來了很多路徑,但是我不太明白每個(gè)路徑是什么意思,就仔細(xì)去看了看,然后發(fā)現(xiàn)了一個(gè)路徑 /usr/share/man/man8/ 這個(gè)目錄,下面一般都是手冊(cè)路徑,在這里面可以看很多軟件的基本指令操作 可使用指令 man nginx 來查看 nginx.8.gz 手冊(cè)。
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基本身份驗(yàn)證的情況下,遠(yuǎn)程用戶的用戶名。$time_local: 本地時(shí)間的訪問時(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)求通過代理服務(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 {} 里面就解決問題了
成功解決問題–使用 Nginx 代理 WebSocket
nginx.conf具體配置如下, 實(shí)現(xiàn)的功能是將所有發(fā)往 10.6.30.185:9001 的請(qǐng)求去匹配一下 url里面有沒有 /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)也能夠推送信息過去。
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基本身份驗(yàn)證的情況下,遠(yuǎn)程用戶的用戶名。
#$time_local: 本地時(shí)間的訪問時(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)求通過代理服務(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)的問題
- 同一個(gè)網(wǎng)關(guān)出來的 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編譯安裝出現(xiàn)的常見錯(cuò)誤及解決方法
這篇文章給大家介紹了nginx在編譯安裝過程中容易出現(xiàn)的常見錯(cuò)誤以及解決方法,文中有詳細(xì)的代碼講解,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08
Nginx?Socket代理的實(shí)現(xiàn)方法
Nginx的socket代理通常指的是Nginx通過stream模塊來處理非HTTP的?TCP?流量,本文就來介紹一下Nginx?Socket代理的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Nginx動(dòng)態(tài)配置upstream的使用小結(jié)
本文深入探討了Nginx動(dòng)態(tài)配置upstream的多種方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
解決Nginx安裝完成,沒有sbin目錄如何啟動(dòng)的問題
用戶安裝Nginx后因未生成sbin目錄導(dǎo)致啟動(dòng)失敗,嘗試多種方法無效,最終發(fā)現(xiàn)需正確指定安裝路徑,進(jìn)入/usr/local/nginx/sbin目錄執(zhí)行./nginx啟動(dòng),或通過服務(wù)腳本啟動(dòng)2025-09-09
Nginx的auth_request模塊的應(yīng)用小結(jié)
Nginx的auth_request模塊通過子請(qǐng)求實(shí)現(xiàn)動(dòng)態(tài)鑒權(quán),支持JWT驗(yàn)證、API鑒權(quán)等場(chǎng)景,本文就來詳細(xì)的介紹auth_request模塊的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2026-04-04

