Lua 風格的 Nginx 重寫或內部重新導向循環

Lua 風格的 Nginx 重寫或內部重新導向循環

我對 和 都是nginx新手lua。我正在嘗試按照本教程進行操作http://leafo.net/posts/creating_an_image_server.html#installation_requirements但我一生都無法得到nginx.conf正確的答案。

我檢查過類似的其他問題問題出在哪裡location,但我看不出我的程式碼有什麼問題。

這裡是nginx.conf

error_log stderr notice;
daemon off;

events { }

http {
  include /usr/local/openresty/nginx/conf/mime.types;

  server {
    listen 6789;
    lua_code_cache on;

    location @image_server {
      content_by_lua_file "serve_image.lua";
    }

    location ~ ^/images/(?<size>[^/]+)/(?<path>.*\.(?<ext>[a-z_]*))$ {
      root cache;
      try_files /$path @image_server;
    }

  }
}

這是serve_image.lua程式碼

local sig, size, path, ext =

ngx.var.sig, ngx.var.size, ngx.var.path, ngx.var.ext local images_dir = "images/" -- 映像來自本地 cache_dir = "cache/" -- 映像快取的地方

local function return_not_found(msg)
  ngx.status = ngx.HTTP_NOT_FOUND
  ngx.header["Content-type"] = "text/html"
  ngx.say(msg or "not found")
  ngx.exit(0)
end

local source_fname = images_dir .. path
ngx.log(ngx.STDERR, "->>>>  " .. source_fname)

-- make sure the file exists
local file = io.open(source_fname)

if not file then
  ngx.log(ngx.STDERR, "Couldn't find the input file" .. source_fname)
  return_not_found()
end

file:close()

local dest_fname = cache_dir .. "/" .. size .. "/" .. path

-- resize the image
local magick = require("magick")
magick.thumb(source_fname, size, dest_fname)

ngx.exec(ngx.var.request_uri)

當我運行伺服器時,我得到代碼 500。

這是終端機中的錯誤

rewrite or internal redirection cycle while redirect to named location "@image_server", client: 127.0.0.1, server: , request: "GET /images/80x80/wow.png HTTP/1.1", host: "localhost:6789"

我希望有一雙更有經驗和新鮮的眼睛能夠指出我做錯了什麼。謝謝。

相關內容