JDK6 to JDK7 => API Enhancements

Description

打算把系統的執行環境從 JDK6 升級到 JDK7, 整理一下有哪些可用.
比較期待的部分是語法, NIO2, G1. 這頁是整理語法的部分.

Reference

Points

  1. Strings in switch Statements
    package test;
    
    public class TestStringSwitch {
    
     public static void main(String[] params) {
      inJDK7("abc");
      inJDK6("abc");
     }
     
     private static void inJDK7(String txt) {
      switch (txt) {
      case "a":
       System.out.println("a");
       break;
      case "b":
       System.out.println("b");
       break;
      case "abc":
       System.out.println("abc");
       break;
      default:
        System.out.println("not match");
      }
     }
     
     private static void inJDK6(String txt) {
      if ( "a".equals(txt) ) {
       System.out.println("a");
      } else if ( "b".equals(txt) ) {
       System.out.println("b");
      } else if ( "abc".equals(txt) ) {
       System.out.println("abc");
      } else {
       System.out.println("not match");
      }
     }
     
     
    }
    
  2. Type Inference for Generic Instance Creation
    package test;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class TestGenericDeclare {
    
      public static void main(String[] params) {
        // JDK6
        Map> map0 = new HashMap>();
        
        // JDK7
        Map> map1 = new HashMap<>();
      }
      
    }
    
  3. The try-with-resources Statement
    試用後可以看到這個語法可以讓程式簡化很多.
    package test;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    
    public class TestAutoClosable {
    
        public static void main(String[] params) throws IOException {
            File file1 = new File("d:/test.txt");
            File file2 = new File("d:/test2.txt");
            inJDK6(file1,file2);
            inJDK7(file1,file2);
        }
    
        private static void inJDK7(File file1, File file2) throws IOException {
            try (
                FileReader fileReader1 = new FileReader(file1);
                FileReader fileReader2 = new FileReader(file2);
                BufferedReader reader1 = new MyBufferedReader("7 reader1", fileReader1);
                BufferedReader reader2 = new MyBufferedReader("7 reader2", fileReader2);
            ) {
                System.out.println(reader1.readLine());
                System.out.println(reader2.readLine());
            }
        }
        
        private static void inJDK6(File file1, File file2) {
            BufferedReader reader1 = null;
            BufferedReader reader2 = null;
            try {
                reader1 = new MyBufferedReader("JDK6 style reader1", new FileReader(file1));
                reader2 = new MyBufferedReader("JDK6 style reader2", new FileReader(file2));
                System.out.println(reader1.readLine());
                System.out.println(reader2.readLine());
            } catch(Throwable ex) {
                ex.printStackTrace();
            } finally {
                if ( reader1 != null ) {
                    try {
                        reader1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if ( reader2 != null ) {
                    try {
                        reader2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        private static class MyBufferedReader extends BufferedReader {
            private final String name;
            public MyBufferedReader(String name, Reader reader) {
                super(reader);
                this.name = name;
            }
            @Override
            public void close() throws IOException {
                super.close();
                System.out.println("close " + name);
            }
        }
        
    }
    
  4. Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking
    有時對於不特別處理的 checked exception 動作也只是往外丟, 在 JDK7 之前需要在 method 上特別註明這個 checked exception, 但 JDK7 之後, 沒有特別需要的, 直接往外丟的 exception 就算沒有在 method 上宣告, compiler 也不會判你錯.
    package test;
    
    public class TestHandleMoreThanOneException {
    
        public static void main(String[] params) throws Exception {
            rethrowEx("3");
        }
    
        private static void rethrowEx(String exNum) throws Ex1,Ex2 {
            try {
                throwEx(exNum);
            } catch (Ex1 | Ex2 e) {
                throw e;
            } catch (Exception e) {
                throw e;
            }
        }
        
        private static void throwEx(String exNum) throws Ex1, Ex2 {
            switch (exNum) {
            case "1":
                throw new Ex1();
            case "2":
                throw new Ex2();
            default:
                System.out.println("not match");
            }
        }
        
        public static class Ex1 extends Exception {}
        public static class Ex2 extends Exception {}
        
    }
    


python 處理 json

Description

現在很常用 json 當成儲存資料的格式, 用 java 的話需要導入 library.
python 則是內建 json 的 module, 只要操作原生的資料結構就行了. 很方便.

Reference

Codes

>>> import json
>>> m = {'a':1,'b':[1,2,3,4,5,6]}
>>> with open('d:/test.txt','w') as f:
        json.dump(m,f)

        
>>> with open('d:/test.txt') as f:
        f.readline()

        
'{"a": 1, "b": [1, 2, 3, 4, 5, 6]}'
>>> with open('d:/test.txt') as f:
        x = json.load(f)

        
>>> x
{'a': 1, 'b': [1, 2, 3, 4, 5, 6]}
>>> 

File content

{"a": 1, "b": [1, 2, 3, 4, 5, 6]}

python 的 main method

之前看 python 文章好像都沒提到怎麼寫 main method, 似乎這件事情很自然, 不過我卻不知道.
看了 6.1.1. Executing modules as scripts 裡面有寫
"the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module:"
if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))
試著作一次

  1. C:\Python33\say.py
    def say(words):
            print(words)
    
    if __name__ == '__main__':
            import sys
            say(sys.argv[1])
            print(__name__,sys.argv[0])
    
    
  2. Output
    C:\Python33>python.exe say.py hello
    hello
    __main__ say.py

這樣就可以了, 很簡單..

處理正在 iterate 的 dictionary


今天練習 Python 的時候發現 5.6. Looping Techniques 有個提示
"To change a sequence you are iterating over while inside the loop (for example to duplicate certain items), it is recommended that you first make a copy. Looping over a sequence does not implicitly make a copy."

照著文件上的程式輸入
>>> words = ['a','b','cc']
>>> for w in words:
    if len(w)>1:
        print('insert',w)
        words.insert(0,w)

執行之後就出現
insert cc
insert cc
insert cc
insert cc
insert cc
insert cc
insert cc
insertTraceback (most recent call last):
  File "<pyshell#208>", line 3, in <module>
    print('insert',w)
  File "C:\Python33\lib\idlelib\PyShell.py", line 1318, in write
    return self.shell.write(s, self.tags)
KeyboardInterrupt

要 Ctrl+C 才能停下來.
這時候檢查一下哪打錯了, 原來我打
for w in words:

但文件上是
for w in words[:]:

修正之後果然就正常了.
原來 words[:] 是 words 的 copy.
試一下
>>> words is words
True
>>> words is words[:]
False
果然沒錯.

在 Java 也是一樣, 如果要改變正在 iterate 的 list 也會有問題
package test;

import java.util.Arrays;
import java.util.List;

public class TestIter {

    public static void main(String[] params) {
        List words = Arrays.asList("a","b","cc");
        for (String w: words) {
            if (w.length() > 1) {
                System.out.println("insert " + w);
                words.add(w);
            }
        }
    }
    
}
會報錯
insert cc
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at test.TestIter.main(TestIter.java:13)

要改成
package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestIter {

    public static void main(String[] params) {
        List words = Arrays.asList("a","b","cc");
        List copyWords = new ArrayList(words);
        for (String w: words) {
            if (w.length() > 1) {
                System.out.println("insert " + w);
                copyWords.add(0,w);
            }
        }
        System.out.println(words);
        System.out.println(copyWords);
    }
    
}

寫了 Python 跟 Java 這兩段程式, 感覺有微妙的差異...

巴斯卡三角形 in Python

Source Code

def print_pascal(row_cnt):
    pascal = []
    for row in range(row_cnt):
        cols = []
        for col in range(row+1):
            if row == 0:
                cols.append(1)
            elif col == 0 or col == row:
                cols.append(1)
            else:
                val = pascal[row-1][col-1]+pascal[row-1][col];
                cols.append(val)
        pascal.append(cols)
    for row in pascal:
        for i in range(row_cnt-len(row)):
            print('%5s' %'',end='')
        for col in row:
            print('%5d' %col,end='%5s' %'')
        print()

Usage


>>> print_pascal(6)
                             1     
                        1         1     
                   1         2         1     
              1         3         3         1     
         1         4         6         4         1     
    1         5        10        10         5         1  

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

Lessons Learned While Benchmarking vLLM with GPU

Recently, I benchmarked vLLM on a GPU to better understand how much throughput can realistically be expected in an LLM serving setup. One ...