nginx.conf配置文件
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" "$http_host" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$request_time" "$ssl_protocol" "$ssl_cipher" "$http_x_forwarded_for" '
'"$upstream_addr" "$upstream_status" "$upstream_response_length" "$upstream_response_time"';
access_log ./logs/gateway_access.log main;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 10090;
location /ws-test {
proxy_pass http://127.0.0.1:10091;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
}
server {
server_name 0.0.0.0;
listen 10091;
location /ws-test {
default_type text/html;
return 200 '<html><body>Websocket test successfully!</body></html>';
}
}
}
curl命令测试
curl --include
--no-buffer
--header "Connection: Upgrade"
--header "Upgrade: websocket"
--header "Host: websocket-test.com:80"
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ=="
--header "Sec-WebSocket-Version: 13"
http://127.0.0.1:10090/ws-test/
参考:
1.nginx支持ws反向代理
2.Websocket Proxy