nginx代理到远程node.js表达子目录中的app

时间:2021-10-23 03:04:47

I am completely stuck with a situation where I want to have several node applications on one server. I get this working fine by having the applications running on different ports. I can access the applications by putting in the ip address with port.

我完全陷入了一种情况,我想在一台服务器上安装多个节点应用程序。通过让应用程序在不同的端口上运行,我可以正常工作。我可以通过输入带端口的IP地址来访问应用程序。

I would like to proxy the applications from my nginx server by using different sub-directories like so:

我想通过使用不同的子目录从我的nginx服务器代理应用程序,如下所示:

my.domain

location /app1 {
  proxy_pass http://10.131.6.181:3001;
}
location /app2 {
  proxy_pass http://10.131.6.181:3002;
}

Doing this I had to move the all the express routes to /app1 for application1. This works but now I am stuck with the static files.

这样做我必须将所有快速路由移动到/ app1 for application1。这有效,但现在我被静态文件困住了。

I can now access the application with http://10.131.6.181:3001/app1 which is great, but via http://my.domain/app1 the static files are not loaded.

我现在可以使用http://10.131.6.181:3001/app1访问该应用程序,这很棒,但是通过http://my.domain/app1,不会加载静态文件。

The static files can be accessed directly http://10.131.6.181:3001/css but not via the proxy http://my.domain/css

静态文件可以直接访问http://10.131.6.181:3001/css但不能通过代理http://my.domain/css

Ideally I would like to have the applications on different ports without the sub-directory in the express routes but only sub-directories in the proxy. I tried to put my head through the wall for the last 5 hours but didn't achieve anything.

理想情况下,我希望将应用程序放在不同的端口上,而不在快速路由中使用子目录,而只在代理中使用子目录。在过去的5个小时里,我试图将头穿过墙壁,但没有取得任何成果。

Now I would happy if can at least get the static files via the nginx proxy.

现在我很高兴,如果至少可以通过nginx代理获取静态文件。

1 个解决方案

#1


0  

I finally worked it out after a google surge.

谷歌激增之后我终于解决了这个问题。

I added the directories to the nginx proxy_pass

我将目录添加到nginx proxy_pass中

my.domain

location /app1 {
  proxy_pass http://10.131.6.181:3001/app1;
}
location /app2 {
  proxy_pass http://10.131.6.181:3002/app2;
}

And I had to change the express applications to use the subdirectory

我不得不更改快速应用程序以使用该子目录

app.use('/app1', express.static(path.join(__dirname, 'public')));

app.use('/app1'', require('./routes'));

In the router I had to prefix all the redirects.

在路由器中,我必须为所有重定向添加前缀。

router.get('/logout', function (req, res) {
  req.logout();
  res.redirect('/app1/login');
});

The static files are called like so from html

从html中调用静态文件

<link rel="stylesheet" href="/app1/css/style.css"/>

A bit of a pain to change all the redirects and static url. I am sure there is a smarter way by setting a global variable in my node-express app. If anybody knows an easier way please post...

改变所有重定向和静态URL有点痛苦。我确信通过在我的node-express app中设置全局变量有一种更聪明的方法。如果有人知道更简单的方法,请发布...

#1


0  

I finally worked it out after a google surge.

谷歌激增之后我终于解决了这个问题。

I added the directories to the nginx proxy_pass

我将目录添加到nginx proxy_pass中

my.domain

location /app1 {
  proxy_pass http://10.131.6.181:3001/app1;
}
location /app2 {
  proxy_pass http://10.131.6.181:3002/app2;
}

And I had to change the express applications to use the subdirectory

我不得不更改快速应用程序以使用该子目录

app.use('/app1', express.static(path.join(__dirname, 'public')));

app.use('/app1'', require('./routes'));

In the router I had to prefix all the redirects.

在路由器中,我必须为所有重定向添加前缀。

router.get('/logout', function (req, res) {
  req.logout();
  res.redirect('/app1/login');
});

The static files are called like so from html

从html中调用静态文件

<link rel="stylesheet" href="/app1/css/style.css"/>

A bit of a pain to change all the redirects and static url. I am sure there is a smarter way by setting a global variable in my node-express app. If anybody knows an easier way please post...

改变所有重定向和静态URL有点痛苦。我确信通过在我的node-express app中设置全局变量有一种更聪明的方法。如果有人知道更简单的方法,请发布...