Description
最近在練習 Nginx, 不過電腦的環境只有 Windows 也沒打算裝 VM,
所以跟網路上文章的環境不太一樣, 在這紀錄怎麼在Windows上練習.
所以跟網路上文章的環境不太一樣, 在這紀錄怎麼在Windows上練習.
不過最終應該還是要裝個 Linux 環境來練習比較好..
Reference
Preparation
- Download nginx for Windows
- 下載後解壓縮
- 在 console mode 到解壓縮後的資料夾
- 開 nginx => start nginx
- 關 nginx => nginx -s stop
- 看 nginx process => tasklist /fi "imagename eq nginx.exe"
- reload config => nginx -s reload
- Download Simple REST Client for Chrome
Practice
用 set 定義變數
- 設定 nginx.conf, 加上 /test 這個 location, 用 set $foo 設定變數 response
- 如果要回傳的字串可以直接使用變數, 直接指定 $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; } }
- 如果要回傳的字串需要在變數外再加東西,使 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; } }
- run "nginx -s reload"
- 用 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
- 得到 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
沒有留言:
張貼留言