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 跳出來了很多路徑,但是我不太明白每個路徑是什么意思,就仔細去看了看,然后發(fā)現了一個路徑 /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基本身份驗證的情況下,遠程用戶的用戶名。$time_local: 本地時間的訪問時間。$request: 客戶端請求的內容。$status: 服務器響應的HTTP狀態(tài)碼。$body_bytes_sent: 發(fā)送給客戶端的字節(jié)數,不包括響應頭的大小。$http_referer: 引用頁面的URL。$http_user_agent: 客戶端的User-Agent字符串,標識客戶端的瀏覽器和操作系統等信息。$http_x_forwarded_for: X-Forwarded-For 頭,用于標識原始客戶端的IP地址,當請求通過代理服務器時使用。$upstream_addr: 后端(上游)服務器的IP地址。$upstream_response_time: 從后端服務器接收響應的時間。$request_time: 客戶端發(fā)起請求到收到響應的總時間。
[!錯誤]
配置 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具體配置如下, 實現的功能是將所有發(fā)往 10.6.30.185:9001 的請求去匹配一下 url里面有沒有 /websocket 這一級,如果有就使用 WebSocket 請求發(fā)往 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狀態(tài)碼。
#$body_bytes_sent: 發(fā)送給客戶端的字節(jié)數,不包括響應頭的大小。
#$http_referer: 引用頁面的URL。
#$http_user_agent: 客戶端的User-Agent字符串,標識客戶端的瀏覽器和操作系統等信息。
#$http_x_forwarded_for: X-Forwarded-For 頭,用于標識原始客戶端的IP地址,當請求通過代理服務器時使用。
#$upstream_addr: 后端(上游)服務器的IP地址。
#$upstream_response_time: 從后端服務器接收響應的時間。
#$request_time: 客戶端發(fā)起請求到收到響應的總時間。
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是 `連接的對象信息 - 能指定發(fā)消息的需求
到此這篇關于Nginx配置WebSocket代理的示例代碼的文章就介紹到這了,更多相關Nginx WebSocket代理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

