[英]How to proxy angular app with nginx on a rails box?
I have one domain and two servers, one running rails one angular js. 我有一个域和两台服务器,一台运行着一个角度js。
The domain name points to the box running rails. 域名指向盒子滑轨。
I want to use a path as a namespace to serve up my angular site via proxy_pass. 我想使用路径作为命名空间来通过proxy_pass服务我的角度站点。
For example.. 例如..
mydomain.com/#/admin
or mydomain.com/admin
<-- This should proxy_pass to the angular server mydomain.com/#/admin
或mydomain.com/admin
<-这应该proxy_pass到角度服务器
mydomain.com
or mydomain.com/anything-except-admin
<-- this should serve up rails content mydomain.com
或mydomain.com/anything-except-admin
<-这应该提供Rails内容
I've tried may things to get this to work, but the problems I run into are: 我尝试了一些可能使它起作用的方法,但是遇到的问题是:
Problem 1 问题1
Angular uses the '#' symbol at the root path. Angular在根路径处使用“#”符号。 Rails will always interpret the '#' symbol as root also.
Rails还将始终将'#'符号解释为root。
So any thing served from mydomain.com/#/
is interpreted by rails as mydomain.com/
因此,由
mydomain.com/#/
任何内容都会被rails解释为mydomain.com/
This means that mydomain.com/#/admin
is considered mydomain.com/
这意味着
mydomain.com/#/admin
被视为mydomain.com/
But angular needs the '#' symbol, so the angular app will not be found in this case. 但是角度需要使用“#”符号,因此在这种情况下将找不到角度应用。
Problem 2 问题2
As an attempt to resolve the first problem, there is a way in angular to avoid using the '#' symbol. 为了解决第一个问题,有一种方法可以避免使用“#”符号。
$locationProvider.html5Mode(true)
While this does in part solve the first problem, it introduces another. 尽管这部分解决了第一个问题,但它引入了另一个问题。 For instance:
例如:
mydomain.com/admin
<-- this works to serve up the angular app mydomain.com/admin
<-这可以提供角度应用程序
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
但是,如果刷新页面,则将显示404或Nginx错误,具体取决于我如何设置proxy_pass
For example, if I navigated here by clicking around the angular app: 例如,如果我通过单击有角度的应用程序导航到这里:
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. mydomain.com/admin/dashboard
然后刷新页面,它被解释为对该路径的新GET请求,并且我无法获取proxy_pass将该请求传递给angular,因此它总是会触动Rails应用程序。 Since there is no route for this path in rails I get a 404. 由于在rails中没有此路径的路线,所以我得到了404。
Problem 3 问题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.
这是无法更改的业务规则。
Any ideas? 有任何想法吗?
You need something like that: 您需要这样的东西:
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
<...>
}
Here is documentation on server_name and locations , please give it a try and tell us if there will be any problems. 这是关于server_name和location的文档,请尝试一下,告诉我们是否有任何问题。 For debugging
locations
and whole configuration your best friends are /var/log/nginx/access.log
and /var/log/nginx/error.log
. 对于调试
locations
和整个配置,最好的朋友是/var/log/nginx/access.log
和/var/log/nginx/error.log
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.