赞
踩
利用 ip + prot 连接websocket服务器,缺点:
1. 暴露服务器地址和端口;
2. 只能单节点部署,无法做集群;
3. 诸多缺点。
#配置upstream
- upstream my-server {
- least_conn;
-
- # 微服务网关地址
- server 192.0.1.22:8888 weight=5 max_fails=2 fail_timeout=50;
- }
#配置server
- server
- {
- listen 80;
- charset utf-8;
- server_name sokcet.com;
- add_header Cache-Control max-age=43200;
- location / {
-
- # web页面路径配置
- root /home/webdata/test;
- index index.html index.htm;
- }
- location /test-server {
- root html;
- index index.html index.htm;
- proxy_pass http://my-server/test-server;
- }
- location /socket {
- proxy_pass http://192.1.1.20:8081/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Host $Server_name;
- proxy_connect_timeout 12000s;
- proxy_read_timeout 36000s;
- proxy_send_timeout 36000s;
- }
- }
配置完后,回到bin目录,重启Nginx:
nginx -s reload
域名连接websocket,连接地址:
ws://socket.com/socket/demo/test
1. socket.com是上面配置的server_name;
2. /socket 是location /socket的配置路径;
3. /demo/test 是websocket服务端接口地址。
以上配置亲测有效!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。