今日我们一起学习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