Nginx Practice in Windows - 2

Reference

Practice

變數的有效範圍是 request, 所以就算不同 request 也可以使用變數

  1. config nginx.conf
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            location /test {
                set $foo "hello";
                rewrite ^ /test2;
            }
            location /test2 {
                return 200 "${foo} world";
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        } 
    
  2. run "nginx -s reload"
  3. request "http://127.0.0.1:8080/test"
  4. get response: hello world

使用 Nginx 內建變數

  1. config nginx.conf
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            location /test {
                return 200 "Your address:${remote_addr}:${remote_port}. nginx version:${nginx_version}. nginx pid:${pid}";
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        } 
    
  2. run "nginx -s reload"
  3. request "http://127.0.0.1:8080/test"
  4. get response: Your address:127.0.0.1:53102. nginx version:1.4.3. nginx pid:8132
  5. run tasklist /fi "imagename eq nginx.exe"
    C:\nginx-1.4.3>tasklist /fi "imagename eq nginx.exe"
    
    映像名稱                       PID 工作階段名稱      工作階段 #    RAM使用量
    ========================= ======== ================ =========== ============
    nginx.exe                     4044 Console                    1      6,044 K
    nginx.exe                     8132 Console                    1      6,400 K
    

取得 request parameter value: ${arg_XXX}

  1. Note: nginx 取 parameter 不分大小寫
  2. config nginx.conf
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            location /test {
         return 200 "The parameter abc value: ${arg_abc}";
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        } 
    
  3. run "nginx -s reload"
  4. request "http://127.0.0.1:8080/test?abc=qq"
  5. get response: The parameter abc value: qq
  6. request "http://127.0.0.1:8080/test?Abc=qq"
  7. get response: The parameter abc value: qq 

取得 header value: ${http_XXX}

  1. config nginx.conf
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            location /test {
                return 200 "The header 'abc' value: ${http_abc}";
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: 127.0.0.1:8080
    Connection: close
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
    Accept: */*
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
    ABC: 123
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sat, 19 Oct 2013 14:03:40 GMT
    Content-Type: application/octet-stream
    Content-Length: 27
    Connection: close
    
    The header 'abc' value: 123
    

取得 cookie value: ${cookie_XXX}

  1. config nginx.conf
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            location /test {
                return 200 "The cookie 'abc' value: ${cookie_abc}";
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: 127.0.0.1:8080
    Connection: close
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
    Accept: */*
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
    Cookie: ABC=123
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sat, 19 Oct 2013 14:09:30 GMT
    Content-Type: application/octet-stream
    Content-Length: 27
    Connection: close
    
    The cookie 'abc' value: 123
    

沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...