简体   繁体   English

将Apache子目录重定向到Node.js时访问socket.io

[英]Accessing socket.io when redirecting an Apache subdirectory to Node.js

I am wanting to redirect an Apache subdirectory to a Node.js app. 我想将Apache子目录重定向到Node.js应用程序。 I have added the following in my Apache VirtualHost .conf file for my regular domain: 我在我的常规域的Apache VirtualHost .conf文件中添加了以下内容:

<VirtualHost *:80>

    # Already existing Apache lines here removed to keep this post short

    # The following are all the new lines I have added

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full
    <Proxy *>
        Require all granted
    </Proxy>

    <Location /mysubdir>
        ProxyPass http://127.0.0.1:3000
        ProxyPassReverse http://127.0.0.1:3000
    </Location>
</VirtualHost>

However, now when trying to connect to my site through http://example.com/subdirname I get a 404 error for socket.io . 但是,现在,当尝试通过http://example.com/subdirname连接到我的网站时, socket.io出现404错误。

In my app.js I had the following: 在我的app.js中,我有以下内容:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

Based on another stackoverflow post I changed my app.js to this: 基于另一个stackoverflow帖子,我将app.js更改为:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io')(server, {path: '/mysubdir/socket.io'}).listen(server);

But it still gives me a 404 error. 但这仍然给我一个404错误。 What am I doing wrong? 我究竟做错了什么?

Update: I posted an answer below that seems to be working for me. 更新:我在下面发布了一个答案,该答案似乎对我有用。 I've only tested it now for a few minutes, so feel free to look it over in case you notice anything that will cause me issues down the road. 我现在只测试了几分钟,因此,如果您发现任何可能导致我遇到的问题,请随时查看。

I have discovered that I did not need to make any changes to my app.js only to my html code. 我发现我不需要仅对html代码对app.js进行任何更改。 So to solve, I restored my app.js to: 因此,为了解决问题,我将app.js恢复为:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

And modified the following lines in my html code: 并在我的html代码中修改了以下几行:

I changed: 我变了:

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

To: 至:

<script type="text/javascript" src="/mysubdir/socket.io/socket.io.js"></script>

I also changed: 我也改变了:

var socket = io.connect();

To: 至:

var socket = io.connect('http://example.com:3000');

Everything now seems to be working the way it should. 现在一切似乎都在按应有的方式运行。 It is interesting, however, that all I needed to change was the client side. 有趣的是,我需要更改的只是客户端。 Most of the other examples I found either required changing the server side app or adding redirects to the Apache hosts file. 我发现的大多数其他示例都需要更改服务器端应用程序或将重定向添加到Apache主机文件。 Neither of those worked for me. 这些都不适合我。 Perhaps there is something I am missing? 也许我缺少什么?

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

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