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.
Related
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.
I created two node.js instance on DigitalOcean's Ubuntu machine.
Both listen to localhost, with different ports.
Then test by curl http:\localhost:${port}. Both worked.
Then I added them to /etc/nginx/site-available/default
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://localhost:8837;
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;
}
location /app {
proxy_pass http://localhost:9837;
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;
}
Only http://${ip_address}:${port}/ worked.
http://${ip_address}:${port}/app return
"Cannot GET /app"
I used express in node.js is it the problem come from?
I don't know where is wrong, here is what I found:
If remove express from node.js and listen port like this:
var http = require('http');
http.createServer(function (req, res) {
...
}).listen(8001, 'localhost');
The /app worked, no error happen.
If use express like this:
const app = express();
app.listen(8001, 'localhost', function() {
...
});
/app will return error.
To resolve this problem I open another port that create another configuration by copy /etc/nginx/sites-available/default ,then link it to /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/default2 /etc/nginx/sites-enabled/
and change the port in server{}
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 nginx running as a proxy server along with a couple of node.js applications. I have nginx port forwarding to each app in distinct ports. I have one main server file that looks like this in sites-enabled:
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app1 {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app2 {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Each app is running the following code in the /var/www/html directory with a port variable assigned to the above ports respectively:
var http = require('http');
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Under Construction\n');
}).listen(port, '127.0.01');
This file doesn't look maintainable if you have a large amount of applications. What are some other ways to develop this?
I use express to listen to the ports I need without ever having to use nginx.
app.listen(port, function() {
console.log('listening on port ', port);
}
I'm not sure if this is best practice, but this has abstracted the routing from nginx to express like #jfriend00 suggested.
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);
});