NGINX:使用域/路径在同一服务器上的多个节点js应用程序 [英] NGINX : multiple node js applications on same server using domain/path
问题描述
问题
如果我有一些节点js应用程序,并且要将其发布为mydomain.com/app1、mydomain.com/app2等,则必须将 app.get'/'更改为 app.get('/app1',在某些情况下还包括css,js和图片路径.
If I have some node js apps and if I want publish it as mydomain.com/app1, mydomain.com/app2, etc I have to change the app.get '/' to app.get('/app1' and also in some cases css,js and images paths.
问题
要分配域/路径时,是否应该始终修改应用程序?
Should you always modify the application when you want to assign a domain/path?
有什么方法可以使应用程序独立吗?
Is there any way to make the application independent?
是nodejs还是nginx配置?
Is a nodejs or nginx configuration?
这是一个示例的节点js应用程序:
https://github.com/jrichardsz/sensitive_web1.1 /blob/master/server.js
这是我的 mydomain.com 节点js应用程序的Nginx配置(有效!)
This is my nginx configuration for my node js app for mydomain.com (works!)
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://localhost:8080/;
}
}
节点应用程序:
app.get('/', function(req, res) {
// ejs render automatically looks in the views folder
res.render('index');
});
这是我对同一节点js应用程序的nginx配置,但 mydomain.com/app1 (有效!)
This is my nginx configuration for the same node js app but mydomain.com/app1 (works!)
server {
listen 80;
server_name mydomain.com;
location /app1/ {
proxy_pass http://localhost:8080/app1/;
}
}
这是Node js应用程序中的修复程序
And this is the fix in node js app
app.get('/app1', function(req, res) {
// ejs render automatically looks in the views folder
res.render('index');
});
我尝试过:
https://github.com/expressjs/express-namespace
http://expressjs.com/en/4x/api.html
但是在两种情况下,我都需要更改我的node js应用.
But in both cases, I need change my node js app.
谢谢.
推荐答案
要分配域/路径时,是否应该始终修改应用程序?
Should you always modify the application when you want to assign a domain/path?
不,您根本不需要修改应用程序.
No, you shouldn't have to modify the application at all.
以这种方式使用proxy_pass
时,需要用正则表达式重写URL.尝试这样的事情:
When you use proxy_pass
in this manner, you need to rewrite the URL with regex. Try something like this:
location ~ ^/app1/(.*)$ {
proxy_pass http://localhost:8080/$1$is_args$args;
}
另请参阅: https://serverfault.com/q/562756/52951
这篇关于NGINX:使用域/路径在同一服务器上的多个节点js应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!