Nginx实现websocket负载均衡

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xy_cpp/article/details/79033958

目录

Nginx实现websocket负载均衡... 1

软件版本:... 1

实验过程:... 1

Nginx参考代码(nginx.conf):1

参考资料:... 3

注意事项:... 3

 

Nginx实现websocket负载均衡

软件版本:

实验平台:windows

Nginx版本:nginx-1.12.2

Tomcat: apache-tomcat-8.0.47-windows-x64

 

实验过程:

我在公司的ip:192.168.3.59的电脑的8080端口部署了Nginx服务器

并将其收到的请求以负载均衡的方式分发到以下两台服务器

  server 192.168.3.59:8081 

  server 192.168.3.18:8080 

经过试验可以看到两台服务器可以负载均衡处理客户端传来的链接请求。

 

Nginx参考代码(nginx.conf):

events {

   worker_connections  1024;

}

 

 

http {

   include       mime.types;

   default_type application/octet-stream;

 

   #log_format  main  '$remote_addr - $remote_user [$time_local]"$request" '

   #                  '$status$body_bytes_sent "$http_referer" '

   #                 '"$http_user_agent" "$http_x_forwarded_for"';

 

   #access_log  logs/access.log  main;

 

   sendfile        on;

   #tcp_nopush     on;

 

   #keepalive_timeout  0;

   keepalive_timeout  65;

 

   #gzip  on;

 

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           root   html;

           index  index.html index.htm;

       }

 

 

 

       #error_page  404              /404.html;

 

       # redirect server error pages to the static page /50x.html

       #

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   html;

       }

 

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80

       #

       #location ~ \.php$ {

       #    proxy_pass   http://127.0.0.1;

       #}

 

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

       #

       #location ~ \.php$ {

        #   root           html;

       #    fastcgi_pass   127.0.0.1:9000;

       #    fastcgi_index  index.php;

       #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

       #    include        fastcgi_params;

       #}

 

       # deny access to .htaccess files, if Apache's document root

       # concurs with nginx's one

       #

       #location ~ /\.ht {

       #    deny  all;

       #}

    }

 

 

 

   upstream tomcatserver1 {

   server 192.168.3.59:8081 weight=1;

    server 192.168.3.18:8080  weight=1;

    }

 

   server {

       listen       8080;

       server_name  192.168.3.59;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           proxy_pass http://tomcatserver1;

           index  index.html index.htm;

           proxy_http_version 1.1;

           proxy_set_header Upgrade $http_upgrade;

           proxy_set_header Connection "upgrade";

       }

    }

}

 

参考资料:

【1】https://www.bilibili.com/video/av14413785/index_4.html

【2】http://www.jb51.net/article/112183.htm

【3】http://blog.csdn.net/xluren/article/details/16951247

注意事项:

1.      注意nginx的代码编写时对于空格字符使用有严格的限制。

2.      由于nginx对windows支持并不好,结束nginx服务请使用命令行,否则会出现丢失文件的错误。

展开阅读全文

没有更多推荐了,返回首页