【问题标题】:404 page not found when a url is hit but properly served when opened from the link on index page命中 url 时找不到 404 页面,但从索引页面上的链接打开时正确提供
【发布时间】:2016-09-25 14:37:09
【问题描述】:

我正在使用带有redis 的nginx-lua 模块来提供ember-app 的静态文件。 index 文件内容作为value 存储在redis 中,当(根)domain/IP 被击中时,nginx 将正确地提供它。

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

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)

     ';
 }

【问题讨论】:

  • 是不是一个用post一个get,而服务器只响应一个?
  • @randy 不,这些只是 GET 请求。就像mydomain/login 通过来自index 页面的链接打开时一样,它会得到正确的服务。但是通过点击网址栏或刷新页面打开时,它显示404 page not found

标签: nginx ember.js lua


【解决方案1】:

必须添加以下 nginx 位置块,以便从 redis 提供的索引文件中提供子路由。详细解释和完整的nginx配置可以在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;
  }

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2021-08-15
    • 1970-01-01
    • 2015-04-26
    • 2019-02-20
    相关资源
    最近更新 更多