您好,登錄后才能下訂單哦!
這篇文章主要介紹了nginx代理模塊怎么使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇nginx代理模塊怎么使用文章都會有所收獲,下面我們一起來看看吧。
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代理模塊怎么使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“nginx代理模塊怎么使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。