Showing
3 changed files
with
127 additions
and
1 deletions
1 | +# copy sec.config.json to remote openresty server and reload nginx | ||
1 | - hosts: sec | 2 | - hosts: sec |
2 | tasks: | 3 | tasks: |
3 | - - name: copy file to remote host | 4 | + - name: copy lua and config files |
5 | + template: | ||
6 | + src: {{ item }} | ||
7 | + dest: /usr/local/openresty/nginx/conf/ | ||
8 | + with_items: | ||
9 | + - 'sec/nginx.conf' | ||
10 | + - 'sec/sec.lua' | ||
11 | + | ||
12 | + - name: copy sec.config.json to remote host | ||
4 | copy: | 13 | copy: |
5 | src: /root/.cert/sec.config.json | 14 | src: /root/.cert/sec.config.json |
6 | dest: /usr/local/openresty/nginx/conf/sec.config.json | 15 | dest: /usr/local/openresty/nginx/conf/sec.config.json |
playbooks/templates/sec/nginx.conf
0 → 100644
1 | +#user nobody; | ||
2 | +worker_processes 1; | ||
3 | + | ||
4 | +error_log /var/log/nginx/error.log; | ||
5 | + | ||
6 | + | ||
7 | +events { | ||
8 | + worker_connections 1024; | ||
9 | +} | ||
10 | + | ||
11 | + | ||
12 | +http { | ||
13 | + include mime.types; | ||
14 | + default_type application/octet-stream; | ||
15 | + | ||
16 | + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | ||
17 | + '$status $body_bytes_sent "$http_referer" ' | ||
18 | + '"$http_user_agent" "$http_x_forwarded_for"'; | ||
19 | + | ||
20 | + access_log /var/log/nginx/access.log main; | ||
21 | + | ||
22 | + sendfile on; | ||
23 | + #tcp_nopush on; | ||
24 | + | ||
25 | + #keepalive_timeout 0; | ||
26 | + keepalive_timeout 65; | ||
27 | + | ||
28 | + | ||
29 | + #lua | ||
30 | + lua_package_path "/usr/local/openresty/nginx/conf/?.lua;;"; | ||
31 | + init_worker_by_lua_block { | ||
32 | + sec = require("sec") | ||
33 | + sec:init() | ||
34 | + } | ||
35 | + | ||
36 | + #gzip on; | ||
37 | + | ||
38 | + server { | ||
39 | + listen 80; | ||
40 | + server_name security.config.yohoops.org; | ||
41 | + | ||
42 | + allow 10.66.70.0/24; | ||
43 | + allow 172.31.70.0/24; | ||
44 | + allow 10.66.0.118/32; | ||
45 | + deny all; | ||
46 | + #charset koi8-r; | ||
47 | + | ||
48 | + #access_log logs/host.access.log main; | ||
49 | + | ||
50 | + location /status { | ||
51 | + default_type text/html; | ||
52 | + return 200 'server is ok!'; | ||
53 | + } | ||
54 | + | ||
55 | + location /config { | ||
56 | + content_by_lua_block { | ||
57 | + sec = require("sec") | ||
58 | + sec:get_config() | ||
59 | + } | ||
60 | + } | ||
61 | + | ||
62 | + #error_page 404 /404.html; | ||
63 | + | ||
64 | + # redirect server error pages to the static page /50x.html | ||
65 | + # | ||
66 | + error_page 500 502 503 504 /50x.html; | ||
67 | + location = /50x.html { | ||
68 | + root html; | ||
69 | + } | ||
70 | + | ||
71 | + } | ||
72 | + | ||
73 | + | ||
74 | + | ||
75 | +} |
playbooks/templates/sec/sec.lua
0 → 100644
1 | +local modname= ... | ||
2 | +local M={} | ||
3 | +_G[modname]=M | ||
4 | +package.loaded[modname]=M | ||
5 | + | ||
6 | +local cjson=require "cjson" | ||
7 | + | ||
8 | +local config = {} | ||
9 | + | ||
10 | +-- load config files from local | ||
11 | +local function load_config() | ||
12 | + local myTable = {} | ||
13 | + local file = io.open( "/usr/local/openresty/nginx/conf/sec.config.json", "r" ) | ||
14 | + if file then | ||
15 | + --print("trying to read ", filename) | ||
16 | + -- read all contents of file into a string | ||
17 | + local contents = file:read( "*a" ) | ||
18 | + myTable = cjson.decode(contents); | ||
19 | + io.close( file ) | ||
20 | + --print("Loaded file") | ||
21 | + return myTable | ||
22 | + else | ||
23 | + ngx.log(ngx.ERR, "file is not found. ") | ||
24 | + return nil | ||
25 | + end | ||
26 | +end | ||
27 | + | ||
28 | +--- called by init_worker_by_lua_file | ||
29 | +function M:init() | ||
30 | + config = load_config() | ||
31 | +end | ||
32 | + | ||
33 | + | ||
34 | + | ||
35 | +-- get all config -------- | ||
36 | +function M:get_config() | ||
37 | + ngx.header["Content-type"]="application/json;charset=utf-8" | ||
38 | + | ||
39 | + local body = cjson.encode(config) | ||
40 | + ngx.say(body) | ||
41 | + ngx.exit(ngx.HTTP_OK) | ||
42 | +end |
-
Please register or login to post a comment