# ngx_mruby inline code configuration
worker_processes auto;
events {
worker_connections 1024;
}
http {
# Inline mruby initialization
mruby_init_code '
Userdata.new.shared_data = "initialized"
';
mruby_init_worker_code '
Userdata.new.worker_id = Process.pid
';
server {
listen 80;
server_name mruby-inline.example.com;
# Inline content handler
location /hello {
mruby_content_handler_code '
Nginx.echo "Hello from mruby!"
Nginx.echo "Request URI: #{Nginx::Request.new.uri}"
';
}
# Inline access handler
location /api {
mruby_access_handler_code '
request = Nginx::Request.new
token = request.headers_in["Authorization"]
if token.nil? || token.empty?
Nginx.return Nginx::HTTP_UNAUTHORIZED
end
';
proxy_pass http://127.0.0.1:8080;
}
# Inline rewrite handler
location /old {
mruby_rewrite_handler_code '
request = Nginx::Request.new
if request.uri =~ /^\/old\/(.*)/
Nginx.redirect "/new/#{$1}"
end
';
}
# Inline set handler
location /dynamic {
mruby_set_code $greeting '
"Hello, #{Time.now}"
';
add_header X-Greeting $greeting;
return 200 "OK";
}
# Inline log handler
location /logged {
mruby_log_handler_code '
request = Nginx::Request.new
status = Nginx::Var.new.status
Nginx.errlogger Nginx::LOG_INFO, "Request: #{request.uri} Status: #{status}"
';
proxy_pass http://127.0.0.1:8080;
}
# Inline output filter
location /filtered {
mruby_output_body_filter_code '
body = Nginx::Filter.new.body
Nginx::Filter.new.body = body.gsub("foo", "bar")
';
proxy_pass http://127.0.0.1:8080;
}
location / {
root /var/www/html;
index index.html;
}
}
}