nginx配置WebSocket参数wss连接

目录

一、原文连接

[二、 配置参数](#二、 配置参数)

三、实践

四、重启nginx

五、连接websocket


一、原文连接

nginx配置websocket支持wss-腾讯云开发者社区-腾讯云

二、 配置参数

map $http_upgrade $connection_upgrade {  
    default upgrade;  
    '' close;  
}  
upstream websocket {  
    server 128.190.82.105:8888;  
}  
server {  
    listen 8888;  
    server_name proxy.hello.com;
    ssl on;
    ssl_certificate /etc/nginx/ssl/hello.com_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/hello.com.key;
    ssl_session_timeout 20m;
    ssl_verify_client off;
    location / {  
        proxy_pass http://websocket;  
        proxy_http_version 1.1;  
        proxy_set_header Upgrade $http_upgrade;  
        proxy_set_header Connection "Upgrade";  
    }  
}

三、实践

1.websocket服务

2、在nginx.conf配置文件中配置:

XML 复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    # http段增加
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    upstream websocket {  
    server 112.xxx.xxx.225:85;  
    }
    
    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;
}

3、在default.conf中配置

XML 复制代码
server {
    listen 80;
    listen 443 ssl;
    server_name  www.xxxxxx.net;
    # access_log  conf.d/logs/access.log;
    # error_log  conf.d/logs/error.log;

    ssl_certificate /etc/nginx/ssl/educhat.top.pem; 
    ssl_certificate_key /etc/nginx/ssl/educhat.top.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
    ssl_prefer_server_ciphers on;
    ssl_session_timeout 5m;
    

    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location ^~/api {
        proxy_pass  http://112.xxx.xxx.255:8088/api/;

        proxy_redirect     off;
        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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
        client_max_body_size 50m;
    }
    
    location ^~/websocket {
        proxy_pass http://websocket;  
        
	    proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade; 

        
        proxy_redirect     off;
        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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
        client_max_body_size 50m;
    }
}

四、重启nginx

XML 复制代码
docker restart nginx
XML 复制代码
docker exec -it nginx bash
XML 复制代码
nginx -s reload

五、连接websocket

XML 复制代码
wss://xxxx.xxxx/websocket
相关推荐
迷茫运维路15 分钟前
Jenkins声明式Pipeline流水线语法示例
运维·pipeline·jenkins·声明式
Zfox_32 分钟前
【Linux】进程信号全攻略(二)
linux·运维·c语言·c++
安於宿命37 分钟前
【Linux】简易版shell
linux·运维·服务器
追梦不止~44 分钟前
Docker常用命令+详解
运维·docker·容器
黑龙江亿林等保1 小时前
深入探索哈尔滨二级等保下的负载均衡SLB及其核心算法
运维·算法·负载均衡
黄小耶@1 小时前
linux常见命令
linux·运维·服务器
叫我龙翔1 小时前
【计网】实现reactor反应堆模型 --- 框架搭建
linux·运维·网络
古驿幽情1 小时前
CentOS AppStream 8 手动更新 yum源
linux·运维·centos·yum
BillKu1 小时前
Linux(CentOS)安装 Nginx
linux·运维·nginx·centos
BillKu1 小时前
Linux(CentOS)yum update -y 事故
linux·运维·centos