简体   繁体   English

从子文件夹中提供Express.JS应用程序

[英]Serve Express.JS app from subfolder

I use nginx to serve a static html site and a expressjs app under the same domain. 我使用nginx在同一个域下提供静态html站点和expressjs应用程序。 My nginx config looks like this: 我的nginx配置看起来像这样:

    location / {
            try_files $uri $uri/ /index.html;
            autoindex  off;
            root /var/www/example.com/static/;
    }

    location /admin {
            proxy_pass http://localhost:3007/;
            proxy_set_header Host $host;
            proxy_buffering off;
            autoindex  off;
    }

I'm able to access my app running on port 3007 if i access example.com/admin so it seems that my app.get('/', routes.index); 如果我访问example.com/admin,我可以访问在端口3007上运行的应用程序,所以看起来我的app.get('/', routes.index); is working but I'm having some troubles getting my other routes to work. 正在努力,但我有一些麻烦让我的其他路线工作。

For example: 例如:

app.get('/admin/test', function(req, res) {
    console.log("test!")
});

If I try to access example.com/admin/test I'll just see Cannot GET //test and a 404 according to my logs: 如果我尝试访问example.com/admin/test我只会看到根据我的日志Cannot GET //test和404:

GET //test 404 11ms

If I change the route to /test it's the same problem. 如果我改变路线/测试它是同样的问题。 Any pointers what's wrong there. 任何指针都有什么不对。 I haven't found a way to make routes relative to the base path they are mounted to (/admin). 我还没有找到一种方法来相对于它们所安装的基本路径(/ admin)制作路由。

Thanks! 谢谢!

Mind the slash: 记住斜线:

location /admin {
    proxy_pass http://localhost:3007/; # here
    …
}

If you remove it, the real request URI will be proxied. 如果删除它,将代理实际请求URI。

You can also remove the /admin part with Nginx: 您还可以使用Nginx删除/admin部分:

location ~ ^/admin($|/.*$) {
    proxy_pass http://localhost:3007$1;
    …
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM