简体   繁体   中英

Socket.io with apache

I dont have much experience neither in Node.js not in socket.io, thus maybe I will ask silly questions and sorry for that first of all.

I am trying to do following:

  1. Installed node on ubuntu where I have apache also installed.
  2. Created virtual host in apache and set it as proxy to node. My conf file looks like:

     <VirtualHost *:80> ServerAdmin giorgi@omedia.ge ServerName node.aidemo.info ServerAlias www.node.aidemo.info ProxyRequests off <Proxy *> Order deny,allow Allow from all </Proxy> <Location /> ProxyPass http://127.0.0.1:8080 ProxyPassReverse http://127.0.0.1:8080 </Location> </VirtualHost> 
  3. Have created simple js file for server (first server example in socket.io website) and started server from cli with command: node server.js. It starts perfectly and listens to 8080

  4. Created another virtualhost where I put clientside index.html (also from first example in socket.io). At first I had problem (and actually main problem is this), browser couldn't resolve path /socket.io/socket.io.js. Then I went to the url (http://localhost:8080/socket.io/socket.io.js) from lynx locally from terminal, downloaded that js and put locally with virtualhost near index.html. After this, browser could resolve that request, but I have error when socket.io.js itself is trying to get the url:

     http://localhost:8080/socket.io/1/?t=1347623348836 

Do you have any ideas how can I solve this problem? My main goal is to have web url from which I can access my node server and talk with it with socket.io - for example to create very simple chat.

I hope I was clear. Thank you everyone who will try to help.

I am using express + socket.io and they are listening on port 3001 . And I want http://example.com/folder to redirect to my Express app listening on port 3001 (ie, to http://localhost:3001 , server-side ).

I have done the following.

The .html file has this:

<script src='/folder/socket.io/socket.io.js'> </script>  
<script>  
var socket = io.connect('http://example.com', {resource: 'folder/socket.io'});  
...  
</script>  

And my apache2 conf looks like this:

ProxyPreserveHost On
ProxyPass /folder/ http://localhost:3001/
ProxyPassReverse /folder/ http://localhost:3001/

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

<Location /folder/>
    allow from all
</Location>

Note that it requires the module proxy_http to be enabled. To enable it, run this command:

sudo a2enmod proxy_http; service apache2 restart

If you put socket.io.js file locally near your index.html file, it will not resolve the problem because you probably didn't change the url in your socket var in main.js file, look at:

var socket = io.connect();

in your main.js/index.html (script)

replace with:

var socket = io.connect(httpprotocol+hostname+httpport);

My code source look like this:

var socket = io.connect('https://192.168.43.187:8443/');

http://localhost:8080 is obviously not going to be available to anything outside of your server.

The client-side javascript's io.connect() should be connecting to http://node.aidemo.info so that apache can send that off to Node.

http://node.aidemo.info:8080 might also work if you've opened up port 8080.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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