Nginx Practice in Windows - 5 static content

Description

使用 nginx 主要目的之一就是要存放靜態檔如 html 或圖片

Reference

Practice

設定讓 "GET /" 的 request 首頁導向 testhtml/data/www/index.html

  1. prepare folders
    D:\nginx-1.4.3\nginx.exe
    D:\nginx-1.4.3\conf\nginx.conf
    D:\nginx-1.4.3\testhtml\data\www\index.html
    D:\nginx-1.4.3\testhtml\data\images
    
  2. edit index.html
    Hello!, This is testhtml\data\www\index.html
  3. setup nginx.conf
        server {
            listen       8080;
            server_name  127.0.0.1;
            location / {
                root   testhtml/data/www;
                index  index.html;
            }
        } 
    
  4. run "nginx -s reload"
  5. run "telnet 127.0.0.1 8080"
  6. send request
    GET / HTTP/1.1
    Host: server1: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
    
  7. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sun, 27 Oct 2013 23:22:23 GMT
    Content-Type: text/html
    Content-Length: 44
    Last-Modified: Sun, 27 Oct 2013 23:22:19 GMT
    Connection: close
    ETag: "526da02b-2c"
    Accept-Ranges: bytes
    
    Hello!, This is testhtml\data\www\index.html
    

設定去取 "/" 以外的檔案

  1. prepare files
    D:\nginx-1.4.3\testhtml\data\www\www.html
    D:\nginx-1.4.3\testhtml\data\data.html
    D:\nginx-1.4.3\conf\nginx.conf
    
  2. edit data.html
    Hello!, This is data.html 
  3. edit www.html
    Hello!, This is www.html
    
  4. setup nginx.conf
        server {
            listen       8080;
            server_name  127.0.0.1;
            location / {
                root   testhtml/data/www;
                index  www.html;
            }
            location /data/ {
                root   testhtml;
                index  data.html;
            }
        } 
    
  5. run "nginx -s reload"
  6. run "telnet 127.0.0.1 8080"
  7. send request
    GET / HTTP/1.1
    Host: server1: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
    
  8. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 28 Oct 2013 15:33:05 GMT
    Content-Type: text/html
    Content-Length: 24
    Last-Modified: Sun, 27 Oct 2013 23:32:37 GMT
    Connection: close
    ETag: "526da295-18"
    Accept-Ranges: bytes
    
    Hello!, This is www.html
  9. send request (這個 request 是去要 /data/ 的 resource, 其實也可以對上 "/" 的 location, 不過 nginx 會選擇 prefix 對應最長的 location)
    GET /data/ HTTP/1.1
    Host: server1: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
    
  10. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 28 Oct 2013 15:49:13 GMT
    Content-Type: text/html
    Content-Length: 25
    Last-Modified: Sun, 27 Oct 2013 23:32:15 GMT
    Connection: close
    ETag: "526da27f-19"
    Accept-Ranges: bytes
    
    Hello!, This is data.html
    
  11. send request (這個 request 是去找 "/" 的 index.html)
    GET /data/www/www.html HTTP/1.1
    Host: server1: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
    
  12. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 28 Oct 2013 15:50:23 GMT
    Content-Type: text/html
    Content-Length: 24
    Last-Modified: Sun, 27 Oct 2013 23:32:37 GMT
    Connection: close
    ETag: "526da295-18"
    Accept-Ranges: bytes
    
    Hello!, This is www.html

如果多個 location 使用同樣的 root, 只要在 server 區塊中設定 root 即可

  1. prepare files
    D:\nginx-1.4.3\testhtml\testhtml.html
    D:\nginx-1.4.3\testhtml\data\data.html
    D:\nginx-1.4.3\conf\nginx.conf
    
  2. edit data.html
    Hello!, This is data.html 
  3. edit testhtml.html
    Hello!, This is testhtml.html
    
  4. setup nginx.conf
        server {
            listen       8080;
            server_name  127.0.0.1;
            root   testhtml;
            location / {
                index  testhtml.html;
            }
            location /data/ {
                index  data.html;
            }
        } 
    
  5. run "nginx -s reload"
  6. run "telnet 127.0.0.1 8080"
  7. send request
    GET / HTTP/1.1
    Host: server1: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
    
  8. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 28 Oct 2013 23:51:46 GMT
    Content-Type: text/html
    Content-Length: 29
    Last-Modified: Mon, 28 Oct 2013 23:45:17 GMT
    Connection: close
    ETag: "526ef70d-1d"
    Accept-Ranges: bytes
    
    Hello!, This is testhtml.html
  9. send request
    GET /data/ HTTP/1.1
    Host: server1: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
    
  10. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 28 Oct 2013 23:52:29 GMT
    Content-Type: text/html
    Content-Length: 25
    Last-Modified: Sun, 27 Oct 2013 23:32:15 GMT
    Connection: close
    ETag: "526da27f-19"
    Accept-Ranges: bytes
    
    Hello!, This is data.html

沒有留言:

張貼留言

別名演算法 Alias Method

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