I have created a small express app on Alibaba ECS.
Let's say that my private IP address is 121.22.15.111 and my public IP address is 50.45.23.22 (these are imaginary values).
The code is as follows:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
})
app.listen(8080, '121.22.15.111');
and my Nginx setup is as follows:
server {
listen 80;
server_name http://50.45.23.22;
location / {
proxy_pass http://121.22.15.111: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 then when I open http://50.45.23.22 (public IP), it shows connection timeout.
Where am I going wrong?
P.S. I don't have a domain name, rather I want to access my app through the IP address. How do I do so?
Just Check the security group settings of your ECS instance and allow connection from port 80 and any other which you are using in your app.
It will work.
Related
I'm trying to point my nodejs app to nginx so i can access the node.js app with my domain name, been trying for hours but nothing works, the name server are setup correctly, in fact i can access my nodejs app if i type in url the ip address of my server + the port
e.g.
192.168.1.1:3000
browser displays... Hello world!
but when i go to my domain name
e.g.
mydomainname.com
browser displays... 502 Bad Gateway nginx/1.18.0
server.js
...
//routes
app.use("/", require("./routes/web"));
app.use("/api", require("./routes/api"));
app.use(function(req, res, next) {
res.status(404).send("Error 404");
});
const http = require('http');
const hostname = '192.168.1.1';
const port = 3000;
const server = http.createServer(app);
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
...
/etc/nginx/conf.d/domainname.com
server {
listen 80;
server_name mydomainname.com;
location / {
proxy_pass http://192.168.1.1:3000/;
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;
}
}
for other folders and config files like sites-enabled/default is configure correctly and etc/nginx/nginx.conf is configured correctly since if i run
sudo nginx -t there's no error and the syntax is ok.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Please Help, all tutorials and videos about this issue are way back from 2014-2018, there's nothing from 2019 or 2020
I have a React app communicating with a very simple node backend, it works perfectly fine in local and was working perfectly fine over http. For obvious reason, the app is now running in https but the socket is not connecting anymore. I have been looking to so many threads but couldn't find a way to fix it. Here are the socket parts of my code and nginx conf.
Client side:
const socket = io('wss://sub.domain.io', { path: '/my-path',});
Server side:
const server = require('http').createServer();
const io = require('socket.io')(server, {
path: '/my-path',
serveClient: false
});
server.listen(8080);
nginx proxy
server {
....website serving part...
location /my-path/ {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Error is: GET https://sub.domain.io/my-path/?EIO=3&transport=polling&t=MsCot3e net::ERR_CONNECTION_TIMED_OUT VM17:1 which looks like a timeout because it fails to connect.
So I found an answer after so many hours and it is pretty dumb and simple. My Nginx only handle the www website (DNS redirection is enough for the website), hence I needed www.sub.domain.io here:
const socket = io('wss://www.sub.domain.io', { path: '/my-path',});
I need to run multiple sails web application on the port and run a small server to route between them by application name.
Using next code, I could route between them by adding application name after the port 3000.
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
var app1 = 'http://localhost:1337/',
app2 = 'http://localhost:1338/'
app.all("/app1/*", function(req, res) {
proxy.web(req, res, {target: app1});
});
app.all("/app2/*", function(req, res) {
proxy.web(req, res, {target: app2});
});
app.listen(3000);
This Program make the redirection well but now the problem is that, how i can change the generated links in sailsjs for any file as javascript and css files?
Many thanks
Install nginx
Run your apps on different ports.
On Linux /etc/nginx/sites-available/default (different on Windows, but probably easy to find):
--
server {
listen 80;
server_name apps.dev/1;
location / {
proxy_pass http://localhost:3001;
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;
}
}
server {
listen 80;
server_name apps.dev/2;
location / {
proxy_pass http://localhost:3002;
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;
}
}
You can access your apps using: apps.dev/1 or apps.dev/2
For production you can apply any domain addressed to the server.
For local dev just set virtual host to redirect anything from production domain to local nginx.
I currently have a digital ocean droplet connected to a domain. On the server, I'm running NGINX and trying to reverse proxy multiple node apps to it. Currently, my root directory has one node express app, at location / .
I'm trying to connect another node express application, to another sub directory. Here's the nginx config file:
server {
listen 80;
server_name servername.com;
# my root app
location / {
proxy_pass http://127.0.0.1:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# new app
location ~^ /newapp {
proxy_pass http://127.0.0.1:6002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
The problem is that the new app is trying to serve files out of /newapp, which is breaking. I'm thinking it's probably something in my app.js file to play around with Express in the new app, to set the base directory as /newapp/ - to serve both static files and routes from there. Any ideas on how to do this?
In the newapp, I'm serving static files as such:
// Serve files out of ./public
app.use(express.static(__dirname + '/public'));
and have route files as:
var index = require('./routes/index');
app.use('/', index);
with index routing file:
var express = require('express');
var router = express.Router();
// Get index page
router.get('/', function(req, res, next) {
res.render('index', {
index : 'active'
});
});
module.exports = router;
First of all, don't use regexp location if you no need it. Use simple location. And about your question - put / at the end of proxy_pass URI. Nginx will rewrite /newapp/xxx to /xxx and vice versa (for http redirects, for example). But (!) will not rewrite links in HTML body.
location /newapp/ {
proxy_pass http://127.0.0.1:6002/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
I have my express server running on port 3000 with nginx for the reverse proxy.
req.ip always returns 127.0.0.1 and req.ips returns an empty array
app.enable('trust proxy');
With/without enabling trust proxy, x-forwarded-for doesn't work:
var ip_addr = req.headers['X-FORWARDED-FOR'] || req.connection.remoteAddress;
nginx configuration:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/dev_localhost.log;
location / {
proxy_pass http://localhost:3000;
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;
}
}
How do i get the IP address of the requesting client?
You need to pass the appropriate X-Forwarded-For header to your upstream. Add these lines to your upstream config:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
According to the express documentation:
Express behind proxies
When running an Express app behind a proxy, set (by using app.set()) the application variable trust proxy to one of the values listed in the following table.
You can set it to a boolean, ip address, number or a custom function. If you want to just get the client's proxy to your express app's req.ip, you can just set it to true.
app.set('trust proxy',true);
app.get("/", (req, res)=>{
console.log(req.ip);
});