[英]Hosting 2 website on a single URL in Node.js
I have 2 websites compiled using angular-universal (For SEO). 我有2个使用角度通用(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. 针对您的情况的最佳解决方案是使用nginx(Web服务器)代理而不是nodejs重定向。
SOLUTION 1 : So below is the solution for proxying using nginx. 解决方案1:因此,以下是使用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. 用上面的配置替换nginx.conf。
SOLUTION 2 : still if you want to proxy using nodejs 解决方案2:如果您仍想使用Node.js代理,还是这样
How to redirect to another page in node.js 如何重定向到node.js中的另一个页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.