如何使用https在生产服务器上使用Laravel Echo

时间:2016-09-03 18:51:32

标签: node.js nginx socket.io laravel-5.3

我想知道在https生产服务器上设置Laravel Echo的正确(或任何相关的)方式是什么。我已经让它在我当地的流浪汉工作了一段时间,现在我已经将更改推送到生产中,我无法让脚本连接到节点服务器。这是我现在拥有的。

var echo = require('laravel-echo-server');

var options = {
    host: 'https://localhost',
    port: '6001',
    sslCertPath: '/etc/nginx/ssl/nginx.crt',
    sslKeyPath: '/etc/nginx/ssl/nginx.key'
};

echo.run(options);

然后在javascript中

import Echo from "laravel-echo"
window.echo = new Echo({
    broadcaster: 'socket.io',
    host: 'https://localhost:6001'
});

以上配置是我的开始,但我尝试了很多其他组合,包括尝试编辑nginx配置以一起绕过https。如果绕过https是所需的方法,那么关于如何使用Laravel Echo执行此操作的任何建议都将受到赞赏,因为我所引用的关于此主题的socket.io线程似乎并不是为我做的伎俩。

4 个答案:

答案 0 :(得分:5)

这就是我解决问题的方法

<强>第一即可。您必须使用ssl配置您的nginx vhost(尚未连接websocket)。我使用Let的加密(https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

<强>第二即可。您可以在vhost上创建路径以代理您的websocket。这样nginx处理ssl协议,你也不使用另一个端口

laravel-echo-server

之后,您的脚本必须连接到“https://localhost/ws/

注意:我使用端口3000作为我的2017-03-27 15:13:08 ERROR ContextLoader:331 - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.example.myapp.integration.SimpleGateway com.example.myapp.controller.HelloWorldContr oller.gw; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.myapp.integration.SimpleGateway] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.example.myapp.integration.SimpleGateway com.example.myapp.controller.Hell oWorldController.gw; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.myapp.integration.SimpleGat eway] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.ann otation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 26 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.myapp.integration.SimpleGateway] found for dependency: ex pected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=tru e)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ... 28 more intance,而不是原始问题中所述的6001

答案 1 :(得分:1)

除了user237329响应外,javascript中的laravel echo设置应为:

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.host,
    path: '/ws/socket.io',
});

答案 2 :(得分:0)

我找到了解决这个问题的方法。使用Forge和Let's Encrypt时,Forge将在月初创建包含SSL密钥的唯一ID的文件夹。起初,当我尝试使用这些密钥时,laravel-echo-server表示无法找到密钥。在上面我的问题中的代码中,我引用了我服务器上的其他两个密钥,我认为它是这个月正确密钥的sym链接,但结果并非如此。我确实需要引用唯一ID文件夹中的文件。为了让它工作,我只需运行“ sudo 节点socket.js”。

答案 3 :(得分:0)

也许有人会像我一样来到这里。 首先,请记住将不受信任的证书添加到浏览器例外,因为对于laravel echo端口,我们必须单独执行。否则你只会在控制台中拒绝连接。 其次,当前版本的laravel echo不接受主机选项中的协议,而是在一个名为&#39; protocol&#39;的单独选项中。