I'm new both to nginx
and lua
. I'm trying to follow this tutorial http://leafo.net/posts/creating_an_image_server.html#installation_requirements but for the life of me I can't get the nginx.conf
right.
I've checked other questions similar to this where the problem is location
, but I can't see what wrong with my code.
Here is the 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;
}
}
}
And this is the serve_image.lua
code
local sig, size, path, ext =
ngx.var.sig, ngx.var.size, ngx.var.path, ngx.var.ext local images_dir = "images/" -- where images come from local cache_dir = "cache/" -- where images are cached
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)
When I run the server I get a code 500.
This is the error in the terminal
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"
I'm hoping a more experienced and fresh pair of eyes can point out what I'm doing wrong. Thanks.