简体   繁体   English

配置apache和nodejs socket.io

[英]Config apache and nodejs socket.io

Hi I'm working on a website using apache 2.2 and feathersjs. 嗨,我正在使用apache 2.2和feathersjs的网站上工作。 on this website we have DV SSL so we can't access subdomains using https. 在此网站上,我们拥有DV SSL,因此我们无法使用https访问子域。

my node application is running on 3030 port. 我的节点应用程序正在3030端口上运行。 so I can access to feathersjs app using this address: 因此我可以使用以下地址访问feathersjs应用程序:

http://mywebsite.com:3030 http://mywebsite.com:3030

but https://mywebsite.com:3030 not working. https://mywebsite.com:3030无法正常工作。

I need to use a Location like https://mywebsite.com/socket/ to connect to feathersjs websocket (guess ws://localhost:3030) 我需要使用一个类似于https://mywebsite.com/socket/的位置来连接到feathersjs websocket(猜测ws:// localhost:3030)

here is client side code: 这是客户端代码:

const host = 'https://mywebsite.com/socket';
const socket = io(host,{
            transports: ['websocket'],
            forceNew: true
        });

I need httpd configuration to connect ws over https. 我需要httpd配置才能通过https连接ws。

post_virtualhost_global.conf post_virtualhost_global.conf

<VirtualHost 185.173.106.42:80>
  ServerName mywebsite.com

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule .* ws://localhost:3030%{REQUEST_URI} [P]

    ProxyPass /socket http://localhost:3030/
    ProxyPassReverse /socket http://localhost:3030/

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

</VirtualHost>

<VirtualHost 185.173.106.42:443>
  ServerName freevery.com

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule .* ws://localhost:3030%{REQUEST_URI} [P]

    ProxyPass /socket http://localhost:3030/
    ProxyPassReverse /socket http://localhost:3030/

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location /feathers>
        ProxyPass http://localhost:3030/
        ProxyPassReverse http://localhost:3030/
    </Location>

</VirtualHost>

if I try to connect web socket using http://mywebsite.com/socket it says 如果我尝试使用http://mywebsite.com/socket连接网络套接字,则会显示

Failed to load http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1 : Redirect from ' http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1 ' to ' https://mywebsie.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1 :从' http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-重定向1lgZ1 '到' https://mywebsie.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1 '已被CORS策略阻止:所请求的标题上没有'Access-Control-Allow-Origin'标头资源。 Origin ' http://localhost:3001 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:3001 '。

and if I try to connect web socket using https://mywebsite.com/socket it says 如果我尝试使用https://mywebsite.com/socket连接网络套接字,则会显示

Failed to load https://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lUP5 : No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载https://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lUP5 :所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:3001 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:3001 '。 The response had HTTP status code 404. 响应的HTTP状态码为404。

I added Header set Access-Control-Allow-Origin "*" to my .htaccess file but problem exists. 我在.htaccess文件中添加了Header set Access-Control-Allow-Origin "*" ,但是存在问题。

what is wrong with my configurations? 我的配置有什么问题?

Problem solved. 问题解决了。 Apache had conflicts with nginx and 301 error was form it. Apache与nginx发生冲突,因此出现301错误。 So I used nginx to proxy websocket and it's working on a subdomain without any problem. 因此,我使用nginx代理websocket,它可以在子域上正常工作。

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

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