• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
nginx – 当网址被点击时找不到404页面,但是当从索引页面上的链接打开时正确提供

https://www.jb51.cc/nginx/434443.html

https://segmentfault.com/q/1010000040742954?utm_source=sf-similar-question

https://blog.csdn.net/liming1016/article/details/105685563

我正在使用带有redis的Nginx-lua模块来提供ember-app的静态文件.索引文件内容存储在redis中,作为当(根)域/ IP被命中时由Nginx正确提供的值.

如果登录页面是从链接打开的,则会正确打开.但是当通过点击url栏或刷新页面直接打开时,Nginx会找不到404.
索引文件是redis,其余文件是从CDN上存在的已编译的js提供的.
以下是Nginx配置

 

  1. server
  2. {
  3. listen 80 ;
  4. server_name 52.74.57.154;
  5. root /;
  6. default_type text/html;
  7. location = / {
  8. try_files $uri $uri/ /index.html?/$request_uri;
  9. set_unescape_uri $key $arg_index_key;
  10. set $fullkey 'ember-deploy-cli:index:${key}';
  11. content_by_lua '
  12. local redis = require "resty.redis"
  13. local red = redis:new()
  14. red:set_timeout(1000) -- 1 sec
  15. local ok,err = red:connect("127.0.0.1",6379)
  16. if not ok then
  17. ngx.say("Failed to connect: ",err)
  18. return
  19. end
  20. if ngx.var.key == "" then
  21. --ngx.say("No Argument passed")
  22. local res,err = red:get("ember-deploy-cli:index:current-content")
  23. ngx.say(res)
  24. return
  25. end
  26. local res,err = red:get(ngx.var.fullkey)
  27. if res == ngx.null then
  28. ngx.say("Key doesnt exist ")
  29. return
  30. end
  31. ngx.say(res)
  32. ';
  33. }
最佳答案
必须添加以下Nginx位置块,以便从redis提供的索引文件中提供子路由.详细说明和完整的Nginx配置可以找到here.

 

 

    1. # 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).
    2. location ~* / {
    3. rewrite ^ / last;
    4. }
posted on 2021-12-12 22:28  红无酒伤  阅读(114)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3