I spend 1 day to search all on google and try all the code but it seem like nothing work.
My server code.
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
I try to replace 3000 by 80 but not work.
I also try to change in etc/nginx/conf/example.com.cof
server {
listen 80;
upstream project {
server example.com:3000;
}
server {
listen 80;
location / {
proxy_pass http://project;
}
server_name example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80 default_server;
...
}
Nothing work.
Please help
using port 80 requires sudo privileges
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
Start app using,
sudo PORT=80 node app.js
sudo PORT=80 npm start #or like this, depends on your app
Related
I have nodejs running on pm2 and its listening on port 5000. If i run netstat -na |grep 5000 on the server i get
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN
But when my react app makes a call to http://localhost:5000/patients i get
::ERR_CONNECTION_REFUSED
Here is the code for the server:
const app = express();
const port = process.env.PORT || 5000;
app.use('/patients', require("./routes/patientRoute"));
app.listen(port, "0.0.0.0", () => {
console.log(`server is running on port: ${port}`);
})
and from React production build i run GET with axios to http://localhost:5000/patients thats when the err occurs
Also i have opened the port on firewalld
first i had to get rid of the "0.0.0.0", then i replaced http://localhost:5000 with /api/ on all of my axios calls and then in my nginx conf file i needed to add location
/api/ { proxy_pass http://127.0.0.1:5000; }
.....the location block in NGINX was the key!
I'm new to Nginx, and I have some trouble with hosting 2 websites on my RaspberryPi (Raspbian).
I have 2 domains, site1.com (:8080) and site2.com(:8000), both are Node.JS apps (with Express). I have working SSL certifications with Let's Encrypt for both.
This is my site1 nginx config (/etc/nginx/site-available/site1):
server {
server_name site2.com;
listen 80;
listen [::]:80;
location / {
include /etc/nginx/proxy_params;
proxy_pass http://192.168.1.11:8080;
}
}
This is my site2 nginx config (/etc/nginx/site-available/site2):
server {
server_name site1.com;
listen 80;
listen [::]:80;
location / {
include /etc/nginx/proxy_params;
proxy_pass http://192.168.1.11:8000;
}
}
So indeed there is no part of 443 in these conf files but https://site2.com is working well but https://site1.com redirect me to the webpage of https://site2.com (keeping the site1 URL). I guess it's because my_server_ip:443 is already taken by site2 (no ??).
And the http://site2.com give me a 502 Bad Gateway and is not redirected to https (site1 is well redirected to his https).
This is the server part of my Node apps, they are the same for the 2 apps (except a port and SSL URI)
const express = require('express')
const app = express()
var https = require('https');
var http = require('http');
var fs = require('fs');
const privateKey = fs.readFileSync('/etc/letsencrypt/live/site1or2.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/site1or2.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/site1or2.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
const httpsServer = https.createServer(credentials, app);
const bodyParser = require("body-parser");
const urlencodedParser = app.use(bodyParser.urlencoded({
extended: true
}));
http.createServer(function (req, res) {
//redirect to https
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(8080); //8000 for my site2.com
httpsServer.listen('443', () => {
console.log('Server https listening on Port 443');
})
I tried to change the Nginx confs to add 'listen 443; SSL on;...' but I have always errors like 'Failed to start A high-performance web server and a reverse proxy server' and I don't understand how to fix it.
So is the problem from my JS code or my Nginx confs? (or both maybe..?)
Thank for reading, it's my first StackOverflow post, I hope I didn't forget information and sorry if there is an English mistake.
Have a good evening (or day)!
I finaly understood how does multiples node websites hosting with SSL works, in the node.JS configuration (app.js) the app has to only listen to 1 port (8080 and 8000 for me) and must not refer to SSL and port 443 at all.
All the ssl configurations and https redirections have to be in the Nginx conf, for exemple, my file /etc/nginx/sites-available/site1:
server {
server_name site1.com;
listen 80;
listen [::]:80;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/site1.com/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem;
server_name site1.com;
location / {
proxy_pass http://192.168.1.11:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
And the same for site2 with port 8000.
I am using node and express, AWS Ec2 Linux and running two web apps in port number 8080 and 8081 using pm2.
added subdomains to my Elastic IP admin.example.com and app.example.com.
My both app running in localhost:8080 and 8081.
/etc/nginx/conf.d/virtual.conf // After Edit
server {
listen admin.example.com:80;
server_name admin.example.com;
location / {
proxy_pass http://localhost:8080;
}
}
/etc/nginx/conf.d/virtual.conf // Before Edit
#
# A virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server.js
const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/login', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/logout', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen('8080');
console.log('Server started at port 8080');
nginx running fine after the restart but did not remove port number 8080 from my domain.
What are the things to be done with port 80?, I just enabled 80 from my AWS Instance inbound rule, Anything else I miss?
You are providing the domain with the port.
Try it this way:
server {
server_name admin.example.com;
listen 80;
location / {
proxy_pass http://127.0.0.1:8081;
}
}
change virtual.conf to :
server {
listen 80;
server_name admin.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
And make sure you have done creating DNS record to point to your public ip address.
Issues Fixed :
Inbound rules added only ::/0 in the source for port 80, after remove and add HTTP, source updated like 0.0.0.0/0, ::/0 now all runs fine.
I have a node server with express on top. Godaddy is the domain registrar that I use.
If I try to connect with www.example.com doesn't work, only with https://www.example.com.
This is how the code looks like:
app.use(express_enforces_ssl());
app.enable('trust proxy');
app.use (function (req, res, next) {
if(req.secure){
next();
}else {
res.redirect('https://' + req.headers.host + req.url);
}
});
var options = {
ca: fs.readFileSync('./gd_bundle-g2-g1.crt'),
key: fs.readFileSync('./server_cert_key.key'),
cert: fs.readFileSync('./cfbe0f99da37dcae.crt')
};
https = require('https').createServer(options, app);
io = require('socket.io')(https);
However, if I 'ping www.example.com' the server is reached, but not in the browser.
The process is running on port 443 (dedicated port for secured connection).
I have made port forwarding from internal port 443 with output on port 80, this is how domain registrar works, only with port 80.
I don't know how can I fix it. Do you have any idea ?
DNS doesn't usually require ports info. Browser automatically assumes 80 for http scheme and 443 for https scheme.
The app code is only listening on 443. You need to add additional:
require('http').createServer(app).listen(80);
Try normalizing the port before listen to it.
In the code below, the port will be normalized according to the app settings. If none were found, it will be defaulted to port 80
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) return val;
if (port >= 0) return port;
return false;
}
//port 443 if https:// is detected
var port = normalizePort(process.env.PORT || '80');
// for me, I imported ('http') instead of ('https')
// but it also works on port 443
https.listen(port, function listening() {
console.log('Listening on %d', server.address().port);
});
I have some trouble with nginx proxy_pass redirection on localhost subdomain. I have a domain "domain.com", i want to redirect all request on *.domain.com on *.localhost:9000. Then node handle all request on *.localhost:9000 to the good express app.
On nginx conf when i try the following :
server {
server_name extranet.domain.com;
listen 80;
location / {
proxy_pass http://extranet.localhost:9000;
}
}
Request on extranet.domain.com are well redirected to the good express webapp.
With this :
server {
server_name ~^(.*?)\.domain\.com$;
listen 80;
location / {
proxy_pass http://localhost:9000/$1;
}
}
Express app running on localhost:9000 handle request /mysubdomainname, which implie that regex is good.
But when i try :
server {
server_name ~^(.*?)\.domain\.com$;
listen 80;
location / {
proxy_pass http://$1.localhost:9000;
}
}
All request on *.domain.com return http code 502.
Why http://localhost:9000/$1; works and not http://$1.localhost:9000; ?
(all subdomain are set in /etc/hosts).
Thanks in advance. I'm totally lost !
When a host name isn't known at run-time, nginx has to use its own resolver. Unlike the resolver provided by OS, it doesn't use your /etc/hosts file.
Maybe this will give you a hint, I wanted to pass the subdomain from Nginx to an Express app. Here is my code:
nginx.conf
http {
upstream express {
server localhost:3000;
}
domain.com inside nginx/sites-available
server {
listen 80;
server_name ~^(?<subdomain>.+)\.domain\.com$;
location / {
proxy_set_header Subdomain $subdomain;
proxy_set_header Host $host;
proxy_pass http://express;
}
}
Express app index.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
const subdomain = req.headers.subdomain;
});
app.listen(3000, function () {
console.log('Example app listening on port 4000!');
});