Nginx代理到远程Node.js Express应用程序在子目录中 [英] nginx proxy to remote node.js express app in subdirectory

查看:126
本文介绍了Nginx代理到远程Node.js Express应用程序在子目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全陷入一种情况,即我想在一台服务器上有多个节点应用程序.通过使应用程序在不同的端口上运行,我可以很好地工作.我可以通过在IP地址中输入port来访问应用程序.

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.

我想通过使用像这样的不同子目录来代理我的Nginx服务器中的应用程序:

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

my.domain

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

为此,我必须将所有快速路由移至application1的/app1.可以,但是现在我只能使用静态文件.

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.

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

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/css 访问静态文件,但不能直接访问通过代理 http://my.domain/css

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

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

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.

现在,我希望至少可以通过nginx代理获取静态文件.

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

推荐答案

需要的人的最新答案:

代替

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

使用

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

或者在本地

location /app1/ {
  proxy_pass http://localhost:3000/;
}

这是正确的方法,因此您无需修改​​express. Express只会接收/app1/

This is the correct way and this way you will not need to modify express. Express will receive only the part after /app1/

这篇关于Nginx代理到远程Node.js Express应用程序在子目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆