【Nginx配置 Websocket代理(ws、wss)】
【Nginx配置Websocket代理(ws、wss)】
·
Websocket代理配置(ws、wss):
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 172.20.2.1:58588;
}
server {
listen 80;
server_name 172.20.2.1;
#首页index代理 http
location / {
root /var/www/sass-ui;
index index.html index.html;
}
#websocket代理 ws
location /socketurl {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "connection_upgrade";
}
}
server {
listen 443 ssl;
server_name est.com;
ssl_certificate /opt/nginx/ssl/STAR_est_com.crt;
ssl_certificate_key /opt/nginx/ssl/STAR_est_com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#首页index代理 https
location / {
root /var/www/sass-ui-out;
index index.html;
}
#websocket代理 ws
location /socketurl {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "connection_upgrade";
}
}
}
wss协议:Wesocket + SSL ,就是Websocket协议加上SSL,类似于http和https(http + ssl)
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
#58588:服务端口号
server 172.20.2.1:58588;
}
http|https升级为websocket连接(ws|wss):
#代理websocket最重要配置,将http|https升级为websocket连接(ws|wss)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "connection_upgrade";
更多推荐
已为社区贡献2条内容
所有评论(0)