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中文網其他相關文章!

NGINX可通过以下方式提升网站性能和可靠性:1.作为Web服务器处理静态内容;2.作为反向代理服务器转发请求;3.作为负载均衡器分配请求;4.作为缓存服务器减轻后端压力。通过配置优化如启用Gzip压缩和调整连接池,NGINX能显著提高网站性能。

nginxserveswebcontentandactsasareverseproxy,loadBalancer和more.1)效率高效的servesstaticContentLikeHtmlandImages.2)itfunctionsasareverseproxybalancer,and andginxenhanceperforfforfforfforfforfforffrenfcaching.4)

NGINXUnit通過動態配置和多語言支持簡化應用部署。 1)動態配置無需重啟服務器即可修改。 2)支持多種編程語言,如Python、PHP、Java。 3)採用異步非阻塞I/O模型,提升高並發處理性能。

NGINX起初解決C10K問題,現已發展為處理負載均衡、反向代理和API網關的全能選手。 1)它以事件驅動和非阻塞架構聞名,適合高並發。 2)NGINX可作為HTTP和反向代理服務器,支持IMAP/POP3。3)其工作原理基於事件驅動和異步I/O模型,提升了性能。 4)基本用法包括配置虛擬主機和負載均衡,高級用法涉及復雜負載均衡和緩存策略。 5)常見錯誤包括配置語法錯誤和權限問題,調試技巧包括使用nginx-t命令和stub_status模塊。 6)性能優化建議包括調整worker參數、使用gzip壓縮和

Nginx常見錯誤的診斷與解決方法包括:1.查看日誌文件,2.調整配置文件,3.優化性能。通過分析日誌、調整超時設置和優化緩存及負載均衡,可以有效解決404、502、504等錯誤,提高網站穩定性和性能。

nginxunitischosenfordEployingApplicationsDuetoItsflexibility flexibility,sisofuse,andability tohandledynamicApplications.1)itupportsmultProgramprogrogminglanguagesLikeLikeLikePython,php,node.js,andjava.2)

NGINX可用於服務文件和管理流量。 1)配置NGINX服務靜態文件:定義監聽端口和文件目錄。 2)實現負載均衡和流量管理:使用upstream模塊和緩存策略優化性能。

NGINX適合處理高並發和靜態內容,Apache適用於動態內容和復雜URL重寫。 1.NGINX採用事件驅動模型,適合高並發。 2.Apache使用進程或線程模型,適用於動態內容。 3.NGINX配置簡單,Apache配置複雜但更靈活。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。