Recently, there is a need to use Nginx reverse proxy websocket, after looking for some information, has been tested through, this article only make a record
Copy Code code as follows:
Note: Read the Official document said Nginx in the 1.3 version only support WebSocket reverse proxy, so to use the support WebSocket features, must upgrade to the later version of 1.3, so I am the download of the latest version of the Tengine test
1. Download the tengine recent source code
Copy Code code as follows:
wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz
2. Reliance on the installation base Package
Copy Code code as follows:
Yum-y Install pcre*
Yum-y Install zlib*
Yum-y Install openssl*
3. Decompression Compile Installation
Copy Code code as follows:
TAR-ZXVF tengine-2.0.3.tar.gz cd tengine-2.0.3./configure--prefix= installation directory make sudo make install
The nginx.conf configuration is as follows:
Copy Code code as follows:
User apps apps;
Worker_processes 4; # This is because I am using the virtual machine, so the configuration of 4, in addition Tengine can automatically set the number of CPUs according to CPU and bound CPU affinity
# worker_processes Auto
# worker_cpu_affinity Auto
Error_log Logs/error.log;
PID Logs/nginx.pid;
#Specifies the value for maximum file descriptors the can is opened by this process.
Worker_rlimit_nofile 65535;
Events {
Use Epoll;
Worker_connections 65535;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 4k;
Large_client_header_buffers 4 32k;
Client_max_body_size 80m;
Sendfile on;
Tcp_nopush on;
Client_body_timeout 5;
Client_header_timeout 5;
Keepalive_timeout 5;
Send_timeout 5;
Open_file_cache max=65535 inactive=20s;
Open_file_cache_valid 30s;
Open_file_cache_min_uses 1;
Tcp_nodelay on;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css Application/xml;
gzip_vary on;
proxy_temp_path /dev/shm/temp;
proxy_cache_path /dev/shm/cache levels=2:2:2 keys_zone=cache_go:200m INACTIVE=5D max_size=7g;
Log_format log_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 need to add this.
Map $http _upgrade $connection _upgrade {
Default upgrade;
' Close;
}
include/home/apps/tengine/conf/test.com;
}
test.com Configuration file Contents:
Copy Code code as follows:
Upstream test.com {
Server 192.168.1.5:9000;
}
server {
Listen 80;
server_name test.com;
#charset Koi8-r;
#access_log Logs/host.access.log Main;
Location ^~/websocket {
Proxy_pass http://test.com;
Proxy_redirect off;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header Host $host;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_http_version 1.1;
Proxy_set_header Upgrade $http _upgrade;
Proxy_set_header Connection "Upgrade";
}
}
Parse map directives
The map $http _upgrade in the above nginx.conf configuration $connection The role of _upgrade, reference http://www.ttlsa.com/nginx/using-nginx-map-method/
The effect is to construct a value that changes $connection _upgrade based on the value $http _upgrade in the client request, that is, to create a new variable based on the value of the variable $http _upgrade $connection _upgrade, the rule created is {} What's inside, see Configuration:
Copy Code code as follows:
Map $http _upgrade $connection _upgrade {
Default upgrade;
' Close;
}
The rules do not match, so using the default, that is, the value of the $connection _upgrade will always be upgrade. Then, if $http _upgrade is an empty string, the value will be close. A personal understanding!