0%

使用ngrok突破防火牆或NAT限制

ngrok架構圖

自建ngrok服務器

### 編譯 ngrokd (Server) , ngrok (Client) - 下載 ngrok 原始碼
1
2
cd /usr/src/
git clone https://github.com/inconshreveable/ngrok
- 使用openssl 建立 SSL 憑證(自簽)
1
2
3
4
5
openssl genrsa -out rootCA.key 2048
openssl req -new -x509 -nodes -key rootCA.key -days 10000 -subj "/CN=ngrok.aligogo.pw" -out rootCA.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=ngrok.aligogo.pw" -out server.csr
openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -days 10000 -out server.crt
- 分別複製 憑證及key到 此目錄下的server及client
1
2
3
cp -p rootCA.pem   assets/client/tls/ngrokroot.crt
cp -p server.key assets/server/tls/snakeoil.key
cp -p server.crt assets/server/tls/snakeoil.crt
  • 編譯。 編譯完在 /usr/src/ngrok/bin/
    1
    2
    make release-server
    make release-client

    注意: 編譯 ARM 客戶端 (手機用)
    GOOS=linux GOARCH=arm make release-client
    編譯 Mac 客戶端 (Macbook用)
    GOOS=darwin GOARCH=amd64 make release-client
    編譯 windows 客戶端 (電腦用)
    GOOS=windows GOARCH=amd64 make release-client
    或是
    GOOS=windows GOARCH=386 make release-client

在Server端BB 啟動(Centos6_x86_64)

### ngrokd - 啟動ngrokd。指定 httpAddr、httpsAddr 分別是 ngrok 用來轉發 http、https 服務的端口
1
./ngrokd -domain="ngrok.aligogo.pw" -httpAddr=":8081" -httpsAddr=":8082"
  • 另外,ngrokd 預設還會開一個 4443 port 用來跟客戶端通訊 (可以使用 此參數指定 -tunnelAddr=”:xxx”),
    將 tcp 8081 ,8082 ,4443, 22222 加入防火牆
1
2
3
4
iptables -I INPUT 5 -p tcp --dport 4443 -j ACCEPT
iptables -I INPUT 5 -p tcp --dport 8081 -j ACCEPT
iptables -I INPUT 5 -p tcp --dport 8082 -j ACCEPT
iptables -I INPUT 5 -p tcp --dport 22222 -j ACCEPT

在Client端 AA (Centos6_x86_64) 啟動ngrok去連接 ngrok.aligogo.pw:4443

- 目標: 讓外部能連接內部 sshd - 在 Client AA 先確定是否有開啟 sshd
  • 與 ngrok 同目錄建立 ngrok.cfg
    (Server 防火牆要開 port (ex. 58800 (供RDP轉送)、22222 (供SSH轉送)))
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    server_addr: "ngrok.aligogo.pw:4443"
    trust_host_root_certs: false
    tunnels:
    rdp:
    remote_port: 58800
    proto:
    tcp: 3389
    ssh:
    remote_port: 22222
    proto:
    tcp: 22
  • 啟動 ngrok
1
./ngrok  -config=./ngrok.cfg start ssh
  • 換顯示以下訊息
1
2
3
4
5
6
7
8
ngrok                                                                                            (Ctrl+C to quit)

Tunnel Status online
Version 1.7/1.7
Forwarding tcp://ngrok.aligogo.pw:22222 -> 127.0.0.1:22
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
  • 遠端測試機 是否可以連接 Client 端 AA
1
ssh -p 22222 ngrok.aligogo.pw

在Client端CC (Windows 2003) 啟動ngrok 去連接 ngrok.aligogo.pw:4443

- 目標: 讓外部能連接內部 RDP 3389 - 因為是 Windows ngrok 需要重新編譯
1
GOOS=windows GOARCH=386  make release-client  (for 32位元電腦)
1
GOOS=windows GOARCH=amd64 make release-client (for 64位元電腦)
  • 與 ngrok 同目錄建立 ngrok.cfg
    (Server 防火牆要開 22222 (供RDP轉送))

    1
    2
    3
    4
    5
    6
    7
    server_addr: "ngrok.aligogo.pw:4443"
    trust_host_root_certs: false
    tunnels:
    rdp:
    remote_port: 22222
    proto:
    tcp: 3389
  • 啟動 ngrok

1
ngrok.exe -config=ngrok.cfg  start rdp

參考資料

單純做cache-Only DNS

LAB架構圖

cache-only_Architecture

DNS設定檔

  • 主設定檔: /etc/named.conf (主要是紅框的部分)
    cache-only

DNS啟動

- /etc/init.d/named start

DNS測試

- 正解測試: - dig -t a www.google.com @127.0.0.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.4 <<>> -t a www.google.com @127.0.0.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25098
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.google.com. IN A

;; ANSWER SECTION:
www.google.com. 82 IN A 216.58.200.228

;; Query time: 82 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; MSG SIZE rcvd: 48

參考資料

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)

Nginx location 匹配原則

語法格式

1
2
3
4
5
6
7
8
9
http{
...
server{
location [ = | ~ | ~* | ^~ | @ ] /uri/ {
......
}
}
...
}

location 優先順序比對原則 (重要!!!)

  • “=” > “^“ > ““ > “/uri/“ > “/“ (location 的 前綴)
  • 其中 “=” , “^~” , “@” 和 /uri/ (無任何前綴) 都是屬於 一般 location(也就是文字字串)
  • 其中 ~ , ~* 是屬於 正規表示法的 location
  • location 會先匹配文字字串 ,再匹配正規表示法
  • 文字字串 以匹配最長字串為原則, 文字字串沒有位置的先後順序。
  • 正規表示法 有先後順序

“=”

  • 嚴格精確比對
  • 匹配之後, 即終止後面的location 比對
  • 文字字串比對

“^~”

  • 文字字串比對
  • 匹配之後, 即終止後面的location 比對,其實還有另一種含意 (^ 表示「非」,~ 表示「正規表示法」,字符意思是:不要繼續匹配正規表示法)
  • 區分大小寫
  • 不會做URL encode

“~”

  • 正規表示法比對
  • 區分大小寫

“~*”

  • 正規表示法比對
  • 不區分大小寫

location LAB 實踐演練說明

“^“ ““ 優先順序

  • Nginx 設定檔
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    server {
    listen 80;
    server_name localhost 172.17.0.*;
    root /var/www/html;
    access_log /var/log/nginx/localhost-access.log main;
    error_log /var/log/nginx/localhost-error.log;

    location ~ /test/ {
    echo "A";
    }
    location ^~ /test {
    echo "B";
    }
    }
  • 測試及說明
    1
    2
    3
    curl 172.17.0.2/test/    ==> 會return  B
    按照location 匹配原則;先執行文字字串的比對,再比對正規表示法。
    這邊找到 "^~" 比對文字字串,因 "^~" 特性比對到,就終止後面 正規表示法的比對。

“=” “^“ ““ 優先順序

  • Nginx 設定檔
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    server{
    ...
    location ~ /test/ {
    echo "A";
    }
    location ^~ /test {
    echo "B";
    }
    location =/test{
    echo "C"
    }

  • 測試和說明
    1
    2
    curl 172.17.0.2/test/  ==> return C
    按照location 匹配原則;這邊找到 "=" 比對文字字串,因 "=" 精確比對到,就終止後面 正規表示法的比對。

    “~” 正規表示法 有位置執行順序

  • Nginx 設定檔
    1
    2
    3
    4
    5
    6
    7
    8
    9
    server {
    ....
    location ~ ^/test/.*\.(html|htm|jpg|gif)$ {
    echo "A";
    }
    location ~ ^/test/.*\.(html|htm|jpg|gif|png)$ {
    echo "B";
    }
    }
  • 測試及說明
    1
    2
    3
    4
    5
    6
    7
    8
    curl 172.17.0.2/test/123.html   ==> A
    這邊找不到文字字串比對,再以正規表示法比對, 第一筆location 比對到就終止後面正規表示法的執行。

    curl 172.17.0.2/test/123.jpg ==> A
    這邊找不到文字字串比對,再以正規表示法比對, 第一筆location 比對到就終止後面正規表示法的執行。

    curl 172.17.0.2/test/123.png ==> B
    這邊找不到文字字串比對,再以正規表示法比對, 第一筆location 沒有比對到,再繼續 第二筆正規表示法的比對,比對到就終止後面正規表示法的執行。

    /uri/ ~ 優先順序

  • nginx 設定檔
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    server {
    ....
    location /test/123.png {
    echo "C";
    }

    location ~ ^/test/.*\.(html|htm|jpg|gif)$ {
    echo "Test A";
    }

    location ~ \.(html|png)$ {
    echo "B";
    }

    location ~ ^/test/.*\.(html|htm|jpg|gif|png)$ {
    echo "Test B";
    }
    }
  • 測試及說明
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    curl http://172.17.0.2/test/123.png  ===>  B
    以文字字串比對 匹配到 第一筆location ,再以 正規表示法比對 匹配第三筆 location ,就終止後面的比對。

    若將第三筆 location mark 起來 "#location ~ \.(html|png)$" 測試看看; curl http://172.17.0.2/test/123.png ==> Test B
    以文字字串比對 匹配到 第一筆location ,再以 正規表示法比對 匹配第四筆location,就終止後面的比對。

    將剛才mark 第三筆 location拿掉。測試
    curl http://172.17.0.2/123.html ==> B
    以文字字串比對 沒匹配到,再以 正規表示法比對匹配第三筆 location ,就終止後面的比對。

    curl http://172.17.0.2/test/123.html ==> Test A
    以文字字串比對,沒匹配到,再以正規表示法比對,匹配第二筆 location,就終止後面的比對。

    參考資料:

  • 官網 NGINX- location
  • 51Cto - nginx location 規則優先級
  • Nginx 關於 location 的匹配規則詳解
  • Nginx Location REGEX 比對語法和設定
  • nginx配置location總結及rewrite規則寫法

Nginx Proxy Cache

  • 應用於Reverse Proxy,負載均衡
  • 作用是緩存後端服務器的內容,可能是任何內容,包括靜態的和動態
  • 另外,也可作正向代理 (Nginx 這邊支援 web proxy)

Nginx_Proxy_Cache

Nginx Fastcgi Cache

  • proxy_cache 與 fastcgi_cache功能大至一樣。
  • 用是緩存fastcgi生成的內容,很多情況是php生成的動態的內容。
  • 緩存作用。減少了nginx與Application(php,python,cgi等等)通信的次數。

Nginx_Fastcgi_Cache

Nginx Cache 運作流程

Nginx 除了 work Process 管理 Http Request 請求等其他工作外,Nginx Cache 也由另外程序來作管理。

  1. Nginx Cache loader
  • 主要Nginx 一開始初始化時,有呼叫Nginx Cache 相關模組,Nginx master process 會 fork Nginx Cache loader 去抓取 Nginx Cache Store(cache 儲存區),來重新建立索引(hash key)並存放在memory 中,
         建立完成後, Cache loader process 就隨即結束。
    
  1. Nginx Cache mamager
  • 主要接受 master process 指令 去管理 Nginx Cache 大小事務,像是 Hash key 過期,或是搜尋 此筆資料是否存放在 cache 儲存區中 等工作。

Ngnx Cache運作流程

Ngnx Cache Manager

Nginx Fastcgi Cache 簡要參數說明

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
http{
....
fastcgi_cache_path /dev/shm/fastcgicache levels=1:2 keys_zone=democache:10m max_size=1000m inactive=60s;
server {
...
location ~ ^/other_sport/A0017_0050/(.*) {
access_log /var/log/nginx/localhost-demo_cache-access.log main;
error_log /var/log/nginx/localhost-demo_cache-error.log;
try_files $uri $uri/ =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fastcgi_params;
set $prefix_fastcgi_name $1;
if ($query_string != ""){ set $prefix_fastcgi_name $1?$query_string;}

include /etc/nginx/conf.d/skip_cache_new;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache democache;
fastcgi_cache_valid 1m;

}
....

location /123.html {
internal;
set $memc_cmd $arg_cmd;
set $memc_key $arg_key;
memc_pass 127.0.0.1:11211;

}


}

....
}

fastcgi_cache_path

  • /dev/shm/fastcgicache 儲存cache的地方

  • levels=1:2 設定目錄階層

1
2
3
4
5
6
7
8
/dev/shm/fastcgicache
|-- 1
| `-- a7
| `-- a5225f331f10bbe6099bfe0e48e82a71
|-- 6
| `-- 8f
`-- a
`-- fe
  • keys_zone=democache:10m **為cache 空間設定名字為 democache , 此空間10MB **

  • 10MB 是儲存在 share memory,,比較常存取(hot data)會被儲存在share memory

  • max_size=1000m 最大使用1G空間 ,是指定儲存cache的容量

  • inactive=60s cache 1分鐘,預設值10分鐘

fastcgi_cache_key

  • 將 “$scheme$request_method$host$request_uri” 用MD5方法來 當作 hash key

    fastcgi_cache

  • 指定 key_zone 名字為 democache

  • 參數: inactive ,指在一段時間內文件沒被存取使用,就會被緩存管理器自動刪除。 ,預設 10m(10分鐘)

fastcgi_cache_valid

指定接收到回應程式 的 http code 快取時間

  • fastcgi_cache_valid [http code …] time;

  • 若只有指定時間。預設只有 http code :200, 301, and 302 等回應狀態會被快取
    ex: 當Nginx 收到回應程式 http 狀態碼 200 時,就快取 1 小時

    fastcgi_cache_use_stale

  • 定義那些情況使用過期cache

  • invalid_header 無效的回應

  • error 回應的Header 發生錯誤

  • http_500 回應500錯誤

  • time 當Nginx 與 Application 建立連接時,Nginx 等待 Application 回應超時

fastcgi_ignore_headers

  • 忽略 Http header 帶有 Cache-Control , Expires ,Set-Cookie

  • 若從後端 Application回應 http header 帶有Cache-Control , Expires ,Set-Cookie 會影響fastcgi_cache 不會cache 回應資料

fastcgi_no_cache

  • 不等於空值 及 值不等於 0 都不被 cache。 只要是 0 或 空值 就指定 cache

    fastcgi_cache_bypass

  • 不等於空值 及 值不等於 0 ,即是 不從cache區 回應資料給Client端,而是 By_Pass 到後端 。 只要是 0 或 空值 就指定 cache

$upstream_cache_status 包含以下幾種狀態

狀態 說明
MISS 未命中,請求被傳送到後端
HIT 緩存命中
EXPIRED 緩存已經過期請求被傳送到後端
UPDATING 正在更新緩存,將使用舊的應答
STALE 後端將得到過期的應答
BYPASS request會By_Pass到後端伺服器上,因為請求匹配了一個proxy_cache_bypass 或是 fastcgi_cache_bypass

執行Nginx + lua 之前 需要了解的一些事

載入 lua 模組, 測試是否可以運作

1-1.

1
2
3
4
5
6
7
dso{
path /etc/nginx/modules/;
load ngx_http_lua_module.so;
}
http{
.....
}
1
2
nginx -t
/etc/init.d/nginx reload

驗證lua 模組是否有載入

1
nginx -m
1
2
3
4
...
...
ngx_http_lua_module (shared, 3.1)
...

1-2. 測試 lua 模組是否可用

1
2
3
4
5
6
7
8
9
10
11
12
13
http{
....
server{
....
location /lua3{
default_type text/html;
set $RRR I come from Taiwan;
content_by_lua 'ngx.say("<h1><a href=http://tw.yahoo.com>",ngx.var.RRR,"</a></h1>")';
}
.....
}
..
}

Nginx hander處理模組將 http request 劃分11個階段來處理,及了解每個階段執行順序。

2-1.

  • 先由Nginx核心 讀取 http Header

  • 根據Nginx設定檔,Nginx 核心 挑選一個Hander模組 處理 Http 請求 ,並將 http Header 交給 Hander 模組

  • 由Hander 模組 讀取 Http Body ,並處理好 Http 請求

  • Hander 模組處理Http 請求後,再 發送給 Fliters 模組處理。

  • Fliter 模組處理好後,再發送給客戶端
    Http請求處理流程圖

2-2. Handers模組 精細處理 Http 請求 ,分成 11 個階段處理。下表所列處理階段分別依序處理

Handers階段 說明
NGX_HTTP_POST_READ_PHAS 讀取請求內容階段
NGX_HTTP_SERVER_REWRITE_PHASE Server請求位址重寫階段
NGX_HTTP_FIND_CONFIG_PHASE 配置查找階段:
NGX_HTTP_REWRITE_PHASE Location請求位址重寫階段
NGX_HTTP_POST_REWRITE_PHASE 請求位址重寫提交階段
NGX_HTTP_PREACCESS_PHASE 訪問許可權檢查準備階段
NGX_HTTP_ACCESS_PHASE 訪問許可權檢查階段
NGX_HTTP_POST_ACCESS_PHASE 訪問許可權檢查提交階段
NGX_HTTP_TRY_FILES_PHASE 配置項try_files處理階段
NGX_HTTP_CONTENT_PHASE 內容產生階段
NGX_HTTP_LOG_PHASE 日誌模組處理階段

ngx_http_memc_module.so 模組

使Nginx 能利用 memcached 相關指令 (set ,get ,stat..等等)存取memcached server

Install

1
2
cd /use/local/src;git clone https://github.com/openresty/memc-nginx-module.git
dso_tool --dst=/etc/nginx/modules/ --add-module=/usr/local/src/memc-nginx-module/

修改設定檔 ,載入此模組 (/etc/nginx/modules/ngx_http_memc_module.so )

1
2
3
4
5
6
7
dso {
....
load ngx_http_memc_module.so;
}
http{
....
}
1
nginx -t && /etc/init.d/nginx reload

Nginx access memcached

  • 先啟動 memcached ,並插入一些資料 ,這邊測試,插入 andy 和 amy
1
2
3
4
echo "Google" > andy
echo "Good" > amy
memcp --verbose --debug --servers=127.0.0.1 --expire=120 andy
memcp --verbose --debug --servers=127.0.0.1 --expire=120 amy
  • 測試抓取 andy , amy key 的值
1
2
curl 'http://192.168.0.3/memt.html?cmd=get&key=andy'
curl 'http://192.168.0.3/memt.html?cmd=get&key=amy'
  • 為了能安全取得 memcached key 值

    可以不要讓外部直接利用Nginx 存取 memcached Server,需要修改 nginx 設定檔

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    http{
    ...
    server {
    location /memt.html {
    internal;
    set $memc_cmd $arg_cmd;
    set $memc_key $arg_key;
    memc_pass 127.0.0.1:11211;
    }
    }
    }
  • 測試

    目標結果 : 404 沒找到

1
curl 'http://192.168.0.3/memt.html?cmd=get&key=andy'

自行撰寫lua流程說明

  • 先判斷是否有 帶 cookie ; 若沒帶 則 By_Pass 後端php-cgi處理

  • 再確定 cookie 是否有帶 “WEBSESSID=” 字串 ; 若沒帶 則 By_Pass 後端php-cgi處理

  • 有帶cookie , 利用(memcached API For Nginx) 讀取 cookie值

  • 確定cookie 值的內容 是否有帶登入資訊 ;若沒帶 則By_Pass 後端php-cgi處理

  • 有帶登入資訊, 是否為列表要cache 的對象 ; 若不是 則By_Pass 後端php-cgi處理

簡單演示 及 問題說明

演示

問題說明

  • 說明背景:
1
2
3
4
http://220.229.227.26/other_sport/A0017_0050/om/ 
http://220.229.227.26/other_sport/A0017_0050/wlb/
兩個位址都在同一個DocumentRoot /var/www/html/other_sport/A0017_0050/
兩個位址都在同一個Nginx設定檔中 /etc/nginx/conf.d/00-default.conf
  • 產生問題執行:
1
2
3
4
用Firefox 瀏覽此url 並登入 http://220.229.227.26/other_sport/A0017_0050/om/
執行設定會被cache的url http://220.229.227.26/other_sport/A0017_0050/om/league-listing.php (看 /var/log/nginx/localhost-demo_cache-access.log 是否有hit到,有被Hit到 就馬上執行下一個步驟)
用IE 瀏覽此url 並登入 http://220.229.227.26/other_sport/A0017_0050/wlb/
再瀏覽此url http://220.229.227.26/other_sport/A0017_0050/om/league-listing.php,就發生未登入即可以抓取的到頁面。

參考資料