今日我們一起學習nginx_http_proxy_module,實現反向代理及緩存功能。
先學習nginx負載均衡:
在http上下文定義server組
http {
upstream backend {
server IP:PORT weight=num max_fails=num fail_timeout=num;
server IP:PORT ..;
}
}
在location上下文使用server組:
location / {
proxy_pass http://backend;
}
實驗環境:
主機名稱 |
主要功能 |
外網地址 |
內網地址 |
Nginx |
Nginx代理服務器 |
192.168.1.135 |
10.0.0.6 |
Node1 |
Httpd應用服務器 |
|
10.0.0.7 |
Node2 |
Httpd應用服務器 |
|
10.0.0.8 |
2.配置nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
在主配置文件的http段使用upstream定義一個server組
upstream httpserver {
server 10.0.0.7;
server 10.0.0.8;
}
在後面的location / 中使用proxy_pass設置將用戶請求發送到server組中
location / {
proxy_pass http://httpserver;
root html;
index index.html index.htm;
}
3.分別在node1和node2安裝httpd並啓動起來
yum -y install httpd
service httpd start
echo “this is node1” > /var/www/html/index.html (在node1)
echo “this is node2” > /var/www/html/index.html (在node2)
4.在瀏覽器登錄http://192.168.1.135
進行刷新。查看是否在變化;node1和node2輪詢
5.停止node1的服務器
service httpd stop
6.查看瀏覽器變化,此時只能訪問node2