Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 page not found when a url is hit but properly served when opened from the link on index page

I am using nginx-lua module with redis to serve static files of ember-app. The index file content is stored in redis as a value which is being properly served by nginx when the (root) domain/IP is hit.

If login page is open from link, it gets opened properly. But when opened directly by hitting the url bar or refreshing the page the nginx gives 404 not found. The index file is in redis and rest of the files are being served from compiled js which is present on a CDN. Following is the nginx configuration

server
{
  listen 80 ;
  server_name 52.74.57.154;

  root /;

 default_type   text/html;
 location = / {
    try_files $uri $uri/ /index.html?/$request_uri;
    set_unescape_uri $key $arg_index_key;
    set $fullkey 'ember-deploy-cli:index:${key}';

     content_by_lua '
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeout(1000) -- 1 sec



                local ok, err = red:connect("127.0.0.1", 6379)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end


        if ngx.var.key == "" then
            --ngx.say("No Argument passed")
            local res, err = red:get("ember-deploy-cli:index:current-content")
            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
        ngx.say(res)

     ';
 }
like image 352
drishti ahuja Avatar asked May 27 '16 09:05

drishti ahuja


1 Answers

Following nginx location block has to be added in order to serve the subroutes from index file being served from redis. A detailed explanation and full nginx config can be found here.

  # 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;
  }
like image 178
Ajeet Khan Avatar answered Nov 15 '22 09:11

Ajeet Khan