如何代理角应用nginx的上轨盒子? [英] How to proxy angular app with nginx on a rails box?

查看:76
本文介绍了如何代理角应用nginx的上轨盒子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域和两个服务器,一个运行轨道一个角的js。

I have one domain and two servers, one running rails one angular js.

域名指向机器上运行轨道上。

The domain name points to the box running rails.

我想使用的路径为命名空间通过proxy_pass担任了我的棱角部位。

I want to use a path as a namespace to serve up my angular site via proxy_pass.

例如..

mydomain.com /#/管理员 mydomain.com/admin < - 这应该proxy_pass到角服务器

mydomain.com/#/admin or mydomain.com/admin <-- This should proxy_pass to the angular server

mydomain.com mydomain.com/anything-except-admin &LT; - 这应该成为了轨含量

mydomain.com or mydomain.com/anything-except-admin <-- this should serve up rails content

我试过5月的事情得到这个工作,但我遇到的问题是:

I've tried may things to get this to work, but the problems I run into are:

问题1

角使用的根路径中#符号。 Rails会永远间preT为根也是#符号。

Angular uses the '#' symbol at the root path. Rails will always interpret the '#' symbol as root also.

因此​​,从 mydomain.com /#/ 送达的任何事情是轨道PTED为 mydomain.com /

So any thing served from mydomain.com/#/ is interpreted by rails as mydomain.com/

这意味着 mydomain.com /#/管理员被认为是 mydomain.com /

但角度所需要的#符号,所以角应用程序不会在这种情况下被发现。

But angular needs the '#' symbol, so the angular app will not be found in this case.

问题2

作为试图解决的第一个问题,有在角的方式,以避免使用#符号。

As an attempt to resolve the first problem, there is a way in angular to avoid using the '#' symbol.

$ locationProvider.html5Mode(真)

虽然这确实部分解决第一个问题,它引入了另一种。例如:

While this does in part solve the first problem, it introduces another. For instance:

mydomain.com/admin &LT; - 这工作来提供角应用

mydomain.com/admin <-- this works to serve up the angular app

在这里,当我浏览角度应用作为用户会在浏览器中,一切都很好。但是,如果我刷新页面,我根据我怎么样了设置得到404或nginx的错误proxy_pass

Once here, as I navigate the angular app as a user would in the browser, all is fine. But if I refresh the page, I get a 404 or an nginx error depending on how I've setup the proxy_pass

例如,如果我在这里导航点击周围的角应用:

For example, if I navigated here by clicking around the angular app:

mydomain.com/admin/dashboard 然后我刷新页面,这是PTED作为一个新的GET请求该路径间$ P $,我不能让该proxy_pass的请求传递到角,所以它总是打Rails应用程序。由于在轨这条路径的路由我收到了404。

mydomain.com/admin/dashboard then I refresh the page, it is interpreted as a new GET request to that path, and I could not get the proxy_pass to pass the request to angular, so it always hit the rails app. Since there is no route for this path in rails I get a 404.

问题3

最后,它已经提出使用自定义子域。不幸的是,我不能因为这个应用程序需要通配符子域的多租户身份验证的一部分。这是不可更改的业务规则。

Finally, it's been proposed to use custom subdomains. Unfortunately I can't because this app requires wildcard subdomains as part of the multi-tenant authentication. This is business rule that can't be changed.

任何想法?

推荐答案

您需要类似的东西:

server {
    # implemented by default, change if you need different ip or port
    # listen *:80 | *:8000;

    # same as server_name  yoursite.com  www.yoursite.com  *.yoursite.com;
    server_name .yoursite.com;

    location / {
            try_files $uri $uri/ @ruby;
    }

    location ~ ^/(\#/)?(admin) {
            try_files $uri $uri/ @angular;
    }

# there must be configured @ruby and @angular locations
<...>
}

下面是服务器名文档和的 的位置,请给它一个尝试,并告诉我们是否会有任何问题。
为了调试地点键,整机配置你最好的朋友是 /var/log/nginx/access.log /var/log/nginx/error.log

Here is documentation on server_name and locations, please give it a try and tell us if there will be any problems. For debugging locations and whole configuration your best friends are /var/log/nginx/access.log and /var/log/nginx/error.log.

这篇关于如何代理角应用nginx的上轨盒子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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