npm node app.js different apache - apache

I was able to start my own RTCMultiConnection in my dedicated hosting.
https://github.com/muaz-khan/RTCMultiConnection
The problem I hit is when I want to integrate it in a full php / ajax / jquery website I made.
A site with login using ajax calls.
because the RTCMultiConnection requires node js and a different port (like https://www.__MyDomain.com:9001/ ),
I thought I could insert it in my site usign a XMLHttpRequest.
**** Problem ; I get a Allow-Control-Allow-Origin error
-- I tried to add it in .htaccess but without success... ;(
-- I went to my regular php.ini and installed mod_headers ; same thing no success ;(
•• I tried curl -I https://__MyDomain.com:9001/
and "Surprise surprise I get : HTTP/1.1 200 OK......
but my regular site (without the :9001) have Apache/2.2.31
The QUESTION HERE (is related to node and not RTCMultiConnection) ; WHY does "node server.js" have a different apache ???
Is it possible to "tell" or "ask" node to use the same apache config that I have ??
Thanks

You can keep running server.js on port 9001 and access/use/invoke socket.io/nodejs from your main PHP website. E.g.
rtcMultiConnection.socketURL = 'https://yourdomain.com:9001/';
Now your PHP pages will successfully use socket.io that is running on port 9001.
You can try this for testing purpose:
rtcMultiConnection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/';
Note: You can NOT run both nodejs and appache/php on same port: 80 or 443. That's why it is suggested to keep running nodejs on-a-separate-port 9001 and access/use socket.io from your PHP pages.

Related

Redirect keystone app to sub directory using htaccess

I am new in htaccess. I am using Keystone.js, using which I developed a blog which is listening on port 3000. Like this:
https://localhost:3000
Everything is fine now. But what I want is that my blog should run on this url :
https://localhost/blog
How can I achieve this scenario where I provide "https://localhost/blog" and its should work like "https://localhost:3000". In this case URL will remain the same: https://localhost/blog
Plus I also want that when a user visits this url: "https://localhost:3000", it should redirect to: "https://localhost/blog".
How can I achieve this scenario? All I want is to hide a port from URL.
I have tried alot of things to work around but its not working for me.
Something like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^blog(.*) http://localhost:3000/$1 [P,L]
Ok now this about code is working perfect for main page i.e: https://localhost/blog because I added a rule for it in htaccess. But the issue is that all links, images & included files are broken in my blog.
Now I used base tag for this issue. But it converts my links to this format:
http://localhost:3000/contact
http://localhost:3000/help
http://localhost:3000/post
I considered http://localhost:3000/ as a base tag in my page head.
As you can see 3000 port is again appearing in urls which I dont want to show. It should be:
http://localhost/blog/contact
http://localhost/blog/help
http://localhost/blog/post
This is the safest and easiest way to run Node on port 80:
Login to the server and issue the following commands:
$ sudo apt-get install libcap2-bin
$ sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
Note: Change the path to Node above to whatever is displayed when you type which node
Now when you tell Node to run on port 80, it won't complain. And you won't have to deal with Apache or Nginx, run your app as root, or worry about port forwarding.
I would advise to use NGINX to solve this issue. You can check those links :
https://allaboutghost.com/how-to-proxy-port-80-to-2368-for-ghost-with-nginx/
Node.js + Nginx - What now?

HHVM through Apache mod_fastcgi on debian

I've been able to rather easily get facebooks hhvm working from prebuilt debian packages as well as compile it, and afterwards to run it behind apache as a proxy. The problem with the proxy setup is though, that I can't get response headers other than http status code 200 - like 304 for example - through. It's not the proxy config of apache, but something wrt hhvm and apache interact, or even in hhvm.
Anyway, HHVM officially stopped supporting the standalone server, and they're moving over to fastcgi, and as all of our servers are running Debian, I don't have access to mod_proxy_fastcgi without compiling it (the only backports I found of apache 2.4 don't have mod_proxy_fastcgi backported unfortunately).
So I'm currently trying to get HHVM to run behind the old mod_fastcgi with apache 2.2. But currently I'm only getting "connect() failed" in the error log of apache, while hhvm is listening on :::1080
The important part of my apache config is
RemoveHandler application/x-httpd-php
FastCgiExternalServer /home/www/hhvm/hostname/htdocs/php5.fcgi -flush -host ip6-localhost:1080
AddType application/x-hhtpd-fastphp5 .php
Action application/x-httpd-fastphp5 /php5.fcgi
Alias /php5.fcgi /home/www/hhvm/hostname/htdocs/php5.fcgi
netstat also lists hhvm as listening on :::1080 and I can connect to it via telnet
Any Ideas on what I need to change so it works?
Looks like a IP6 port problem. Try \[ip6-localhost\]:1080 . Not sure if this has side effects in Apache.
How do ports work with IPv6?

Cannot load socket.io.js with apache proxy setup

I feel like this question might be a duplicate but from all similar threads I have checked still wasn't able to find a answer.
I have vhost setup to proxy /node/ request to NodeJS/Socket.IO server.
ProxyRequests On
ProxyPass /node http://192.168.2.151:1337/
ProxyPassReverse /node http://192.168.2.151:1337/
This setup successfully serves everything from Apache, expect requests: http://domain.com/node/
Everything works fine when it comes to NodeJS.
I just can't get this line:
<script src="/node/socket.io/socket.io.js"></script>
to load socket.io.js, it always spits out what NodeJS server has to say.
The setup:
index.html is served by Apache. http://domain.com/index.html
index.html is simple HTML with script tag for loading socket.io.js (like I wrote above).
When I request http://domain.com/index.html script src is loaded but the content is NodeJS servers response instead of socket.io.js.
I have 'npm install socket.io' in the directory where NodeJS server is running from.
I have tried to serve index.html with NodeJS as well, regardless - socket.io.js content is still NodeJS responce.
Did anyone had this problem? Or could link me to an answer? :)
Cheers!
Look at this question and all of the answers at serverfault: Configuring Apache2 to proxy WebSocket?.
The problem is that mod_proxy doesn't support WebSockets and just makes it an HTTP request. You could use a different port and avoid proxying, switch to Nginx, try haproxy or use various other Apache module alternatives. For instance, mod_proxy_wstunnel is basically mod_proxy with WebSockets support. See the serverfault link above for details on all of the solutions.

Will running node.js with Apache causes too much performance degradation?

I am trying to run Apache and node.js on the same Amazon EC2 instance. After research online, I came up with the following solution:
run Apache on port 9000
run node.js apps on port 8001, 8002 and so on.
create a reverse proxy in node.js, running on port 80. It routes requests to different ports based on the hostname.
This solution works. (Although I haven't found a way to start node.js automatically)
My question is, will running multiple node instance causes performance degradation? Or will the reverse proxy be a problem?
Thanks,
Performance Degradation
On the contrary. If all you do with node is proxying, the overload is insignificant (as compared to apache's). I do have a quite similar setup as yours (small virtual machine, 3 legacy apache websites, node.js proxying and enhancement). So far, apache is the resource eater, not my node apps, which nonetheless proxy/filter/intercept every incoming http request
Here's my setup :
main proxy
which handles all incoming requests (for as many domains as you like) : I personally use nodejitsu's http-proxy which is very robust and simple to configure
var http = require('http');
var httpProxy = require('http-proxy');
var options = {
hostnameOnly: true,
router: {
'domain1.com': '127.0.0.1:8081',
'www.domain1.com': '127.0.0.1:8081',
'subdomain1.domain1.com': '127.0.0.1:8082',
(...)
'domain2.com': '127.0.0.1:8090',
(...)
}
}
var mainProxy = httpProxy.createServer(options);
mainProxy.listen(8080);
You can redirect to apache directly from the option object, or do some more url parsing in another (middleware) node app on a different port.
WARN: if you don't wish to install/run node as 'root' (which I'd strongly advise in a production environement) : redirect port 80 to some other port with an IPTABLE directive (let's say 8080) where this proxy runs (see here for detailed example of Iptable directives). Mine, on a debian squeeze, reads :
#REDIRECT port 80 to 8080
$IPTABLES -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
node apps
which do some URL parsing with regexes, or whatever you need. Ex: redirect to a few (legacy) apache servers which (in my case) only serve legacy content not yet served by the 'in developement' node apps.
Daemonisation
There are several solutions to make node run as a daemon. My favorite two are :
nodemon will monitor all files in your node app folder and subfolder for change and restart the node app on file change. It's perfect for a development environment
forever (yet again by Nodejitsu) will restart your node app if ever it stops. It's very customisable.
Also :
init.d script : I've written this debian init.d script for my own servers (should work on ubuntu)
Node is really really fast and it's build for handling thousands of connections in the same time, so using a proxy built with it won't be a problem at all in my opinion.

DNS problem - dig resolves but curl cannot connect to host

I have recently created a Rackspace cloud server instance using CentOS 5.5. I have used yum to install the "Web Server" group (it includes Apache, etc.), added www.booztrakr.com as the ServerName in httpd.conf, made sure iptables allows on port 80. I had registered this domain with Go-Daddy and changed their name servers to the Rackspace name servers on their site. I added "A" and CNAME records to the Rackspace name servers. httpd has been started. When I use curl on the server I can get the Apache landing page. When I dig www.booztrakr.com from a remote machine(over the internet) the answer section returns:
www.booztrakr.com. 300 IN CNAME booztrakr.com.
booztrakr.com. 300 IN A 184.106.216.156
When I try a browser or curl, it can't connect:
curl -G www.booztrakr.com
curl: (7) couldn't connect to host
I know this has got to be pretty basic and config related but I'll be dammed if I can see it. Any help would be appreciated. Thanks.
If dig resolves, this just means the DNS server returns the right values. It will even work if the IP doesn't exists.
If a HTTP connecting to the server fails, this is a configuration problem.
The server responds to ICMP requests, so it's not a routing problem.
When I use curl on the server I can get the Apache landing page
Your webserver is running, but you just can't reach it from outside. This is the problem. What does iptables --list outputs?