Monday 6 June 2016

Ember CLI Deploy Lightning Pack Nginx Config

 Sample Nginx Config for Ember Redis CLI Deploy

How to add subroutes to index page served by Redis


Ember CLI Deploy Lightning Pack is an ember-cli-deploy plugin to implement lightning deployment pattern. To use it the instructions can be found at its github page.

To make use of this plugin, the index page is stored in redis as a value and when the browser requests for the page, the web-server (Nginx) gets the value(HTML content) of the key passed as an argument in the URL. If the key is found it's value is rendered and if key is not found or the key is not passed as an argument than the default key-value is served.

One problem may be encountered while serving the subroutes or subrequests. The index.html page is served by the redis as a value stored for a particular key. But when a subrequest, say, http://mydomain.com/login is hit with the url or the page is refreshed, the browser sends a requests to nginx and nginx won't be able to find the login page anywhere and will return a 404 error. This is because nginx won't be able to pass the subroutes to index.html page which in turn can serve the subroutes. To solve this the  following location block is used in nginx.


  # This block handles the subrequest. If any subroutes are requested than this rewrite the url to root and tries to render the subroute page by passing the subroute to index file (which is served by the redis).
  location ~* / {
  rewrite ^ / last;
  }

Here we are saying to nginx, for any subrequests, rewrite the url to root (/) location (root location serves the index page from redis) and find the requested page. The last option tries to find the particular page by revisiting all the blocks defined in nginx as a result of which it is able to go to root location.
 
Following are the pre-requisite.
  • Get the lua-resty-redis packate, untar it and save it at /opt/
  • Download the latest version of the ngx_devel_kit (NDK) module HERE.
  • Download the latest version of ngx_lua HERE.
  •  Install above two module as described HERE.

Now after successful setup of above, we need to create a nginx config file say /etc/nginx/conf.d/ember.conf. Following is the sample nginx conf file with explanation in comments.



# Include the below lua-resty-redis package till lib directory and don't forget to add ?.lua;; at the end.
lua_package_path "/opt/lua-resty-redis-0.22/lib/?.lua;;";

server
{
  listen 80 ;
  server_name mydomain.com;

  default_type   text/html;
  index index.html index.htm;

  location = / {
        # Get the parameter being passed from URL
        # Example: mydomain.com/?index_key=mykey
 set_unescape_uri $key $arg_index_key;

        # Add prefix to the argument passed.
        # Ignore and use the $key variable instead of $fullkey if you are not adding the prefix
 set $fullkey 'ember-deploy-cli:index:${key}';

        # Use of lua language
        content_by_lua '

                -- This is a comment
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeout(1000) -- 1 sec

                -- Try to connect to redis running on localhost
                local ok, err = red:connect("127.0.0.1", 6379)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

                # $key variable are access by ngx.var.key in lua
  if ngx.var.key == "" then
   --if no key is not passed serve default key content
   local res, err = red:get("default")
   ngx.say(res)
   return
  end

  local res, err = red:get(ngx.var.fullkey)
  if res == ngx.null then
   ngx.say("Key doesnt exist ")
   return
  end

  if not res then
   ngx.say("page not found")
  end
  ngx.say(res)
     ';
  }

  # This block handles the subrequest. If any subroutes are requested than this rewrite the url to root and tries to render the subroute page by passing the subroute to index file (which is served by the redis).
  location ~* / {
  rewrite ^ / last;
  }
}

To access the default key with mydomain.com simply hit the url http://mydomain.com. To access the html content of a particular key stored in redis hit the url http://mydomain.com/?index_key=mykey.

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel