一起服务 Rails API 和 Ionic 移动网站 [英] Serve Rails API and Ionic mobile website together

查看:17
本文介绍了一起服务 Rails API 和 Ionic 移动网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于 如何永久运行 Ionic serve?将 Ionic 部署为网站,nginx 应该能够提供 Ionic 的 www 文件夹中的代码.我正在利用使用相同域地址为其 Rails 后端提供服务的想法......这样就不会增加 CORS 流量和开销.Rails WEB 的另一个要求是仍然处理网站的桌面 (HTML) 版本.本质上,将有 3 种类型的请求到达 nginx 服务器:

Basing on How to run Ionic serve permanently? and Deploy Ionic as a website, nginx should be able to serve the code from the Ionic's www folder. I am exploiting the idea of serving it with its Rails back-end together using the same domain address... so that no CORS traffic and overhead will be added. Another requirement for the Rails WEB is to still handle the desktop (HTML) version of the website. Essentially, there will be 3 types of requests coming to the nginx server:

  1. 从 mobile/www/目录加载 html、js、css 文件
  2. 移动网站和应用程序对 Rails API 的 JSON 调用
  3. 桌面网站对 Rails 的 HTML 调用

类型 2 请求可能很简单,因为它们都具有 .json 扩展名.子域由用户名占用,即 username.example.com,关于如何让 nginx 正确路由 html、js 和 css 请求的任何想法?或者这是否是一个太大的挑战?

Type 2 requests may be simple because they all have the .json extension. With sub-domains are taken by the username, i.e. username.example.com, any ideas on how to have nginx route the html, js, and css requests correctly? Or is this too much of a challenge?

推荐答案

采取 #1:提出一个 Nginx 配置,当 Rails 以隐藏的方式向它发出信号时,该配置返回 Ionic 文件.可能会很笨拙,所以请随时提出批评、陷阱或改进.

Take #1: Come up with a Nginx config that returns Ionic files when Rails signals it in a hidden manner. May be clumsy, so please feel free to offer criticism, pitfalls, or improvements.

Nginx 配置:

server {
  # Development logging
  access_log /home/builder/projects/web/log/access.log;
  error_log /home/builder/projects/web/log/error.log notice;

  listen  80;
  server_name projects.host www.projects.host;

  # Eusure Rails' index route gets uri "/" first.
  index index.html;

  # All API json calls and requests to Rails controllers.
  location ~ ^/(.+.json$|others.*|users.*|index.html$) {

    # Rails server
    proxy_pass  http://127.0.0.1:3000;

    # The Rails server may request Ionic mobile website with a temporary redirect (status 307)
    proxy_intercept_errors on;
    error_page 307 = @temp_redirect;
  }

  # If a temporary redirect is to /mobile_web, response with Ionic mobile root.
  location @temp_redirect {
    if ($upstream_http_location ~ ^http.+//.+/mobile_web$) {
      set $mobile true;
      root /home/builder/projects/mobile/www;
    }
    # Something else, return it.
    if ($mobile != true) {
      return 307 $upstream_http_location;
    }
  }

  # Ionic mobile root
  location / {
    root        /home/builder/projects/mobile/www;
  }
}

在 RoR 中:

  # Decide whether to handle the root action within Rails app or to
  # signal the downstream server (nginx) to return Ionic mobile web.
  def index
    # TODO: Needs a bit of logic before the following redirect.
    redirect_to '/mobile_web', status: :temporary_redirect  # 307
  end

两只鸟同一个APP :)

Two birds with one APP :).

这篇关于一起服务 Rails API 和 Ionic 移动网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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