Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

nginx - Serve Rails API and Ionic mobile website together

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. loading html, js, css files from the mobile/www/ directory
  2. mobile website and APP JSON calls to Rails API
  3. desktop website HTML calls to Rails

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 config:

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;
  }
}

In 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

Two birds with one APP :).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...