Nginx Practice in Windows - 6 simple proxy server

Description

使用 nginx 的一個主因是要支援 web application 放在別台機器上, 將相關的 request 導過去.

Reference

Practice

  1. prepare files
    D:\nginx-1.4.3\nginx.exe
    D:\nginx-1.4.3\conf\nginx.conf
    D:\nginx-1.4.3\testhtml\testhtml.html
    D:\nginx-1.4.3\internal\internal.html
    D:\nginx-1.4.3\internal\service\service.html
    D:\nginx-1.4.3\internal\service\serviceA\serviceA.html
    
  2. setup nginx.conf
    (注意 proxy_pass 的 127.0.0.1 要對應到 port 12345 的 server_name 127.0.0.1, 在練習的時候發現如果設定成 proxy_pass http://localhost:12345 那 nginx 就很容易遇到錯誤: 68 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /service/serviceA/serviceA7.html HTTP/1.1)
        server {
            listen       8080;
            server_name  127.0.0.1;
            root         testhtml;
            location / {
                index testhtml.html;
            }
            location /service/ {
                proxy_pass http://127.0.0.1:12345;
            }
        } 
        server {
            listen       12345;
            server_name  127.0.0.1;
            root         internal;
            location / {
                index  internal.html;
            }        
            location /service/ {
                index  service.html;
            }
        } 
    
  3. run "nginx -s reload"
  4. run "telnet 127.0.0.1 8080"
  5. send request
    GET /service/ 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
    
  6. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 30 Oct 2013 23:25:39 GMT
    Content-Type: text/html
    Content-Length: 20
    Connection: close
    Last-Modified: Wed, 30 Oct 2013 23:25:34 GMT
    ETag: "5271956e-14"
    Accept-Ranges: bytes
    
    This is service.html
  7. 如果把 debug log 打開, 就是把 nginx.conf 上的"#error_log  logs/error.log  debug;" 注解拿掉, 就可以看到 error.log 裡面出現下面這些 log, 看似 nginx 在 proxy_pass 的時候改寫了 http request 然後重新發一次. (不過說起來 proxy 本來就是在作這種事情就是了)

    2013/10/31 07:25:39 [debug] 4348#6716: *139 http script copy: "Host: "
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http script var: "127.0.0.1:12345"
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http script copy: "
    "
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http script copy: "Connection: close
    "
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http script copy: ""
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http script copy: ""
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http proxy header: "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http proxy header: "Accept: */*"
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http proxy header: "Accept-Encoding: gzip,deflate,sdch"
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http proxy header: "Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4"
    2013/10/31 07:25:39 [debug] 4348#6716: *139 http proxy header:
    "GET /service/ HTTP/1.0
    Host: 127.0.0.1:12345
    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. send request
    GET /service/serviceA/serviceA.html 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
    
  9. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 30 Oct 2013 23:33:12 GMT
    Content-Type: text/html
    Content-Length: 21
    Connection: close
    Last-Modified: Wed, 30 Oct 2013 23:33:08 GMT
    ETag: "52719734-15"
    Accept-Ranges: bytes
    
    This is serviceA.html

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

Nginx Practice in Windows - 4 server names

Description

    server {
        listen       127.0.0.1:8080;
        server_name  server1;
        location /test {
            return 200 'server1';
        }
    } 
在這個 server 設定中, server_name 是拿來比對 request header "HOST" 然後決定交由哪個 server 提供服務.
這個 server_name 除了能服務完全相同的 HOST 以外還支援 * (wildcard) 跟 regular expression

Reference

Practice

如果與 HOST 完全一樣, 就取 HOST 完全一樣的 server.

  1. setup nginx.conf (這裡 ~開頭就表示要用 regular expression)
        server {
            listen       127.0.0.1:8080 default;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'www.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test 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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 23 Oct 2013 22:36:16 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server1
    

一個 server_name 不能指定兩個 *

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1.*;
            location /test {
                return 200 '*.server1';
            }
        } 
    
  2. run "nginx -s reload"
  3. got error message
    nginx: [emerg] invalid server name or wildcard "*.server1.*" on 127.0.0.1:8080
    

當 * 開頭跟 * 結尾的兩個 server_name 都 mapping 到一個 request 時, 用 * 開頭的 server 服務

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'server1.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: www.server1
    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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 23 Oct 2013 22:58:48 GMT
    Content-Type: application/octet-stream
    Content-Length: 9
    Connection: close
    
    *.server1
    
  6. run "telnet 127.0.0.1 8080"
  7. send request (這個 request 的 Host 比較長, 但 nginx 仍可以判斷)
    GET /test HTTP/1.1
    Host: www.abc.qqq.hahaha.server1
    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: Wed, 23 Oct 2013 22:58:48 GMT
    Content-Type: application/octet-stream
    Content-Length: 9
    Connection: close
    
    *.server1

如果 mapping 到的 * 開頭的 server 不止一個, 就取 mapping 到最長的 server

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  *.hahaha.server1;
            location /test {
                return 200 '*.hahaha.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'www.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: www.abc.qqq.hahaha.server1
    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
    
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 23 Oct 2013 23:07:06 GMT
    Content-Type: application/octet-stream
    Content-Length: 16
    Connection: close
    
    *.hahaha.server1

如果對應不到 * 開頭的 server_name, 就找 mapping 到最長的以 * 結尾的 server_name

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  *.hahaha.server1;
            location /test {
                return 200 '*.hahaha.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'www.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.abc.qqq.*;
            location /test {
                return 200 'www.abc.qqq.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: www.abc.qqq.hahaha.server1.net
    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
    
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 23 Oct 2013 23:13:59 GMT
    Content-Type: application/octet-stream
    Content-Length: 13
    Connection: close
    
    www.abc.qqq.*

"*" 只能出現在 server_name 的開頭或結尾, 不能放中間

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1.*;
            location /test {
                return 200 '*.server1.*';
            }
        } 
    
  2. run "nginx -s reload"
  3. got error message
    nginx: [emerg] invalid server name or wildcard "*.server1.*" on 127.0.0.1:8080

如果 * 也都對應不到 server_name, 就用 regular expression (~ 開頭) 找第一個對應到的 server_name

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  *.hahaha.server1;
            location /test {
                return 200 '*.hahaha.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'www.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.abc.qqq.*;
            location /test {
                return 200 'www.abc.qqq.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  ~.*abc\.qqq\.hahaha\.server1.*;
            location /test {
                return 200 '~.*abc\.qqq\.hahaha\.server1.*';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: xyz.abc.qqq.hahaha.server1.net
    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
    
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 23 Oct 2013 23:37:15 GMT
    Content-Type: application/octet-stream
    Content-Length: 13
    Connection: close
    
    ~.*server1.*
  6. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  *.hahaha.server1;
            location /test {
                return 200 '*.hahaha.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'www.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.abc.qqq.*;
            location /test {
                return 200 'www.abc.qqq.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  ~.*abc\.qqq\.hahaha\.server1.*;
            location /test {
                return 200 '~.*abc\.qqq\.hahaha\.server1.*';
            }
        } 
    
        server {
            listen       127.0.0.1:8080;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        } 
  7. run "nginx -s reload"
  8. run "telnet 127.0.0.1 8080"
  9. send request
    GET /test HTTP/1.1
    Host: xyz.abc.qqq.hahaha.server1.net
    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: Wed, 23 Oct 2013 23:39:05 GMT
    Content-Type: application/octet-stream
    Content-Length: 31
    Connection: close
    
    ~.*abc\.qqq\.hahaha\.server1.*

如果 server_name 的 regular expression 包含 "{" 跟 "}", 就必須用 "" 把 regular expression 包起來, 否則 nginx 會報錯

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080 default;
            server_name  ~.*abc\.q{3}\.hahaha\.server1.*;
            location /test {
                return 200 '~.*abc\.q{3}\.hahaha\.server1.*';
            }
        }
     
  2. run "nginx -s reload"
  3. get error message
    nginx: [emerg] directive "server_name" is not terminated by ";" in C:\Users\isaac\Downloads\nginx-1.4.3\nginx-1.4.3/conf/nginx.conf:72
    
  4. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  *.server1;
            location /test {
                return 200 '*.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  *.hahaha.server1;
            location /test {
                return 200 '*.hahaha.server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.*;
            location /test {
                return 200 'www.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  www.abc.qqq.*;
            location /test {
                return 200 'www.abc.qqq.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  "~.*abc\.q{3}\.hahaha\.server1.*";
            location /test {
                return 200 '~.*abc\.q{3}\.hahaha\.server1.*';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  ~.*server1.*;
            location /test {
                return 200 '~.*server1.*';
            }
        }
     
  5. run "nginx -s reload"
  6. run "telnet 127.0.0.1 8080"
  7. send request
    GET /test HTTP/1.1
    Host: xyz.abc.qqq.hahaha.server1.net
    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: Wed, 23 Oct 2013 23:51:10 GMT
    Content-Type: application/octet-stream
    Content-Length: 31
    Connection: close
    
    ~.*abc\.q{3}\.hahaha\.server1.*

在 server_name 可用 regular expression 替 mapping 到的值命名成變數以便在之後使用

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  ~(?P.*)\.server1.*;
            location /test {
                return 200 'you request ${serviceName} service, service_name pattern ~(?P.*)\.server1.* is Python compatible syntax, supported since PCRE-4.0';
            }
        } 
        server {
            listen       127.0.0.1:8081;
            server_name  ~(?.*)\.server1.*;
            location /test {
                return 200 'you request ${serviceName} service, service_name pattern ~(?.*)\.server1.* is Perl 5.10 compatible syntax, supported since PCRE-7.0';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: tw.mail.server1.net
    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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sun, 27 Oct 2013 05:14:53 GMT
    Content-Type: application/octet-stream
    Content-Length: 135
    Connection: close
    
    you request tw.mail service, service_name pattern ~(?P.*)\.server1.* is Python compatible syntax, supported since PCRE-4.0
    
  6. run "telnet 127.0.0.1 8081"
  7. send request
    GET /test HTTP/1.1
    Host: tw.mail.server1.net
    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: Sun, 27 Oct 2013 05:16:06 GMT
    Content-Type: application/octet-stream
    Content-Length: 137
    Connection: close
    
    you request tw.mail service, service_name pattern ~(?.*)\.server1.* is Perl 5.10 compatible syntax, supported since PCRE-7.0
    
    

在 server_name 透過 regular expression 將 mapping 到的值可在之後用數字編號當變數使用

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  ~(.*)\.(.*);
            location /test {
                return 200 'you request service "$1" in domain "$2"';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: mail.server1
    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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sun, 27 Oct 2013 05:22:37 GMT
    Content-Type: application/octet-stream
    Content-Length: 46
    Connection: close
    
    you request service "mail" in domain "server1"
    

如果沒指定 server_name, nginx 就會用 hostname 作為 server_name

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            location /test {
                return 200 'you request ${hostname}';
            }
        }  
    
  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
    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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Sun, 27 Oct 2013 05:41:41 GMT
    Content-Type: application/octet-stream
    Content-Length: 20
    Connection: close
    
    you request isaac-pc

設定一個"全部"的 server_name

  1. setup nginx.conf
        server {
            listen       8080;
            server_name  _;
            location / {
                return 200 'you request ${http_Host}';
            }
        }
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET / HTTP/1.1
    Host: abc: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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 30 Oct 2013 23:39:30 GMT
    Content-Type: application/octet-stream
    Content-Length: 20
    Connection: close
    
    you request abc:8080
  6. setup nginx.conf
        server {
            listen       8080;
            server_name  --;
            location / {
                return 200 'you request ${http_Host}';
            }
        }  
  7. run "nginx -s reload"
  8. run "telnet 127.0.0.1 8080"
  9. send request
    GET / HTTP/1.1
    Host: abc: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: Wed, 30 Oct 2013 23:39:30 GMT
    Content-Type: application/octet-stream
    Content-Length: 20
    Connection: close
    
    you request abc:8080
  11. setup nginx.conf
  12.     server {
            listen       8080;
            server_name  !@#;
            location / {
                return 200 'you request ${http_Host}';
            }
        }  
  13. run "nginx -s reload"
  14. run "telnet 127.0.0.1 8080"
  15. send request
    GET / HTTP/1.1
    Host: abc: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
    
  16. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Wed, 30 Oct 2013 23:39:30 GMT
    Content-Type: application/octet-stream
    Content-Length: 20
    Connection: close
    
    you request abc:8080

Nginx Practice in Windows - 3

Reference

Description

    server {
        listen       8080;
        server_name  example.com;
        location /test {
           return 200 'example.com';
        }
    }
nginx 這個 server 區塊的設定中, server_name 是用來判斷 request 的 Host header 是打算導給哪個 server. 比方說這個例子就是如果 header 指定了 example.com 會導向這個 server.

Practice

如果有找到與 request header 的 Host 相對應的 server, 就會導過去

  1. setup nginx.conf
        server {
            listen       8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       8080;
            server_name  server2;
            location /test {
                return 200 'server2';
            }
        } 
    
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: server2: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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 23:04:24 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server2
    

沒有指定誰是預設 server 而且透過 request header 的 Host 又找不到相對應的 server_name, 就會回傳第一個.

  1. setup nginx.conf
        server {
            listen       8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       8080;
            server_name  server2;
            location /test {
                return 200 'server2';
            }
        } 
    
  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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 22:55:05 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server1
    

有指定 default_server, 當 request header Host 找不到相對應的 server_name 時, 就會導向 default server. (文件上說0.8.21之後使用 default_server, 之前使用 default. 不過我在 nginx-1.4.3 for Windows 要使用 default 才可以. 是 Linux 版的才可改 default_server?)

  1. setup nginx.conf
        server {
            listen       8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       8080 default;
            server_name  server2;
            location /test {
                return 200 'server2';
            }
        } 
    
  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
    
  5. get response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 22:59:23 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server2
    

如果指定的 server 包含不同 ip, 那每個 ip 就會有自己的 default server.

  1. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080;
            server_name  server2;
            location /test {
                return 200 'server2';
            }
        } 
     server {
            listen       127.0.0.2:8081;
            server_name  server3;
            location /test {
                return 200 'server3';
            }
        } 
        server {
            listen       127.0.0.2:8081;
            server_name  server4;
            location /test {
                return 200 'server4';
            }
        }
    
     
  2. run "nginx -s reload"
  3. run "telnet 127.0.0.1 8080"
  4. send request
    GET /test HTTP/1.1
    Host: localhost
    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 
  5. get responseget response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 23:20:33 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server1
  6. run "telnet 127.0.0.2 8081"
  7. send request
    GET /test HTTP/1.1
    Host: localhost
    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 responseget response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 23:21:52 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server3
  9. setup nginx.conf
        server {
            listen       127.0.0.1:8080;
            server_name  server1;
            location /test {
                return 200 'server1';
            }
        } 
        server {
            listen       127.0.0.1:8080 default;
            server_name  server2;
            location /test {
                return 200 'server2';
            }
        } 
     server {
            listen       127.0.0.2:8081;
            server_name  server3;
            location /test {
                return 200 'server3';
            }
        } 
        server {
            listen       127.0.0.2:8081 default;
            server_name  server4;
            location /test {
                return 200 'server4';
            }
        } 
    
     
  10. run "nginx -s reload"
  11. run "telnet 127.0.0.1 8080"
  12. send request
    GET /test HTTP/1.1
    Host: localhost
    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 
  13. get responseget response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 23:20:33 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server2
  14. run "telnet 127.0.0.2 8081"
  15. send request
    GET /test HTTP/1.1
    Host: localhost
    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 
  16. get responseget response
    HTTP/1.1 200 OK
    Server: nginx/1.4.3
    Date: Mon, 21 Oct 2013 23:21:52 GMT
    Content-Type: application/octet-stream
    Content-Length: 7
    Connection: close
    
    server4


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
    

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 作為權重. 解法 別名演算法...