Description
使用 nginx 主要目的之一就是要存放靜態檔如 html 或圖片
Reference
Practice
設定讓 "GET /" 的 request 首頁導向 testhtml/data/www/index.html
- 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
- edit index.html
Hello!, This is testhtml\data\www\index.html
- setup nginx.conf
server { listen 8080; server_name 127.0.0.1; location / { root testhtml/data/www; index index.html; } }
- run "nginx -s reload"
- run "telnet 127.0.0.1 8080"
- 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
- 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
設定去取 "/" 以外的檔案
- 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
- edit data.html
Hello!, This is data.html
- edit www.html
Hello!, This is www.html
- 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; } }
- run "nginx -s reload"
- run "telnet 127.0.0.1 8080"
- 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
- 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
- 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
- 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
- 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
- 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 即可
- 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
- edit data.html
Hello!, This is data.html
- edit testhtml.html
Hello!, This is testhtml.html
- setup nginx.conf
server { listen 8080; server_name 127.0.0.1; root testhtml; location / { index testhtml.html; } location /data/ { index data.html; } }
- run "nginx -s reload"
- run "telnet 127.0.0.1 8080"
- 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
- 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
- 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
- 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
沒有留言:
張貼留言