当一个 url 被点击时找不到 404 页面,但从索引页面上的链接打开时可以正确提供 [英] 404 page not found when a url is hit but properly served when opened from the link on index page

查看:17
本文介绍了当一个 url 被点击时找不到 404 页面,但从索引页面上的链接打开时可以正确提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nginx-lua 模块和 redis 来提供 ember-app 的静态文件.index 文件内容作为 value 存储在 redis 中,当 (root) domain/IP 被命中.

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.

如果 login 页面是从链接打开的,它会被正确打开.但是当直接通过点击 url 栏或刷新页面打开时,nginx 给出 404 not found.index 文件位于 redis 中,其余文件由编译后的 js 提供,该文件位于 CDN 上>.以下是nginx配置

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)

     ';
 }

推荐答案

必须添加以下 nginx 位置块,以便为从 redis 提供的索引文件中的子路由提供服务.可以找到详细的解释和完整的 nginx 配置 这里.

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

这篇关于当一个 url 被点击时找不到 404 页面,但从索引页面上的链接打开时可以正确提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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