如何为套接字传递Nginx代理URL [英] How to pass nginx proxy url for socket

查看:92
本文介绍了如何为套接字传递Nginx代理URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用套接字,我想将nginx的套接字连接URL传递为代理URL.我这样做是

I am using socket in my application,i want to pass socket connection url from nginx as proxy url.I am doing it like that

我的套接字代码

var socket = io.connect('/explorer/socket',{
      'reconnect': true,
      'reconnection delay': 500
    });

我的nginx conf

My nginx conf

location /explorer/socket {
                proxy_pass    http://xxx.xxx.xx.xxx:3000;
        }

但是它不起作用,它正在连接到我的本地主机,但是我想连接我在nginx中定义的代理URL.那么如何在io.connect内部传递代理URL?

but it is not working,it is connecting to my localhost,but i want to connect with my proxy url which i have defined in nginx. so how can i pass proxy url inside io.connect ?

推荐答案

您必须像这样更改nginx配置

you have to change in nginx configuartion like that

location /socket.io/ {
                proxy_pass    http://xxx.xxx.xx.xxx:3000;
                 proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;

access_log off;
error_log /opt/nginx/logs/websockets.error.log;


        }

1)/socket.io/-这将告诉nginx将套接字调用传递给给定的ip

1) /socket.io/ - this will tell nginx to pass your socket call to that given ip

现在更改您的套接字代码

Now change in your socket code

var socket = io.connect('/',{
      'reconnect': true,
      'reconnection delay': 500
    });

这篇关于如何为套接字传递Nginx代理URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆