当前位置:首页 > nginx > 正文内容

Nginx反向代理websocket配置实例

canca9年前 (2016-07-20)nginx497
这篇文章主要介绍了Nginx反向代理websocket配置实例,本文是项目需求配置成功后的总结,需要的朋友可以参考下

最近有一个需求,就是需要使用 nginx 反向代理 websocket,经过查找一番资料,目前已经测试通过,本文只做一个记录

复制代码代码如下:

注: 看官方文档说 Nginx 在 1.3 以后的版本才支持 websocket 反向代理,所以要想使用支持 websocket 的功能,必须升级到 1.3 以后的版本,因此我这边是下载的 Tengine 的最新版本测试的

1.下载 tengine 最近的源码

复制代码代码如下:

wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz

2.安装基础的依赖

复制代码代码如下:

yum -y install pcre*
yum -y install zlib*
yum -y install openssl*

3.解压编译安装

复制代码代码如下:

tar -zxvf tengine-2.0.3.tar.gz cd tengine-2.0.3 ./configure --prefix=安装目录 make sudo make install

nginx.conf 的配置如下:

复制代码代码如下:

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

error_log  logs/error.log;

pid        logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be 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 需要加下这个
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

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

}

test.com 的配置文件内容:

复制代码代码如下:

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";
    }

}

解析 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。个人的理解!

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/46.html

标签: nginx
分享给朋友:

“Nginx反向代理websocket配置实例” 的相关文章

nginx 反向代理时丢失端口的解决方案,443端口变80

今天,配置nginx反向代理时遇到一个问题,当设置nginx监听80端口时转发请求没有问题。但一旦设置为监听其他端口,就一直跳转不正常;如,访问欢迎页面时应该是重定向到登录页面,在这个重定向的过程中端口丢失了。    这里给出一个简短的解决方案,修改nginx的配置文件。一、配置...

nginx if判断&&和||写法

今天在写nginx配置的时候需要用到if判断的&&,但是查了一下文档if并不支持&&的写法,现在将方法记录一下①nginx if判断&&(并且)写法,即:采用变量叠加的方法实现set $allowphp ''; i...

Nginx 之 ip_hash 问题

起因公司项目上用到了Asp.Net Core SignalR,由于SignalR需要长链接,所以在进行水平扩展的时,不仅需要Redis做底板,还需要将同一个链接的请求打到同一台机器上;作为一个资深面相百度编程的Coder,第一个想到的当然是nginx的ip_hash了,毕竟在session横行的年代...

Nginx 获取自定义请求header头和URL参数

一、获取 header 请求头在 ngx_lua 中访问 Nginx 内置变量 ngx.var.http_HEADER 即可获得请求头HEADER的内容。在 nginx配置中,通过$http_HEADER 即可获得请求头HEADER的内容。案例:$.ajax({ ....... headers:...

详解nginx的root与alias

详解nginx的root与alias

nginx版本: 1.18.01. 结论location命中后如果是root,会把请求url的 ip/域名+port替换为root指定的目录,访问资源如果是alias,会把请求url的ip/域名+port+匹配到的路径替换为alias指定的目录,访问资源2. 详解root2.1 基本用法以请求htt...

会话丢失-NGINX配置之underscores_in_headers

1.描述 问题 NGINX代理某个web服务时,单机情况下也出现不停的要求认证的情况 初步分析 去掉NGINX代理,直接访问服务,未出现上述情况; 进一步分析: 查看经过NGINX...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。