Nginx 反向代理 websocket

[复制链接]
1051|0
 楼主| keer_zu 发表于 2016-7-4 10:25 | 显示全部楼层 |阅读模式
最近有一个需求,就是需要使用 nginx 反向代理 websocket,经过查找一番资料,目前已经测试通过,本文只做一个记录
注: 看官方文档说 Nginx 在 1.3 以后的版本才支持 websocket 反向代理,所以要想使用支持 websocket 的功能,必须升级到 1.3 以后的版本,因此我这边是下载的 Tengine 的最新版本测试的
  • 下载 tengine 最近的源码wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz
  • 安装基础的依赖包yum -y install pcre*yum -y install zlib*yum -y install openssl*
  • 解压编译安装
    tar -zxvf tengine-2.0.3.tar.gz cd tengine-2.0.3 ./configure --prefix=安装目录 make sudo make install
nginx.conf 的配置如下:

  1. user apps apps;
  2. worker_processes  4; # 这个由于我是用的虚拟机,所以配置的 4 ,另外 tengine 可以自动根据CPU数目设置进程个数和绑定CPU亲缘性
  3. # worker_processes auto
  4. # worker_cpu_affinity auto

  5. error_log  logs/error.log;

  6. pid        logs/nginx.pid;

  7. #Specifies the value for maximum file descriptors that can be opened by this process.
  8. worker_rlimit_nofile 65535;

  9. events {
  10.     use epoll;
  11.     worker_connections  65535;
  12. }

  13. # load modules compiled as Dynamic Shared Object (DSO)
  14. #
  15. #dso {
  16. #    load ngx_http_fastcgi_module.so;
  17. #    load ngx_http_rewrite_module.so;
  18. #}

  19. http {
  20.     include       mime.types;
  21.     default_type  application/octet-stream;

  22.     server_names_hash_bucket_size 128;
  23.     client_header_buffer_size 4k;
  24.     large_client_header_buffers 4 32k;
  25.     client_max_body_size 80m;

  26.     sendfile on;
  27.     tcp_nopush     on;

  28.     client_body_timeout  5;
  29.     client_header_timeout 5;
  30.     keepalive_timeout  5;
  31.     send_timeout       5;

  32.     open_file_cache max=65535 inactive=20s;
  33.     open_file_cache_valid 30s;
  34.     open_file_cache_min_uses 1;

  35.     tcp_nodelay on;

  36.     fastcgi_connect_timeout 300;
  37.     fastcgi_send_timeout 300;
  38.     fastcgi_read_timeout 300;
  39.     fastcgi_buffer_size 64k;
  40.     fastcgi_buffers 4 64k;
  41.     fastcgi_busy_buffers_size 128k;
  42.     fastcgi_temp_file_write_size 128k;

  43.     client_body_buffer_size  512k;
  44.     proxy_connect_timeout    5;
  45.     proxy_read_timeout       60;
  46.     proxy_send_timeout       5;
  47.     proxy_buffer_size        16k;
  48.     proxy_buffers            4 64k;
  49.     proxy_busy_buffers_size 128k;
  50.     proxy_temp_file_write_size 128k;

  51.     gzip on;
  52.     gzip_min_length  1k;
  53.     gzip_buffers     4 16k;
  54.     gzip_http_version 1.0;
  55.     gzip_comp_level 2;
  56.     gzip_types       text/plain application/x-javascript text/css application/xml;
  57.     gzip_vary on;
  58.     proxy_temp_path   /dev/shm/temp;
  59.     proxy_cache_path  /dev/shm/cache levels=2:2:2   keys_zone=cache_go:200m inactive=5d max_size=7g;

  60.     log_format log_access  '$remote_addr - $remote_user [$time_local] "$request" "$request_time" "$upstream_response_time"'
  61.               '$status $body_bytes_sent "$http_referer" '
  62.               '"$http_user_agent" $http_x_forwarded_for $host $hostname' ;

  63.     #websocket 需要加下这个
  64.     map $http_upgrade $connection_upgrade {
  65.         default upgrade;
  66.         ''      close;
  67.     }

  68.     include /home/apps/tengine/conf/test.com;

  69. }

test.com 的配置文件内容:

  1. upstream test.com {
  2.    server 192.168.1.5:9000;
  3. }

  4. server {
  5.     listen       80;
  6.     server_name  test.com;

  7.     #charset koi8-r;

  8.     #access_log  logs/host.access.log  main;

  9.     location  ^~  /websocket {
  10.         proxy_pass http://test.com;

  11.         proxy_redirect    off;
  12.         proxy_set_header X-Real-IP $remote_addr;
  13.         proxy_set_header Host $host;
  14.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  15.         proxy_http_version 1.1;
  16.         proxy_set_header Upgrade $http_upgrade;
  17.         proxy_set_header Connection "upgrade";
  18.     }

  19. }

解析 map 指令上面 nginx.conf 配置中的 map $http_upgrade $connection_upgrade 的作用,参考 http://www.ttlsa.com/nginx/using-nginx-map-method/
该作用主要是根据客户端请求中 $http_upgrade 的值,来构造改变 $connection_upgrade 的值,即根据变量 $http_upgrade 的值创建新的变量 $connection_upgrade,创建的规则就是 {} 里面的东西,请见配置:
    map $http_upgrade $connection_upgrade {        default upgrade;        ''      close;    }其中的规则没有做匹配,因此使用默认的,即 $connection_upgrade 的值会一直是 upgrade。然后如果 $http_upgrade为空字符串的话,那值会是 close。

参考资料

您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1497

主题

12971

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部