Node.js 使用node和nginx作为代理服务器创建多个应用程序的最佳实践是什么?
我让nginx作为代理服务器运行,同时还有几个node.js应用程序。我将nginx端口转发到不同端口中的每个应用程序。我有一个主服务器文件,在已启用的站点中看起来像这样:Node.js 使用node和nginx作为代理服务器创建多个应用程序的最佳实践是什么?,node.js,express,nginx,Node.js,Express,Nginx,我让nginx作为代理服务器运行,同时还有几个node.js应用程序。我将nginx端口转发到不同端口中的每个应用程序。我有一个主服务器文件,在已启用的站点中看起来像这样: server { listen 80; location / { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Connection 'up
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;
}
}
每个应用程序都在/var/www/html目录中运行以下代码,并将端口变量分别分配给上述端口:
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');
如果有大量应用程序,则此文件看起来不可维护。还有什么其他方法可以实现这一点呢?我使用express来监听我需要的端口,而不必使用nginx
app.listen(port, function() {
console.log('listening on port ', port);
}
我不确定这是否是最佳实践,但这已经将路由从nginx抽象为express,就像@jfriend00建议的那样。您通常会在web服务器上使用类似express的框架,以便为该服务器定义路由。然后,每个服务器定义自己的路由,nginx不必参与每个web服务器配置为处理的子路由。因此,在您的特定示例中,您需要在web服务器中为port 5000服务器中的
/app2/aboutus
定义一个路由。@jfriend00我得到了快速路由部分,但我在代理脚本中放了什么?我是否在每个应用程序的location指令的url末尾添加了某种类型的通配符,如*://*?