简体   繁体   English

无法连接到我的棘轮/ WebSocket服务器

[英]Can't connect to my Ratchet / WebSocket Server

I've been trying to get my WebSocket server (using Ratchet in PHP) running for the better part of the day, I feel like I've tried everything and still no luck. 我一直在努力使我的WebSocket服务器(在PHP中使用Ratchet)在一天中的大部分时间里运行,我觉得我已经尝试了一切,但仍然没有运气。 When I try and connect to the WebSocket server in JS it just takes ages and then it times out, essentially, the server accepts the connection (I don't immediately get a connection refused error) but then does nothing. 当我尝试连接到JS中的WebSocket服务器时,它花了很长时间,然后超时,从本质上讲,服务器接受了连接(我没有立即收到连接被拒绝的错误),但是什么也不做。

I'm running a 14.04 Ubuntu with a LEMP stack. 我正在运行带有LEMP堆栈的14.04 Ubuntu。 I have found loads of nginx configurations to make this work, but none of them worked for me. 我发现有很多nginx配置可以完成这项工作,但是它们都不适合我。 The strange thing is that it works perfectly fine locally with my Virtual Machine, which is running a LEMP stack as well, with virtually the same nginx configuration. 奇怪的是,它与我的虚拟机在本地运行正常,虚拟机也运行LEMP堆栈,并且实际上具有相同的nginx配置。 I never needed to configure anything in my nginx to make websockets work. 我从来不需要在Nginx中配置任何东西来使websocket工作。

A very basic Ratchet example I've been using to test this: http://socketo.me/docs/hello-world 我一直使用一个非常基本的棘轮示例进行测试: http : //socketo.me/docs/hello-world

I'm completely lost, and any help would be really appreciated! 我完全迷失了,任何帮助将不胜感激!

Edit: 编辑:

Okay, so I figured out that I needed to set my proxy_pass on a different path than the root path in order for it to work. 好的,所以我发现需要将proxy_pass设置在与根路径不同的路径上,以使其起作用。 So originally I had this: 所以最初我有这个:

location / {
    proxy_pass http://localhost:1111;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    try_files $uri $uri/ /index.php?$query_string;
}

But not that I changed it to this, it's working: 但不是我将其更改为此,它正在工作:

location /ws/ {
    proxy_pass http://localhost:1111;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

However, I'd still like to configure it so that it uses the root url, if that is possible (I mean, it should be since on my local Homestead machine it does work on the root path) 但是,我仍然希望对其进行配置,以使其使用根URL(如果可以的话)(我的意思是,这应该是因为在我的本地Homestead机器上它确实可以在根路径上工作)

I wrote a sweet tutorial on this a short time ago that I think covers the problem you are having. 不久前,我就此编写了一个不错的教程,我认为它涵盖了您遇到的问题。

Basically you can't have try_files and the proxy in the same location block. 基本上,您不能在同一location块中包含try_files和代理。 The easiest work around that causest 0 problems is to run the WebSocket off a subdomain with its own Server definition. 导致0问题最简单的解决方法是在具有其自己的Server定义的子域上运行WebSocket。

Check out the tutorial here: http://blog.samuelattard.com/using-nginx-as-a-proxy-for-websocket-connections/ 在此处查看该教程: http : //blog.samuelattard.com/using-nginx-as-a-proxy-for-websocket-connections/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM