Nginx反向代理websocket配置实例
最近有一个需求,就是需要使用nginx反向代理websocket,经过查找一番资料,目前已经测试通过,本文只做一个记录
注:看官方文档说Nginx在1.3以后的版本才支持websocket反向代理,所以要想使用支持websocket的功能,必须升级到1.3以后的版本,因此我这边是下载的Tengine的最新版本测试的
1.下载tengine最近的源码
wgethttp://tengine.taobao.org/download/tengine-2.0.3.tar.gz
2.安装基础的依赖包
yum-yinstallpcre* yum-yinstallzlib* yum-yinstallopenssl*
3.解压编译安装
tar-zxvftengine-2.0.3.tar.gzcdtengine-2.0.3./configure--prefix=安装目录makesudomakeinstall
nginx.conf的配置如下:
userappsapps; worker_processes 4;#这个由于我是用的虚拟机,所以配置的4,另外tengine可以自动根据CPU数目设置进程个数和绑定CPU亲缘性 #worker_processesauto #worker_cpu_affinityauto
error_log logs/error.log;
pid logs/nginx.pid;
#Specifiesthevalueformaximumfiledescriptorsthatcanbeopenedbythisprocess. worker_rlimit_nofile65535;
events{ useepoll; worker_connections 65535; }
#loadmodulescompiledasDynamicSharedObject(DSO) # #dso{ # loadngx_http_fastcgi_module.so; # loadngx_http_rewrite_module.so; #}
http{ include mime.types; default_type application/octet-stream;
server_names_hash_bucket_size128; client_header_buffer_size4k; large_client_header_buffers432k; client_max_body_size80m;
sendfileon; tcp_nopush on;
client_body_timeout 5; client_header_timeout5; keepalive_timeout 5; send_timeout 5;
open_file_cachemax=65535inactive=20s; open_file_cache_valid30s; open_file_cache_min_uses1;
tcp_nodelayon;
fastcgi_connect_timeout300; fastcgi_send_timeout300; fastcgi_read_timeout300; fastcgi_buffer_size64k; fastcgi_buffers464k; fastcgi_busy_buffers_size128k; fastcgi_temp_file_write_size128k;
client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 464k; proxy_busy_buffers_size128k; proxy_temp_file_write_size128k;
gzipon; gzip_min_length 1k; gzip_buffers 416k; gzip_http_version1.0; gzip_comp_level2; gzip_types text/plainapplication/x-javascripttext/cssapplication/xml; gzip_varyon; proxy_temp_path /dev/shm/temp; proxy_cache_path /dev/shm/cachelevels=2:2:2 keys_zone=cache_go:200minactive=5dmax_size=7g;
log_formatlog_access '$remote_addr-$remote_user[$time_local]"$request""$request_time""$upstream_response_time"' '$status$body_bytes_sent"$http_referer"' '"$http_user_agent"$http_x_forwarded_for$host$hostname';
#websocket需要加下这个 map$http_upgrade$connection_upgrade{ defaultupgrade; '' close; }
include/home/apps/tengine/conf/test.com;
}