Application working on http but not on https - node.js

Background: I have two backend instances running haproxy, apache, nodejs on ports 4000 and 8007 for haproxy, 80 for httpd, 3000 3007 3012 running node. I have a staging site which is running on this instance. I have signed certificate installed at ELB level and ELB listening on https 443 port to backend http 80 port.
Issue: When I tried to login to the stating url with http then the backend is working fine on port 80 and 3000, network flow logs also returning status code as Ok for request to http://stating _url:3000. But when I tried to access site on https then I am getting status as "blocked:mixed-content" for https://stating _url:3000 request. Please refer to below screenshots
Below is output for http which is working as expected
Below is the output for https which is not working as expected
I tried different protocols for listeners at ELB level. I dont understand why ELB on port 80 sends request to port 80 on backend server and all works fine but same ELB on port 443 sends request to same port 80 on backend server but fails to establish connection with 3000.

Related

Cannot get AWS Elastic Beanstalk single instance (no load balancer) to listen on 443

No matter what I do I cannot get my application to listen on port 443 (https). I simply need nginx to forward traffic to my app which is running https on port 8080, but nginx will only listen on port 80 and will refuse to forward to my app unless it is also running on port 80.
I've followed the instructions in this article but it makes no difference.
I do not have a domain name yet, I am simply using a self signed cert so I don't believe certbot will help here.
Please help I am so frustrated hahaaaaaa

Access Node.js server by URL without port at the end

My server is running on a Node.js environment with Express. My server works fine, but I can't remove the port at the end of the domain name from the URL.
What is the right way to access my app with an URL without port at the end ?
Client side
By default, the port is 80 when a browser make an HTTP request.
If you type localhost, the real request is localhost:80 because no port is specified. It will be the same with any domain name. If you type example.com, the real request is example.com:80.
It is the client (here the browser) which choose on which port it will make his request to the server.
You can force your browser to emit a request on any port by adding :port_number after the domain name, as localhost:3000 or example.com:3000. Here we change the port from 80 to 3000.
Server side
The web server chooses on which port it listens for requests. It can be 80, 3000 or any other port.
If a client makes an HTTP request, your web server needs to listen to the right port. If the client emits example.com:4000, your web server must listen on port 4000 to get and process the request.
To make a web server, you can use Node.js, Apache (used in LAMP), Nginx etc. You can have multiple web servers running on your system and each of them can use multiple ports, but you can't make them listen on the same port. One of your web server may not start or could take the lead on others or crash...
Solutions are to use only one web server or to use multiple web server on different ports. In your situation, you are using LAMP so Apache web server. Its probably running on port 80 in his configuration. In this case you can't run a Node web server on port 80 because it's already in use. You should choose another port like 3000 for example. Both Node and Apache will then run on your system but on different ports respectively 3000 and 80.
In this last situation, you can access directly to Apache, but not to Node without precise the port 3000. To be able to access Node web server by port 80 without stopping Apache, you need to go through Apache and to make it redirect requests to your Node server in some cases. To do that, you need to configurate a proxy in your Apache. Note that it would be the same if you was using Nginx or other web servers.
Example
Let's take a simple express server on port 3000 :
// server.js
var express = require('express'),
app = express(),
http = require('http').createServer(app),
port = 3000;
app.get('*', function (req, res, next) { res.sendFile(__dirname + '/views/index.html'); });
http.listen(port, function () { console.log('App running & listening on port ' + port); });
If you type in the terminal node server.js, you can access from browser by localhost:3000, but you can't access by localhost because no web server is running on port 80.
If you change port variable to 80, you can access from browser by localhost or localhost:80, but not by localhost:3000 anymore.
If you edit /etc/hosts (sudo nano /etc/hosts) with a new line 127.0.0.1 example.com, you can access from browser by example.com if port is 80, else example.com:port_number like example.com:3000. This third solution maps domain name to ip address in your local client only.
If the chosen port, 80 for example, is already in use by another process (as LAMP), your node server may not works. In this case you should close this other process first or choose another port for your node process. In the third example, if you close the LAMP first, you can access from browser by example.com, if you choose another port for Node, you can access from browser by example.com:port_number like example.com:3000 for Node and still access your LAMP server on port 80.
Don't forget that 80 is the default port used by the browser if no port is specified. If you use another port, you should precise it from the browser by adding :port_number after your domain.
Now if you own a real domain name you will need to make a real DNS mapping not juts edit /etc/hosts. Configure your DNS on your registar account (where you bought your domain name) to make it point to your server's IP. Like that, when a client make an HTTP request to the domain name, it will be redirected to your server.
To have both Apache and Node.js running and available on port 80, you should make a proxy as explain above. Indeed, for you the problem is probably that you have a web server already running on port 80 (Apache with LAMP) and you want also your Node.js app to run on port 80 to don't force clients to precise the port at the end of the url. To fix that, you need to make a proxy in Apache conf to redirect requests which come from the specific domain name to your localhost node server process on the right port.
Something like that in your apache conf :
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
Here when a request arrive on your server on port 80, Apache will check if it comes from example.com and if it is, it will redirect to 127.0.0.1:3000 where your node server will take the lead. The two different process (Apache & Node) should run in the same time on your server on different port.
If you want to run your node js server without any port and simply by http://localhost then listen your express js server on port 80 .
You could either do as stated by the previous answers and run on port 80 OR
you could keep the server running on whatever port you want and setup a proxy server such as nginx and forward the HTTP requests to said server.
This could be helpful in case you want to spin up multiple instances or even different processes.
When you see a URL, without a port, it means one of two ports are being served:
https:// - port 443
http:// - port 80
Even assuming the port is not in use, you can't service directly to port 80 without superuser privileges because port 80 and port 443 are privileged ports.
If you want to test the server running on port 80 directly:
sudo node index.js
Where index.js is the name of your Express application.
Keeping it running
Because you tagged apache, I'm assuming you want to know how to set up a node server using Apache. If you don't need a production quality server and just want to keep it running all the time, you can do that too.
Dev/Just keep it running
You can daemonize your server. A quick look for a "node" solution exposes forever as a way to do that. Simply install and run like this:
yarn global add forever
# or
# npm i -g forever
# remember, sudo for port 80
sudo forever start index.js
Production/Apache
Use a non-privileged port for Node, and set up a proxy in Apache. Something like:
ProxyPass / http://localhost:8000
If you set the port to 8000. Put that in a <VirtualHost>. Examples here. Likely you would still want to daemonize your nodejs Application using forever or some similar daemon tool (systemd is great for Linux services)

CloudFlare how to point to 2087 port https?

It is written that now CloudFlare supports 2087 as a port for Https
I have a domain lets say www.somethign.com and it is secure using CloudFlare
I run my node.js on a specific port.
If I choose a port 8080, which is a port allowed for http, and then i call my page like:
http://www.mydomaidnExample.com:8080/webhook
it works perfectly.
but when I set a port for https, such as 2087 and call it like
http://www.mydomadin.com:2087/webhook
i get this error
What should I do please ?
Note that this url
localhost:2087/webhook
is working on the server
Update
Firewall is already off
When using CloudFlare there are restrictions around which ports you connect through for security reasons, the 2087 port is reserved for SSL usage:
For requests made via HTTP:
80
8080
8880
2052
2082
2086
2095
For requests made via HTTPS:
443
2053
2083
2087
2096
8443
Therefore, when using Full SSL mode within CloudFlare and you connect over port 2087 the connection to the origin will be over SSL, if you want to disable this you can use a Page Rule to turn SSL to Flexible on that port.

Accessing Node Proxy Server from a locally hosted file

I'm trying to access a node proxy server running on my local machine from somewhere else(specified later). I've tried setting proxy listening domain to 0.0.0.0 with with port 8888. The file that will send request to the proxy server is hosted using a simpleHttpServer at 127.0.0.1 with port 4444 on another computer. In this file, I'm sending the request to http://my_local_ip:8888 (I'm assuming this is where the Node Proxy lives on my computer). However, I'm get connection timeout for some reason. Does anyone see problems with this approach?

How do I use HTTPS port on clould9 ide?

I am just running some tests on c9.io - but I am stuck on how to get the HTTPS server to run.
I am using the port given in process.env.PORT which links up to the browser in c9, but there doesn't seem to an HTTPS port. In a local environment I use port 3001 for HTTPS.
The Cloud9 workspaces don't support custom SSL certificates yet so you won't be able to start an HTTPS server from there, but your http server running on 0.0.0.0:8080 will be accessible via both HTTP and HTTPS.

Resources