I have 2 websites compiled using angular-universal (For SEO). I need to host both these websites on a single url. Based on url change it will be directed to the relevant website
Project structure
dist
- repair
-browser
-server
-server.js
- renew
- browser
- server
- server.js
I am new to this environment and this is the code i tried but the port is clashing and even changing the port is not working. Any help will be appreciated
app.get('/repair', (req, res, next) => {
try {
const port = process.env.PORT || 8080;
const server = require('./dist/repair/server.js');
server.app.listen(port, () => {
console.log(`Listening on: http://localhost:${port}`);
});
} catch (error) {
let err = new def.NError(500, messages.errInternalServerError, error.message);
next(err);
}
console.log("repair");
});
app.get('/renew', (req, res, next) => {
try {
const port = process.env.PORT || 8080;
const server = require('./dist/renew/server.js');
server.app.listen(port, () => {
console.log(`Listening on: http://localhost:${port}`);
});
} catch (error) {
let err = new def.NError(500, messages.errInternalServerError, error.message);
next(err);
}
console.log("renew");
});
var server = app.listen(process.env.PORT || 8080, function () {
console.log('server running', ' at port: ', 8080);
});
The best solution for your case is to use nginx (web server) proxy instead of nodejs redirection.
SOLUTION 1 : So below is the solution for proxying using nginx.
#worker_processes 2;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
log_format timed_combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time $pipe';
access_log /var/log/nginx/access.log timed_combined;
error_log /var/log/nginx/error.log;
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream api_server {
server 127.0.0.1:8042;
}
upstream admin_server {
server 127.0.0.1:8043;
}
server {
listen 80;
server_name _;
return 404;
}
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://api_server;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /proj-admin {
proxy_pass http://admin_server;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Replace the nginx.conf with the above config.
SOLUTION 2 : still if you want to proxy using nodejs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.