0%

ACL For Nginx

ACL For Nginx - 目錄

  • 前言

  • 配置設定檔

  • 測試

  • 參考資料

    前言

    利用Nginx 建立一個簡單的存取控制列表。假設我要阻擋從SG(新加坡)及TW(台灣)來的IP訪問Nginx,另外開放指定的台灣IP訪問Nginx。以下是本篇要使用的材料:
  • Linux 平台 Centos6 x86_64

  • Tengine

    1
    Tengine 是由淘寶網改寫Nginx,而釋放出來的原始碼版本。設定即使用與官方Nginx一樣。 
  • ngx_http_geoip_module.so 模組

    1
    geoip此模組功用是利用Maxmind GeoIP2資料檔來識別來源IP是屬於哪個國家(ex: TW, US, JP,SG,RU,CN...等等)
  • ngx_http_lua_module.so 模組

    1
    Lua是一套輕巧,執行效率高的script語言。需多應用程式利用Lua作為自己的嵌入式手稿語言,加入此模組之後,Nginx就可在設定檔中撰寫Lua做些邏輯性的控制。 

    配置設定檔

    這邊不會將全部設定檔寫出來,只會寫與本篇相關設定及參數,並顯示設定檔的相對位置。
載入Lua 及 Geoip 模組
  • 將Lua及 Geoip 放置在此路徑下
    1
    2
    3
    /etc/nginx/modules/
    ├── ngx_http_geoip_module.so
    └── ngx_http_lua_module.so
  • 在此檔案 /etc/nginx/nginx.conf 修改 Nginx設定檔
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    dso { 
    path /etc/nginx/modules/;
    load ngx_http_lua_module.so;
    load ngx_http_geoip_module.so;
    }
    http{
    ...
    server {
    location / {
    .....
    }
    ....
    }
    ...
    }
  • 利用以下指令 reload Ninx 及 顯示模組是否有被載入
    1
    2
    3
    4
    5
    6
    7
    8
    [root@andy]#nginx -t  && /etc/init.d/nginx reload
    [root@andy]#nginx -m
    Tengine version: Tengine/2.1.0 (nginx/1.6.2)
    loaded modules:
    ...
    ngx_http_lua_module (shared, 3.1)
    ngx_http_geoip_module (shared, 3.1)
    ...
    在此檔案 /etc/nginx/nginx.conf 修改Geoip 及 開放IP等設定
    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
    http{
    #Maxmind GeoIP2資料檔
    geoip_country /usr/local/share/GeoIP/GeoIP.dat;

    # 將阻擋國家簡碼寫在此。 0表阻擋 ; 1 表示開放預設值
    map $geoip_country_code $chek_flag
    {
    default 1;
    SG 0;
    TW 0;
    }

    # 將要開放IP寫在此。 1 表示開放 ; 0為預設值不開放。
    geo $whitelist{
    default 0;
    116.241.114.0/24 1; #這是某個台灣IP
    }
    server {
    location / {
    .....
    }
    ....
    }
    ...
    }
    使用Lua 模組做流程控制
  • 要修改的設定檔
    1
    2
    3
    /etc/nginx/conf.d/
    ├── 1-showip.conf
    └── showip.lua
  • 修改此設定檔 /etc/nginx/conf.d/1-showip.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    http{
    ...
    server {
    listen 80;
    server_name ip2.aligogo.pw;
    access_log /var/log/nginx/showip-access.log main;
    error_log /var/log/nginx/showip-error.log;
    location / {
    default_type text/html;
    #lua 流程控制寫在此,showip.lua
    content_by_lua_file /etc/nginx/conf.d/showip.lua;
    }

    location /showcode {
    default_type text/html;
    content_by_lua 'ngx.say("<center><h1>",ngx.var.geoip_country_code," ",ngx.var.remote_addr,"</h1></center>")';
    }
    #error_page 500 502 503 504 /50x.html;
    location = /50x.html { root /usr/share/nginx/html; }
    }
    }
  • 修改此設定檔 /etc/nginx/conf.d/showip.lua
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    -- 指定要允許ip的判斷。
    if ngx.var.whitelist == "1" then
    -- 秀ip 及國碼
    ngx.exec('/showcode');
    end
    -- 阻擋國家的判斷。
    if ngx.var.chek_flag == "0" then
    ngx.exit(ngx.HTTP_FORBIDDEN)
    end
    -- 以上沒被判斷到的
    ngx.exec('/showcode');
  • reload Nginx
    1
    [root@andy]#nginx -t  && /etc/init.d/nginx reload

    測試

  • 測試阻擋SG新加坡IP
    test_SG
  • 測試開放指定TW IP
    test_TW

參考資料

- [Nginx Doc](http://nginx.org/en/docs/) - [tengine](http://tengine.taobao.org/) - [Nginx使用GeoIP2的拓展模塊處理不同國家的訪問](https://idoseek.com/1774) - [GeoIP2 Downloadable Databases](http://dev.maxmind.com/geoip/geoip2/downloadable/) - [openresty_lua-nginx-module](https://github.com/openresty/lua-nginx-module/blob/master/README.markdown) - [Lua-wiki](https://zh.wikipedia.org/wiki/Lua)

歡迎關注我的其它發布渠道