反向代理:是用來代理服務器的,代理我們要訪問的目標服務器。
代理服務器接受請求,然后將請求轉發給內部網絡的服務器(集群化),并將從服務器上得到的結果返回給客戶端,此時代理服務器對外就表現為一個服務器。
nginx在反向代理上,提供靈活的功能,可以根據不同的正則采用不同的轉發策略,設置好后不同的請求就可以走不同的服務器。
下面演示如何進行配置使Nginx發揮作用。?
模擬n個http服務器作為目標主機?
用作測試,簡單的使用2個tomcat實例模擬兩臺http服務器,分別將tomcat的端口改為8081和8082?
配置IP域名?
192.168.72.49 8081
192.168.72.49 8082
配置nginx.conf
upstream tomcatserver1 { server 192.168.72.49:8081; } upstream tomcatserver2 { server 192.168.72.49:8082; }server { listen 80; server_name 8081.max.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcatserver1; index index.html index.htm; } }server { listen 80; server_name 8082.max.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcatserver2; index index.html index.htm; } }
流程:?
1)瀏覽器訪問8081.max.com,通過本地host文件域名解析,找到192.168.72.49服務器(安裝nginx)?
2)nginx反向代理接受客戶機請求,找到server_name為8081.max.com的server節點,根據proxy_pass對應的http路徑,將請求轉發到upstream tomcatserver1上,即端口號為8081的tomcat服務器。?
更多Nginx相關技術文章,請訪問Nginx使用教程欄目進行學習!
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END