nginx反向代理websocket

 
 
 
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections 102400;
 accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
 multi_accept on;
 #use epoll;  #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport 
}
 
 
http {
  map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
  }
    include       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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 upstream ws {
     server ip:port weight=1;
 }

server {
    listen 81;
    server_name ws;
    
    ssl on;
    ssl_certificate 文件.crt;
    ssl_certificate_key 文件.key;
    ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_prefer_server_ciphers on;
    

    location /{
        #反向代理 ?9999端口,同时协议转换为http,这样服务器端代码就不需要做修改
        proxy_pass http://ws;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        #由于服务器端源码(建议大家做好大小写匹 ?)只匹配了"Upgrade"字符 ?, ?以如果这里填"upgrade"服务器端会将这条http请求当成普 ? 的请求,导致websocket握手失败
        proxy_set_header Connection "Upgrade";
        proxy_set_header Remote_addr $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 3600s;
    }
    
}

}

 

你可能感兴趣的:(nginx,websocket,服务器)