Skip to content

Instantly share code, notes, and snippets.

@athirn
Created January 6, 2016 00:23
Show Gist options
  • Save athirn/52fa88deef5311a393bb to your computer and use it in GitHub Desktop.
Save athirn/52fa88deef5311a393bb to your computer and use it in GitHub Desktop.
nginx & lua
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
lua_package_path "/opt/openresty/lualib/resty/?.lua;;";
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
default_type text/html;
echo "<a href=\"/whatismyip\">check your IP</a><br>";
echo "<a href=\"/redirect\">redirect</a><br>";
echo "<a href=\"/headers\">dump headers</a><br>";
}
location /redirect {
default_type text/html;
content_by_lua '
return ngx.redirect("https://www.surfeasy.com", ngx.HTTP_MOVED_TEMPORARILY)
';
}
location /whatismyip {
default_type text/html;
content_by_lua '
ngx.say(ngx.var.remote_addr)
';
}
location /headers {
default_type text/html;
content_by_lua '
local h = ngx.req.get_headers()
for k, v in pairs(h) do
ngx.say(k .. ":" .. v .. "<br>")
end
';
}
location /auth {
default_type text/html;
access_by_lua '
local redis = require "redis"
local red = redis:new()
local ok, err = red:connect("192.168.99.100", 6379)
if not ok then
ngx.log(ngx.ERR, "failed to connect to redis: ", err)
return ngx.exit(500)
end
local client_ip, err = red:exists(ngx.var.remote_addr)
ngx.log(ngx.INFO, "client IP:", client_ip)
if client_ip == 0 then
ngx.log(ngx.ERR, "unauthorized IP: ", err)
return ngx.exit(401)
end
';
content_by_lua '
ngx.say(ngx.var.remote_addr)
';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment