Nginx Practice in Windows - 1

Description

最近在練習 Nginx, 不過電腦的環境只有 Windows 也沒打算裝 VM,
所以跟網路上文章的環境不太一樣, 在這紀錄怎麼在Windows上練習.
不過最終應該還是要裝個 Linux 環境來練習比較好..

Reference

Preparation

  1. Download nginx for Windows
    1. 下載後解壓縮
    2. 在 console mode 到解壓縮後的資料夾
    3. 開 nginx => start nginx
    4. 關 nginx => nginx -s stop
    5. 看 nginx process => tasklist /fi "imagename eq nginx.exe"
    6. reload config => nginx -s reload
  2. Download Simple REST Client for Chrome

Practice

用 set 定義變數

  1. 設定 nginx.conf, 加上 /test 這個 location, 用 set $foo 設定變數 response
    1. 如果要回傳的字串可以直接使用變數, 直接指定 $foo
          
          server {
              listen       8080;
              server_name  localhost;
              location / {
                  root   html;
                  index  index.html index.htm;
              }
              location /test {
                  set $foo "hello world";
                  return 200 "$foo";
              }
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
          } 
      
    2. 如果要回傳的字串需要在變數外再加東西,使 nginx 無法判斷變數, 就必須變成 ${foo}.
      (注意不是每次都用 ${foo}, 而是 nginx 會無法判斷的時候才要 ${foo})
          server {
              listen       8080;
              server_name  localhost;
              location / {
                  root   html;
                  index  index.html index.htm;
              }
              location /test {
                  set $foo "hello";
                  return 200 "${foo} world";
              }
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
          } 
      
  2. run "nginx -s reload"
  3. 用 Simple REST Client 送出 GET 到 http://127.0.0.1:8080/test
    GET /test HTTP/1.1
    Host: 127.0.0.1:8080
    Connection: keep-alive
    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
    
  4. 得到 response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sat, 19 Oct 2013 11:12:59 GMT
    Content-Type: application/octet-stream
    Content-Length: 11
    Connection: keep-alive
    
    hello world

沒有留言:

張貼留言

別名演算法 Alias Method

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