1
2
|
[root@web1
~] #
yum install -y httpd [root@web2
~] #
yum install -y httpd |
1
2
|
[root@web1
~] #
echo "<h1>web1.test.com</h1>" > /var/www/html/index.html [root@web2
~] #
echo "<h1>web2.test.com</h1>" > /var/www/html/index.html |
1
2
3
4
|
[root@web1
~] #
service httpd start 正在啓動
httpd: [確定] [root@web2
~] #
service httpd start 正在啓動
httpd: [確定] |
(1).正向代理的概念
正向代理,也就是傳說中的代理,他的工作原理就像一個跳板,簡單的說,我是一個用戶,我訪問不了某網站,但是我能訪問一個代理服務器,這個代理服務器呢,他能訪問那個我不能訪問的網站,於是我先連上代理服務器,告訴他我需要那個無法訪問網站的內容,代理服務器去取回來,然後返回給我。從網站的角度,只在代理服務器來取內容的時候有一次記錄,有時候並不知道是用戶的請求,也隱藏了用戶的資料,這取決於代理告不告訴網站。
(2).反向代理的概念
(3).兩者區別
1
2
3
4
|
location
/ { proxy_pass
http: //localhost :8000; proxy_set_header
X-Real-IP $remote_addr; } |
1
2
3
4
5
6
|
[root@nginx
~] #
cd /etc/nginx/ [root@nginx
nginx] #
cp nginx.conf nginx.conf.bak #備份一個原配置文件 [root@nginx
nginx] #
vim nginx.conf location
/ { proxy_pass
http: //192 .168.18.201; } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
4
5
6
7
8
9
10
11
|
[root@web1
~] #
tail /var/log/httpd/access_log 192.168.18.208
- - [04 /Sep/2013 :00:14:20
+0800] "GET
/favicon.ico HTTP/1.0" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:14:20
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:14:20
+0800] "GET
/favicon.ico HTTP/1.0" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.138
- - [04 /Sep/2013 :00:14:45
+0800] "GET
/ HTTP/1.1" 200
23 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.138
- - [04 /Sep/2013 :00:14:48
+0800] "GET
/favicon.ico HTTP/1.1" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:14:55
+0800] "GET
/favicon.ico HTTP/1.0" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:15:05
+0800] "GET
/favicon.ico HTTP/1.0" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:15:13
+0800] "GET
/favicon.ico HTTP/1.0" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:15:16
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 192.168.18.208
- - [04 /Sep/2013 :00:15:16
+0800] "GET
/favicon.ico HTTP/1.0" 404
289 "-" "Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" |
1
2
3
4
|
location
/ { proxy_pass
http: //192 .168.18.201; proxy_set_header
X-Real-IP $remote_addr; #加上這一行 } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
4
5
6
7
8
9
10
11
|
[root@web1
~] #
tail /var/log/httpd/access_log 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.208
- - [03 /Sep/2013 :16:26:18
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@web1
~] #
service httpd restart 停止
httpd: [確定] 正在啓動
httpd: [確定] [root@web1
~] #
tail /var/log/httpd/access_log 192.168.18.138
- - [03 /Sep/2013 :17:09:14
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:14
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [03 /Sep/2013 :17:09:15
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" |
1
2
3
4
5
6
7
8
9
10
11
12
|
upstream test .net{ ip_hash; server
192.168.10.13:80; server
192.168.10.14:80 down; server
192.168.10.15:8009 max_fails=3 fail_timeout=20s; server
192.168.10.16:8080; } server
{ location
/ { proxy_pass
http: //test .net; } } |
-
輪詢(默認)。每個請求按時間順序逐一分配到不同的後端服務器,如果後端某臺服務器宕機,故障系統被自動剔除,使用戶訪問不受影響。Weight 指定輪詢權值,Weight值越大,分配到的訪問機率越高,主要用於後端每個服務器性能不均的情況下。
-
ip_hash。每個請求按訪問IP的hash結果分配,這樣來自同一個IP的訪客固定訪問一個後端服務器,有效解決了動態網頁存在的session共享問題。
-
fair。這是比上面兩個更加智能的負載均衡算法。此種算法可以依據頁面大小和加載時間長短智能地進行負載均衡,也就是根據後端服務器的響應時間來分配請求,響應時間短的優先分配。Nginx本身是不支持fair的,如果需要使用這種調度算法,必須下載Nginx的upstream_fair模塊。
-
url_hash。此方法按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,可以進一步提高後端緩存服務器的效率。Nginx本身是不支持url_hash的,如果需要使用這種調度算法,必須安裝Nginx 的hash軟件包。
-
down,表示當前的server暫時不參與負載均衡。
-
backup,預留的備份機器。當其他所有的非backup機器出現故障或者忙的時候,纔會請求backup機器,因此這臺機器的壓力最輕。
-
max_fails,允許請求失敗的次數,默認爲1。當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤。
-
fail_timeout,在經歷了max_fails次失敗後,暫停服務的時間。max_fails可以和fail_timeout一起使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@nginx
~] #
vim /etc/nginx/nginx.conf upstream
webservers { server
192.168.18.201 weight=1; server
192.168.18.202 weight=1; } server
{ listen
80; server_name
localhost; #charset
koi8-r; #access_log
logs/host.access.log main; location
/ { proxy_pass
http: //webservers ; proxy_set_header
X-Real-IP $remote_addr; } } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
4
5
6
7
8
9
10
11
|
[root@web1
~] #
tail /var/log/httpd/access_log 192.168.18.138
- - [04 /Sep/2013 :09:41:58
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:41:58
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:41:59
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:41:59
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:42:00
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:42:00
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:42:00
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:44:21
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:44:22
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:44:22
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" |
1
2
3
4
5
|
[root@web2
~] #
vim /etc/httpd/conf/httpd.conf LogFormat "%{X-Real-IP}i
%l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined [root@web2
~] #
service httpd restart 停止
httpd: [確定] 正在啓動
httpd: [確定] |
1
2
3
4
5
6
7
8
9
10
11
|
[root@web2
~] #
tail /var/log/httpd/access_log 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:28
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:29
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 192.168.18.138
- - [04 /Sep/2013 :09:50:29
+0800] "GET
/ HTTP/1.0" 200
23 "-" "Mozilla/5.0
(compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" |
-
max_fails,允許請求失敗的次數,默認爲1。當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤。
-
fail_timeout,在經歷了max_fails次失敗後,暫停服務的時間。max_fails可以和fail_timeout一起使用,進行健康狀態檢查。
1
2
3
4
5
|
[root@nginx
~] #
vim /etc/nginx/nginx.conf upstream
webservers { server
192.168.18.201 weight=1 max_fails=2 fail_timeout=2; server
192.168.18.202 weight=1 max_fails=2 fail_timeout=2; } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
|
先停止Web1,進行測試。 [root@web1
~] #
service httpd stop 停止
httpd: [確定] |
1
2
|
[root@web1
~] #
service httpd start 正在啓動
httpd: [確定] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@nginx
~] #
vim /etc/nginx/nginx.conf server
{ listen
8080; server_name
localhost; root /data/www/errorpage ; index
index.html; } upstream
webservers { server
192.168.18.201 weight=1 max_fails=2 fail_timeout=2; server
192.168.18.202 weight=1 max_fails=2 fail_timeout=2; server
127.0.0.1:8080 backup; } [root@nginx
~] #
mkdir -pv /data/www/errorpage [root@nginx
errorpage] #
cat index.html <h1>Sorry......< /h1 > |
1
2
3
4
|
[root@nginx
errorpage] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
4
|
[root@web1
~] #
service httpd stop 停止
httpd: [確定] [root@web2
~] #
service httpd stop 停止
httpd: [確定] |
-
ip_hash,每個請求按訪問IP的hash結果分配,這樣來自同一個IP的訪客固定訪問一個後端服務器,有效解決了動態網頁存在的session共享問題。(一般電子商務網站用的比較多)
1
2
3
4
5
6
7
|
[root@nginx
~] #
vim /etc/nginx/nginx.conf upstream
webservers { ip_hash; server
192.168.18.201 weight=1 max_fails=2 fail_timeout=2; server
192.168.18.202 weight=1 max_fails=2 fail_timeout=2; #server
127.0.0.1:8080 backup; } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
|
[root@web2
~] #
netstat -an | grep :80 | wc -l 304 |
proxy_cache_path
1
|
proxy_cache_path /data/nginx/cache levels=1:2
keys_zone=one:10m; |
1
|
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c |
1
2
3
|
proxy_cache_path /data/nginx/cache/one levels=1
keys_zone=one:10m; proxy_cache_path /data/nginx/cache/two levels=2:2
keys_zone=two:100m; proxy_cache_path /data/nginx/cache/three levels=1:1:2
keys_zone=three:1000m; |
proxy_cache
proxy_cache_valid
1
2
|
proxy_cache_valid
200 302 10m; proxy_cache_valid
404 1m; |
1
|
proxy_cache_valid
5m; |
1
2
3
|
proxy_cache_valid
200 302 10m; proxy_cache_valid
301 1h; proxy_cache_valid
any 1m; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@nginx
~] #
vim /etc/nginx/nginx.conf proxy_cache_path /data/nginx/cache/webserver levels=1:2
keys_zone=webserver:20m max_size=1g; server
{ listen
80; server_name
localhost; #charset
koi8-r; #access_log
logs/host.access.log main; location
/ { proxy_pass
http: //webservers ; proxy_set_header
X-Real-IP $remote_addr; proxy_cache
webserver; proxy_cache_valid
200 10m; } } |
1
|
[root@nginx
~] #
mkdir -pv /data/nginx/cache/webserver |
1
2
3
4
|
[root@nginx
webserver] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
$server_addr
$upstream_cache_status
-
MISS 未命中
-
EXPIRED - expired。請求被傳送到後端。
-
UPDATING - expired。由於proxy/fastcgi_cache_use_stale正在更新,將使用舊的應答。
-
STALE - expired。由於proxy/fastcgi_cache_use_stale,後端將得到過期的應答。
-
HIT 命中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@nginx
~] #
vim /etc/nginx/nginx.conf proxy_cache_path /data/nginx/cache/webserver levels=1:2
keys_zone=webserver:20m max_size=1g; server
{ listen
80; server_name
localhost; #charset
koi8-r; #access_log
logs/host.access.log main; #增加兩頭部 add_header
X-Via $server_addr; add_header
X-Cache $upstream_cache_status; location
/ { proxy_pass
http: //webservers ; proxy_set_header
X-Real-IP $remote_addr; proxy_cache
webserver; proxy_cache_valid
200 10m; } } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
|
[root@nginx
~] #
cd /data/nginx/cache/webserver/f/63/ [root@nginx
63] #
ls 681ad4c77694b65d61c9985553a2763f |
1.URL重寫模塊(Rewrite)
指令
break
1
2
3
4
|
if ($slow)
{ limit_rate
10k; break ; } |
if
-
一個變量的名稱;不成立的值爲:空字符傳”“或者一些用“0”開始的字符串。
-
一個使用=或者!=運算符的比較語句。
-
使用符號~*和~模式匹配的正則表達式:
-
~爲區分大小寫的匹配。
-
~*不區分大小寫的匹配(firefox匹配FireFox)。
-
!~和!~*意爲“不匹配的”。
-
使用-f和!-f檢查一個文件是否存在。
-
使用-d和!-d檢查一個目錄是否存在。
-
使用-e和!-e檢查一個文件,目錄或者軟鏈接是否存在。
-
使用-x和!-x檢查一個文件是否爲可執行文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
if ($http_user_agent
~ MSIE) { rewrite
^(.*)$ /msie/ $1 break ; } if ($http_cookie
~* "id=([^;]
+)(?:;|$)" )
{ set $ id $1; } if ($request_method
= POST ) { return 405; } if (!-f
$request_filename) { break ; proxy_pass
http: //127 .0.0.1; } if ($slow)
{ limit_rate
10k; } if ($invalid_referer)
{ return 403; } if ($args
~ post=140){ rewrite
^ http: //example .com/
permanent; } |
return
rewrite
-
last - 完成重寫指令,之後搜索相應的URI或location。
-
break - 完成重寫指令。
-
redirect - 返回302臨時重定向,如果替換字段用http://開頭則被使用。
-
permanent - 返回301永久重定向。
1
2
3
|
rewrite
^( /download/ .*) /media/ (.*)\..*$
$1 /mp3/ $2.mp3
last; rewrite
^( /download/ .*) /audio/ (.*)\..*$
$1 /mp3/ $2.ra
last; return 403; |
1
2
3
4
5
|
location /download/ { rewrite
^( /download/ .*) /media/ (.*)\..*$
$1 /mp3/ $2.mp3 break ; rewrite
^( /download/ .*) /audio/ (.*)\..*$
$1 /mp3/ $2.ra break ; return 403; } |
1
|
rewrite
^ /users/ (.*)$ /show ?user=$1?
last; |
1
|
/photos/123456 |
1
|
/path/to/photos/12/1234/123456 .png |
1
|
rewrite "/photos/([0-9]
{2})([0-9] {2})([0-9] {2})" /path/to/photos/ $1/$1$2/$1$2$3.png; |
1
2
3
4
|
server
{ server_name
www.example.com; rewrite
^ http: //example .com$request_uri?
permanent; } |
1
2
3
|
if ($args
^~ post=100){ rewrite
^ http: //example .com /new-address .html?
permanent; } |
rewrite_log
set
uninitialized_variable_warn
1
2
3
4
5
6
7
8
|
location /download/ { if ($forbidden)
{ return 403; } if ($slow)
{ limit_rate
10k; } rewrite
^/(download/.*) /media/ (.*)\..*$
/$1 /mp3/ $2.mp3 break ; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
variable
$forbidden checking
to zero recovery
403 completion
of entire code variable
$slow checking
to zero checkings
of regular excodession copying "/" copying
$1 copying "/mp3/" copying
$2 copying ".mp3" completion
of regular excodession completion
of entire sequence |
1
|
rewrite
^/(download/.*) /media/ (.*)\..*$
/$1 /mp3/ $2.mp3 break ; |
1
|
rewrite
^( /download/ .*) /media/ (.*)\..*$
$1 /mp3/ $2.mp3 break ; |
1
2
3
4
5
6
7
|
checking
regular excodession copying
$1 copying "/mp3/" copying
$2 copying ".mp3" completion
of regular excodession completion
of entire code |
2.簡單案例
注,由於配置文件內容較多,爲了讓大家看着方便,我們備份一下配置文件,打開一個新的配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@nginx
~] #
cd /etc/nginx/ [root@nginx
nginx] #
mv nginx.conf nginx.conf.proxy [root@nginx
nginx] #
cp nginx.conf.bak nginx.conf [root@nginx
nginx] #
vim /etc/nginx/nginx.conf server
{ listen
80; server_name
localhost; #charset
koi8-r; #access_log
logs/host.access.log main; location
/ { root
html; index
index.html index.htm; rewrite
^ /bbs/ (.*)$
http: //192 .168.18.201 /forum/ $1; } } |
準備forum目錄與測試文件
1
2
3
4
5
6
7
|
[root@web1
~] #
cd /var/www/html/ [root@web1
html] #
ls index.html [root@web1
html] #
mkdir forum [root@web1
html] #
cd forum/ [root@web1
forum] #
vim index.html <h1>forum
page!< /h1 > |
3.重新加載一下配置文件
1
2
3
4
|
[root@nginx
63] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@nginx
nginx] #
vim /etc/nginx/nginx.conf server
{ listen
80; server_name
localhost; #charset
koi8-r; #access_log
logs/host.access.log main; location
/ { root
html; index
index.html index.htm; rewrite
^ /bbs/ (.*)$ /forum/ $1; } } |
準備forum目錄與測試文件
1
2
3
4
5
6
7
|
[root@nginx
~] #
cd /usr/html/ [root@nginx
html] #
ls 50x.html
index.html [root@nginx
html] #
mkdir forum [root@nginx
html] #
cd forum/ [root@nginx
forum] #
vim index.html <h1>192.168.18.208
forum page< /h1 > |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@nginx
nginx] #
vim /etc/nginx/nginx.conf server
{ listen
80; server_name
localhost; #charset
koi8-r; #access_log
logs/host.access.log main; location
/ { proxy_pass
http: //192 .168.18.202; if ($request_method
= "PUT" ){ proxy_pass
http: //192 .168.18.201; } } } |
1
2
3
4
|
[root@nginx
~] #
service nginx reload nginx:
the configuration file /etc/nginx/nginx .conf
syntax is ok nginx:
configuration file /etc/nginx/nginx .conf test is
successful 重新載入
nginx: [確定] |
1
|
[root@web1
~] #
vim /etc/httpd/conf/httpd.conf |
1
2
3
|
[root@web1
~] #
service httpd restart 停止
httpd: [確定] 正在啓動
httpd: [確定] |
1
2
3
4
|
[root@nginx
~] #
curl http://192.168.18.201 <h1>web1. test .com< /h1 > [root@nginx
~] #
curl http://192.168.18.202 <h1>web2. test .com< /h1 > |
1
2
3
4
5
6
7
8
9
10
|
[root@nginx
~] #
curl -T /etc/issue http://192.168.18.202 <!DOCTYPE
HTML PUBLIC "-//IETF//DTD
HTML 2.0//EN" > <html>< head > <title>405
Method Not Allowed< /title > < /head ><body> <h1>Method
Not Allowed< /h1 > The
requested method PUT is not allowed for the
URL /issue . <hr> <address>Apache /2 .2.15
(CentOS) Server at 192.168.18.202 Port 80< /address > < /body >< /html > |
1
2
3
4
5
6
7
8
9
10
11
|
[root@nginx
~] #
curl -T /etc/issue http://192.168.18.201 <!DOCTYPE
HTML PUBLIC "-//IETF//DTD
HTML 2.0//EN" > <html>< head > <title>403
Forbidden< /title > < /head ><body> <h1>Forbidden< /h1 > You
don't have permission to access /issue on
this server. <hr> <address>Apache /2 .2.15
(CentOS) Server at 192.168.18.201 Port 80< /address > < /body >< /html > |
1
|
[root@web1
~] #
setfacl -m u:apache:rwx /var/www/html/ |
1
2
3
4
5
6
7
8
9
10
|
[root@nginx
~] #
curl -T /etc/issue http://192.168.18.201 <!DOCTYPE
HTML PUBLIC "-//IETF//DTD
HTML 2.0//EN" > <html>< head > <title>201
Created< /title > < /head ><body> <h1>Created< /h1 > Resource /issue has
been created. <hr
/> <address>Apache /2 .2.15
(CentOS) Server at 192.168.18.201 Port 80< /address > < /body >< /html > |
1
2
3
4
5
6
|
[root@web1
~] #
cd /var/www/html/ [root@web1
html] #
ll 總用量
12 drwxr-xr-x
2 root root 4096 9月 4 13:16 forum -rw-r--r--
1 root root 23 9月 3 23:37 index.html -rw-r--r--
1 apache apache 47 9月 4 14:06 issue |