nginx代理模塊怎么使用

nginx 代理模塊

說明:代理模塊的指令有很多我這里只講解重要的proxy_pass,想了解更多代理指令請參考官方中文文檔。
這個模塊可以轉(zhuǎn)發(fā)請求到其他的服務(wù)器。http/1.0無法使用keepalive(后端服務(wù)器將為每個請求創(chuàng)建并且刪除連接)。nginx為瀏覽器發(fā)送http/1.1并為后端服務(wù)器發(fā)送http/1.0,這樣瀏覽器就可以為瀏覽器處理keepalive。????
如下例:

location?/?{ ?proxy_pass????http://localhost:8000; ?proxy_set_header?x-real-ip?$remote_addr; }

注意,當(dāng)使用http proxy模塊(甚至fastcgi),所有的連接請求在發(fā)送到后端服務(wù)器之前nginx將緩存它們,因此,在測量從后端傳送的數(shù)據(jù)時,它的進(jìn)度顯示可能不正確。

實(shí)驗(yàn)拓?fù)洌?/p>

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

默認(rèn)值:no??????

使用字段:location, location中的if字段??????

這個指令設(shè)置被代理服務(wù)器的地址和被映射的uri,地址可以使用主機(jī)名或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.測試一下

nginx代理模塊怎么使用

注,大家可以看到,當(dāng)我們訪問192.168.18.208時,被代理重新定向到web1上。

10.查看一下web服務(wù)器日志

[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代理服務(wù)器的ip,并不是真實(shí)客戶端的ip。下面我們修改一下,讓日志的ip顯示真實(shí)的客戶端的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

默認(rèn)值: host and connection

使用字段:http, server, location

這個指令允許將發(fā)送到被代理服務(wù)器的請求頭重新定義或者增加一些字段。這個值可以是一個文本,變量或者它們的組合。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,沒有顯示真實(shí)客戶端的ip,為什么呢?我們來看一下httpd的配置文件。

14.查看并修改httpd配置文件

[root@web1?~]#?vim?/etc/httpd/conf/httpd.conf

nginx代理模塊怎么使用

注,大家可以這里記錄日志的參數(shù)還是%h,下面我們修改一下參數(shù)。

nginx代理模塊怎么使用

注,這是修改后的參數(shù),將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)"

注,大家可以看到現(xiàn)在的日志里記錄的ip地址就是真實(shí)的客戶端地址了。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊12 分享