nginx 代理模組
說明:代理模組的指令有很多我這裡只講解重要的proxy_pass,想了解更多代理指令請參考官方中文文件。
這個模組可以轉送請求到其他的伺服器。 http/1.0無法使用keepalive(後端伺服器將為每個請求建立並且刪除連線)。 nginx為瀏覽器發送http/1.1並為後端伺服器發送http/1.0,這樣瀏覽器就可以為瀏覽器處理keepalive。
如下例:
location / { proxy_pass http://localhost:8000; proxy_set_header x-real-ip $remote_addr; }
注意,當使用http proxy模組(甚至fastcgi),所有的連接請求在發送到後端伺服器之前nginx將快取它們,因此,在測量從後端傳送的數據時,它的進度顯示可能不正確。
實驗拓樸:
#7.設定http反向代理
[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; }
指令說明:proxy_pass
語法:proxy_pass url
預設值:no
使用欄位:location, location中的if欄位
這個指令設定被代理伺服器的位址和被映射的uri,位址可以使用主機名稱或ip加上連接埠號碼的形式,例如:proxy_pass http://localhost:8000/uri/;
8.重新載入一下設定檔
[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: [确定]
9.測試一下
註,大家可以看到,當我們造訪192.168.18.208時,被代理重新定向到web1上。
10.查看一下web伺服器日誌
[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"
註,大家可以看到我們這裡的客戶的ip全是,nginx代理伺服器的ip,並不是真實客戶端的ip 。下面我們修改一下,讓日誌的ip顯示真實的客戶端的ip。
11.修改nginx設定檔
location / { proxy_pass http://192.168.18.201; proxy_set_header x-real-ip $remote_addr; #加上这一行 }
指令說明:proxy_set_header
語法:proxy_set_header header value
預設值: host and connection
使用欄位:http, server, location
這個指令允許將傳送到被代理伺服器的請求頭重新定義或增加一些欄位。這個值可以是一個文本,變數或它們的組合。 proxy_set_header在指定的欄位中沒有定義時會從它的上級欄位繼承。
12.重新載入一下設定檔
[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: [确定]
13.測試並查看日誌
[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)"
注,大家可以看到日誌記錄的還是代理的ip,沒有顯示真實客戶端的ip,為什麼呢?我們來看看httpd的設定檔。
14.查看並修改httpd設定檔
[root@web1 ~]# vim /etc/httpd/conf/httpd.conf
#註,大家可以這裡記錄日誌的參數還是%h,下面我們修改一下參數。
註,這是修改後的參數,將h%修改為%{x-real-ip}i,好的下面我們再來測試一下。
15.重啟並測試
[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)"
註,大家可以看到現在的日誌裡記錄的ip位址就是真實的客戶端位址了。
以上是nginx代理模組怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!