I am setting up a reactjs application on port 3000 as well as a nodejs API server on port 3500 on the same box on the internet. Assume the box has a domain name example.com, and I am using nginx in reverse proxy to receive the end users over https, and internally direct it to the port 3000 of the reactjs server.
On the react code, while calling axios API for a get command, which of the following should I be using:
localhost:3500
http://localhost:3500
https://localhost:3500
example.com:3500
http://example.com:3500
https://example.com:3500
Few tests I did:
Access from my browser the reactjs application successfully as example.com (nginx does the mapping to port 3000)
Using https://reqbin.com/ I was able to access the nodejs server API and get the correct result using: http://example.com:3500/users
Using https instead of http causes an error: SSL connection error Error code: 10035
If end user is supposed to connect over https to the react server, then the react server as well as the nodejs server should be running in https mode, or the browser will block the request and it will never reach the server.
Here is how to:
Run the react server in https mode:
Change nginx reverse proxy configuration to be:
proxy_pass https://localhost:3000;
Changed the URL for the nodejs server that axios is calling from http://localhost:3500 to https://example.com:3500
After npm run build, and upload the build directory to the server, run the following commands:
su
serve -s build --listen 3000 --ssl-cert "/etc/letsencrypt/live/example.com/fullchain.pem" --ssl-key "/etc/letsencrypt/live/example.com/privkey.pem"
Run the nodejs server in https mode:
Change the code of server.js with the following:
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};
https.createServer(options, app).listen(PORT, ()=>{
console.log(`Server running https on port ${PORT}`)
});
Run the following commands:
su
node server
Related
I'm trying to connect to a nodejs https server from a apache web server hosted javascript client and I'm getting an error message : 522 - Failed to load response data: No data found for resource with given indentifier. The apache web server runs on the same domain/server as the node server and the servers are proxied by Cloudflare:
Client app: https://www.example.com (apache web server on port 443)
Node SERVER: https://www.example.com:2053
Both services run in the same server/machine. This is how I start nodejs server:
// Certificates are the same used by apache web server in Virtual Host
// and were got from Cloud Flare Panel > SSL/TLS > Origin Server
var options = {
key: fs.readFileSync('/etc/cloudflare/example.com.key'),
cert: fs.readFileSync('/etc/cloudflare/example.com.pem'),
};
var socket = require('socket.io');
var http = require('https');
// Port 2053 was listed as a https port supported by Cloud Flare in
// https://developers.cloudflare.com/fundamentals/get-started/reference/network-ports/
var argv = require('optimist')
.usage('Usage: --port [num]')
.default({port: 2053})
.argv;
var server = http.createServer(options, function(req, res) {
});
server.listen(argv.port);
var io = socket.listen(server);
This is how I connect to nodejs server from the javascript client:
let socket = io.connect("https://www.example.com:2053", {secure: true});
Any tips?
Edit 1
It works if I create the node server as http (instead of https).
I was able to connect to node server by doing the follow:
Set "key" and "cert" options when instancing https node server: these files can be generated in Cloud Flare Panel > Select your domain > SSL/TLS > Origin Server. There was no need for "ca", "requestCert" or "rejectUnauthorized" parameters.
Use one of the ports listed in https://developers.cloudflare.com/fundamentals/get-started/reference/network-ports/ in the node server. Cloud flare automatically redirect these ports to the same port in your origin server.
Allow inbound connections on the selected port (step 2) in your origin server firewall.
Set SSL setting to FULL in Cloud Flare Panel > Select your domain > SSL/TLS > Overview
I have a Socket.io server running on port 3000 and when running it (and the website / client) locally everything works fine. But when I push it to the server the client can't connect anymore.
The production server is running over SSL so I assumed that I need the Socket.io server to run over SSL as well. I've setup it up like this:
var app = express();
var fs = require('fs');
var is_production = process.env.NODE_ENV === 'production';
if(is_production){
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/mywebsite.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/mywebsite.com/cert.pem'),
requestCert: true
};
var server = require('https').createServer(options, app);
}else{
var server = require('http').createServer(app);
}
var io = require('socket.io')(server);
server.listen(3000);
This still doesn't work. I don't have much experience with Socket.io so any help would be appreciated. Also note that everything worked fine before I got an SSL certificate setup on the web server.
The client is connecting to ws://mywebsite.com:3000. I've tried using http://, https:// and wss:// as well, but nothing works.
EDIT: I've tried making a request through curl and I get the following error:
curl: (35) gnutls_handshake() failed: The TLS connection was non-properly terminated.
I couldn't figure out what the problem was, so here's what I did.
I have Nginx running on the same server to serve my website so what I ended up doing was configuring Nginx to proxy all SSL connections to port 3000 and forward them to the node.js server running on port 8080. This way Nginx takes care of the SSL so the node.js server doesn't need any additional configuration.
I am hosting a meteor app on an Ubunu Linux machine. The app is listening on port 3000. If I use a webserver, like NginX and forwards the HTTP requests from port 80 to 3000 I can browse to the server from the outside and see reach the app. However, when I try to access the app directly at port 3000, i.e. browse http://myhost:3000 it just tries to connect and nothing happens.
I have made sure that all firewalls are down and that the app is listening on all interfaces, i.e. 0.0.0.0:3000, so that is not the issue.
To verify that port was actually reachable, I created a simple node js webserver:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(3000);
Now browsing to the the sever, I can see "Hello World!". So obviously this works so why I can not reach meteor has nothing to do with firewalls or unopened ports.
Thus it seems that there is something strange when trying to access a meteor app directly at port 3000. But why? I use the following environment variables:
export MONGO_URL=mongodb://localhost:27017/meteor
export HOST=myhost
export PORT=3000
export ROOT_URL=http://myhost
So what am I missing? Ports are open and I can see that the node process instance is listening on port 3000 when I run netstat -tulpan
I was using the force-ssl meteor package which makes a redirect back to the ROOT_URL without port number. So solution is to remove the package to make it work with a custom port.
I was discussing the solution on the meteor forum where I got the solution:
https://forums.meteor.com/t/can-not-access-meteor-app-without-passing-through-nginx-server/40739/11
currently i have the following problem:
i have an apache server, running SSL on Port 443 and standard (http) on 80.
Additionally a installed a Node.js server with socket.io module.
I wrote an socket.io javascript with a http server that listened to the port 3000, so in the client (browser) i include the socket.io.js like this
<script type="text/javascript" src="http://www.example.com:3000/socket.io/socket.io.js"></script>
This works very fine, when i execute my site via http://www.example.com . The browser finds the socket.io.js properly.
If i execute my site via https like https://www.example.com , i adjust the script part above like this:
<script type="text/javascript" src="https://www.example.com:8888/socket.io/socket.io.js"></script>
I changed the src to https:// and the port to 8888. Now i have to adapt my socket.io script also.
var https = require('https');
var fs = require('fs');
var socketio = require('socket.io');
// The server options
var svrPort = 8888; // This is the port of service
var svrOptions = {
key: fs.readFileSync('/path/to/example.key'),
cert: fs.readFileSync('/path/to/example-ca.crt'),
ca: fs.readFileSync('/path/to/example-server.pem')
};
// Create a Basic server and response
var servidor = https.createServer( svrOptions , function( req , res ){
res.writeHead(200);
res.end('Hi! Code here...');
});
// Create the Socket.io Server over the HTTPS Server
io = socketio.listen( servidor );
// Now listen in the specified Port
servidor.listen( svrPort );
If i execute the file with node, the socket.io started properly:
info - socket.io started
But here comes the problem: if i execute the URL of the socket.io.js file in the browser, nothing happens:
https://www.example.com:8888/socket.io/socket.io.js
Firefox answers with: data communication interrupted
The node server also gets no requests. socket.io doesn't react.
What i have to do?
Thx!
There are two js scripts needed here, one for the node.js server (I assume that is the one you posted) and one embedded in the html page.
The <source> Tag has nothing to do with the execution of your node.js server script. It states where the client will download the client js script, when loading your HTML page. It therefore needs to be downloadable from your browser - just try entering that URL yourself.
The address, where the client side of the socket.io code is receiving its content (the address of the node.js Server) must be updated in the client side js.
Depending on the size of your project, why not also use node.js as the only server for the static content?
I am new to proxy server. What I want to do is: I want to write some node.js code, and then upload to my nodejitsu account to run as a proxy server. Then I would like to use my nodejitsu proxy server on my computer, by configuring the http proxy as "abc.jit.su" (my jitsu URL), and the port as "80" in Chrome, Firefox or IE. That's to say, I want my nodejitsu proxy server to have the same function as the proxies listed here: http://www.freeproxylists.net/. Any ideas?
You can write a simple proxy using the request module, like this:
var http = require('http'),
request = require('request');
// For nodejitsu, this will be port 80 externally
var port = process.env.PORT || 8000;
http.createServer(function(req,res) {
req.pipe(request(req.url)).pipe(res)
}).listen(port);
However, this will only work with http, not https.
Nodejitsu also produces a proxy module, you may get some ideas on what to do next by looking at that.