我在我的应用程序中使用套接字,我想从nginx传递套接字连接url作为代理url.I正在这样做
我的套接字代码
var socket = io.connect('/explorer/socket',{ 'reconnect': true, 'reconnection delay': 500 });
我的nginx conf
location /explorer/socket { proxy_pass http://xxx.xxx.xx.xxx:3000; }
但它不工作,它连接到我的本地主机,但我想连接我的代理url,我已经在nginx中定义。 所以我怎么可以通过代理url内io.connect?
你必须改变nginx的配置
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
现在改变你的套接字代码
var socket = io.connect('/',{ 'reconnect': true, 'reconnection delay': 500 });
尝试改变你的nginx.conf的位置如下;
location /explorer/socket { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://xxx.xxx.xx.xxx:3000; }
资料来源: http : //nginx.com/blog/nginx-nodejs-websockets-socketio/