Nginx Reverse proxy websocket Configuration instance

Source: Internet
Author: User
Tags install openssl nginx reverse proxy

Recently, there is a need to use Nginx reverse proxy websocket, after looking for information, has been tested, this article only make a record

Copy CodeThe code is as follows:
Note: Look at official documents said Nginx in the version after 1.3 only support websocket reverse proxy, so to use the support WebSocket features, must be upgraded to 1.3 later version, so I am downloading the latest version of the Tengine test

1. Download Tengine's latest source code

Copy CodeThe code is as follows:
wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz

2. Dependencies of the installation base Package

Copy CodeThe code is as follows:
Yum-y Install pcre*
Yum-y Install zlib*
Yum-y Install openssl*

3. Unzip the build installation

Copy CodeThe code is as follows:
TAR-ZXVF tengine-2.0.3.tar.gz cd tengine-2.0.3./configure--prefix= installation directory make sudo make install

The configuration of the nginx.conf is as follows:

Copy CodeThe code is as follows:
User apps apps;
Worker_processes 4; # This is because I am using the virtual machine, so the configuration of 4, another tengine can automatically set the number of processes based on the number of CPUs and bind 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 CodeThe code is 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";
}

}

Parsing map directives

The map in the nginx.conf configuration above $http _upgrade $connection _upgrade, refer to http://www.ttlsa.com/nginx/using-nginx-map-method/

This role is mainly based on the value $http _upgrade in the client request, to construct a change $connection _upgrade value, that is, according to the variable $http _upgrade value to create a new variable $connection _upgrade, the rule created is {} What's inside, see Configuration:

Copy CodeThe code is 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, that value will be close. A personal understanding!

Nginx Reverse proxy websocket Configuration instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.