提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一. 特点
• 功能强大,性能卓越,运行稳定。
• 配置简单灵活。
• 能够自动剔除工作不正常的后端服务器。
• 上传文件使用异步模式。client—nginx—web1 web2 web3 lvs同步请求 DR client -->dr—web1
• 支持多种分配策略,可以分配权重,分配方式灵活。
优点:
nginx复制用户请求,在后端服务器出现问题时nginx会再复制一份请求发给另一台后端服务器。
lvs则在这种情况,只能用户重新发请求
缺点:
所有流量会经过nginx,nginx成为瓶颈
二. nginx_proxy实现读写分离
环境:
client:192.168.93.136
nginx_proxy: 192.168.93.137
web1: 192.168.93.138:80
web2: 192.168.93.138:8080
php1: 192.168.93.140
php2: 192.168.93.142
- web服务器 安装nginx 并基于8080端口配置虚拟主机
- nginx_proxy配置
yum -y install nginx
vim /etc/nginx/nginx.conf
upstream html{
server 192.168.93.138:80;
server 192.168.93.138:8080;
}
upstream php{
server 192.168.93.140:80;
server 192.168.93.142:80;
}
location / {
proxy_pass http://html;
}
location ~\.php$ {
proxy_pass http://php;
}
注:
upstream 跟 server同级别 写在http{}内
location 写在 server{}内
如果是用nginx官网yum配置的 要在nginx.conf中配置upstream 在default.conf中配置location 注释掉原本的location
- client端测试
curl 192.168.93.137 //访问静态页面
curl 192.168.93.137/index.php //访问动态页面
三. nginx_proxy实现站点分区调度
环境:
client: 192.168.93.136
nginx_proxy: 192.168.93.137
web1(news): 192.168.93.138
web2(news): 192.168.93.139
web3(mail): 192.168.93.140
web4(mail): 192.168.93.142
- 部署web服务器
# yum -y install nginx
web1 web2操作如下:
# mkdir /usr/share/nginx/html/news
# vim /usr/share/nginx/html/news/news.html
# echo news1 >/usr/share/nginx/html/news/news.html
# echo news2 >/usr/share/nginx/html/news/news.html
web3 web4操作如下:
# mkdir /usr/share/nginx/html/mail
# vim /usr/share/nginx/html/mail/mail.html
# echo mail1 >/usr/share/nginx/html/news/mail.html
# echo mail2 >/usr/share/nginx/html/news/mail.html
- 配置nginx_proxy
# vim /etc/nginx/nginx.conf
upstream news{
server 192.168.93.138:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.93.138:8080 weight=1 max_fails=2 fail_timeout=2;
}
upstream mail{
server 192.168.93.140:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.93.142:80 weight=1 max_fails=2 fail_timeout=2:
}
location /news {
proxy_pass http://news/;
}
location /mail {
proxy_pass http://mail/;
}
- 浏览器测试
http:192.168.93.137 网站主页 nginx_proxy提供
http:192.168.93.137/news 在web1 web2之间轮询
http:192.168.93.137/mail 在web3 web4之间轮询