简体   繁体   中英

How to redirect all traffic to Angular app inside Rails (using Nginx)

Ok, basically I'm using Nginx, Passenger, Rails and Angular. I've created an Angular app that compiles to Rails' public folder. Rails provides the API underneath the Angular app.

I'd like to have all traffic that comes www.mydomain.com/admin to this admin.html file located in the public directory. I've got a route in Rails config/routes.rb pointing to admin.html like so:

match     '/admin/*all' => redirect("/admin.html"), via: 'get'

But that doesn't seem to do the trick. I also would like Nginx to redirect all traffic with admin/* to this admin.html file in public folder.

By the way, the admin.html is the Angular app.

EDIT: The Angular app is not being served by Rails

At the moment, there's no views in Rails at all. Rails is just an API underneath a standalone Angular app that happens to be in the public directory inside Rails app. I've chosen this approach at this stage for two reasons: One I might deploy the Angular on a separate server at later stage, but not right now. So keeping the Angular completely independent and separate is a good idea. Two, At the moment, keeping the Angular on top of the Rails app will help reduce latency.

The whole setup is part of an experience that will evolve, I wanted to keep my options open moving forward.

Add a route to an action that loads the angular application, then have your normal resources (probably APIs) in the routes.rb afterwards

routes.rb

match 'admin', to: 'admin#angular', via: :all

admin_controller.rb

def angular
    render 'layouts/admin'
end

Then layouts/admin.html.haml is your Angular application

Reference: https://thinkster.io/angular-rails/ (very good tutorial btw)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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