-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx.conf
More file actions
91 lines (77 loc) · 2.72 KB
/
nginx.conf
File metadata and controls
91 lines (77 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
events {
worker_connections 1024;
}
http {
resolver 1.1.1.1 ipv6=off;
lua_package_path "/usr/local/openresty/lualib/?.lua;/usr/local/openresty/site/?.lua;/opt/app/src/deps/lua-htmlparser/src/?.lua;/opt/app/src/?.lua;;";
init_by_lua_block {
settings = require("settings")
apod = require("apod")
cjson = require("cjson")
}
error_log /var/logs/openresty/error.log error;
access_log /var/logs/openresty/access.log;
server_tokens off;
server {
listen 8000;
client_max_body_size 20k;
client_body_buffer_size 20k;
error_page 404 /err_404;
location /api/v1/ {
access_log off;
alias /var/app/www/;
try_files $uri =404;
header_filter_by_lua_block {
ngx.header["X-Credits"] = settings.APOD_PAGE_URL
}
}
location = /api/v1/apod.json {
# access_log off;
default_type application/json;
content_by_lua_block {
local info = nil
local res = ngx.location.capture("/apod.json")
if res.status == 200 then
info = cjson.decode(res.body)
local year, month, day, hr, min, sec = info.date:match("^(%d+)%-(%d+)%-(%d+)%-(%d+)%-(%d+)%-(%d+)$")
local time = os.time({
year = tonumber(year),
month = tonumber(month),
day = tonumber(day),
hour = tonumber(hr),
min = tonumber(min),
sec = tonumber(sec),
})
if os.time(os.date("!*t")) - time > settings.REFRESH_AFTER_S then
info = apod.extract()
if not info then
ngx.status = 500
ngx.say('{"error":"Internal server error."}')
ngx.exit(500)
end
end
else
info = apod.extract()
if not info then
ngx.status = 500
ngx.say('{"error":"Internal server error."}')
ngx.exit(500)
end
end
info.date = os.date("%Y-%m-%d")
ngx.say(cjson.encode(info))
}
}
location = /apod.json {
internal;
access_log off;
try_files $uri =404;
root /var/app/www/;
}
location = /err_404 {
internal;
default_type text/plain;
return 404 "Not Found.";
}
}
}